paypal SDK的逻辑问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了paypal SDK的逻辑问题相关的知识,希望对你有一定的参考价值。

我已经将PayPal SDK与沙箱帐户集成在一起。这是我的网站上的自定义包名称及其价格。比如基本套餐79美元,专业套餐150美元,专业400美元。一旦用户点击继续按钮,它就会重定向到PayPal,基本包和79美元的价格。对于其他包我需要做什么逻辑?

我试图将一个数组($ basic,$ pro,$ professional)插入到item对象中,但是Setprice()它不接受数组。

我只是坚持它在这里输入代码它甚至我不知道如何提出问题非常抱歉。

这是我的checkout.php

    if ($_POST) {

        $priceBasic= (is_numeric($_POST['b']) ? (int)$_POST['b'] : 89); 
        $pkgBasic = $_POST['basic'];

        $pricePro= (is_numeric($_POST['p']) ? (int)$_POST['p'] : 175);  
        $pkgPro = $_POST['pro'];

        $priceUltra= (is_numeric($_POST['u']) ? (int)$_POST['u'] : 449);    
        $pkgUltra = $_POST['ultra'];

        $shipping = 0.0;
        $total = $priceBasic + $shipping;

        //PAYER INSTANT
         $payer = new Payer();
         $payer->setPaymentMethod('paypal');

        // DEFINING ITEMS

        // $packages = [$pkgBasic, $pkgPro, $pkgUltra];
        // $price = [$priceBasic, $pricePro, $priceUltra];

        $item = new Item();
        $item->setName($packages)->setCurrency('USD')->setQuantity(1)-
        >setPrice($priceBasic);

        //PACKAGES LIST
        $itemList = new ItemList();
        $itemList->setItems([$item]);

         $details = new Details();
         $details->setShipping($shipping)
        ->setSubTotal($total);

        $amount = new Amount();
        $amount->setCurrency('USD')->setTotal($total)
        ->setDetails($details);
        $transaction = new Transaction();

        $transaction->setAmount($amount)->setItemList($itemList)-
        >setDescription('Hello Hash Thanks for first paypal')-
        >setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl(SITE_URL.'/pay.php?success=true')
        ->setCancelUrl(SITE_URL.'/pay.php?success=false');


        $payment = new Payment();
        $payment->setIntent('sale')->setPayer($payer)
       ->setRedirectUrls($redirectUrls)->setTransactions([$transaction]);


       try {
         $payment->create($paypal);
       } catch (Exception $e) {

       die($e);

     }

     $aprovalUrl = $payment->getApprovalLink();

     // echo $aprovalUrl;
     header("location:".$aprovalUrl);

  }

?>`
答案

您可以使用开关盒在价格之间切换。希望这可以帮助。

$package_type = $_POST['package_type'];

switch($package_type) {
    case "basic":
        $price = 89;
    break;
    case "pro":
        $price = 175;
    break;
    case "ultra":
        $price = 449;
    break;
}


$shipping = 0.0;
$total = $priceBasic + $shipping;

//PAYER INSTANT
 $payer = new Payer();
 $payer->setPaymentMethod('paypal');

// DEFINING ITEMS

// $packages = [$pkgBasic, $pkgPro, $pkgUltra];
// $price = [$priceBasic, $pricePro, $priceUltra];

$item = new Item();
$item->setName($packages)->setCurrency('USD')->setQuantity(1)-
>setPrice($price);


//PACKAGES LIST
$itemList = new ItemList();
$itemList->setItems([$item]);

 $details = new Details();
 $details->setShipping($shipping)
->setSubTotal($total);


$amount = new Amount();
$amount->setCurrency('USD')->setTotal($total)
->setDetails($details);
$transaction = new Transaction();

 $transaction->setAmount($amount)->setItemList($itemList)-
 >setDescription('Hello Hash Thanks for first paypal')-
 >setInvoiceNumber(uniqid());

 $redirectUrls = new RedirectUrls();
 $redirectUrls->setReturnUrl(SITE_URL.'/pay.php?success=true')
 ->setCancelUrl(SITE_URL.'/pay.php?success=false');


 $payment = new Payment();
 $payment->setIntent('sale')->setPayer($payer)
->setRedirectUrls($redirectUrls)->setTransactions([$transaction]);


try {
$payment->create($paypal);
} catch (Exception $e) {

die($e);

}

  $aprovalUrl = $payment->getApprovalLink();

// echo $aprovalUrl;
header("location:".$aprovalUrl);

?>

切换案例,检查此代码是否适合您。这是您必须根据计划切换价格的方式。

<?php
$package_type = 'pro';
$price = 0;

switch($package_type) {
    case "basic":
        echo 'hai';
    break;
    case "pro":
        $price = 175;
    break;
    case "ultra":
        $price = 449;
    break;
}
echo $price;

?>

以上是关于paypal SDK的逻辑问题的主要内容,如果未能解决你的问题,请参考以下文章

通过 PayPal iOS SDK 进行的 PayPal 实时交易中的问题

如何使用 Paypal 自适应支付 SDK?

Paypal .Net SDK 实施

使用 paypal-rest-sdk 时出错。请帮助我

create_with_paypal 节点 sdk PayPal 中的发票编号

贝宝 sdk '类 'PayPal\Rest\ApiContext' 未找到'