paypal-sdk - 很抱歉,我们目前无法完成您的购买

时间:2017-11-22 10:01:44

标签: php paypal checkout

我尝试使用PayPal SDK实施结帐,但遗憾的是我无法让它正常运行。它只适用于我在购物车中只有一个项目看起来很奇怪的情况。 我非常确定我的配置有问题,但在线文档很少,所以这里是我的代码

$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();

// Payer
$payer -> setPaymentMethod("paypal");

$total = 0;

foreach ($cart as $item => $quantity) {
   //this loop just aggregates the total for the purchase

    $total += $quantity * $price;
}

// Details
$details -> setShipping("0.00")
    -> setTax("0.00")
    -> setSubtotal($total);

// Amount
$amount -> setCurrency("USD")
    -> setTotal($total)
    -> setDetails($details);

// Transaction
$transaction
    -> setAmount($amount)
    -> setDescription("Analysis & Development");

// Payment
$payment -> setIntent("sale")
    -> setPayer($payer)
    -> setTransactions([$transaction]);

// Redirect URLs
$redirectUrls -> setReturnUrl($base_url."php/pay.php?approved=true")
    -> setCancelUrl($base_url."php/pay.php?approved=false");

$payment->setRedirectUrls($redirectUrls);

try {

    $payment->create($api);

    // code to save transaction temporarily omitted

} catch (Exception $e) {
    header("Location: ../checkout.php?error=".$e->getMessage());
    die();
}

foreach ($payment->getLinks() as $link) {
    if($link->getRel() == "approval_url")
        $redirectUrl = $link -> getHref();
}

header("location: ".$redirectUrl);

当我到达沙箱时,我得到了#34;抱歉,我们目前无法完成购买"用一个按钮返回我的网站

1 个答案:

答案 0 :(得分:0)

vbtheory,

您没有初始化“ $ price ”变量。你可以检查一下吗?初始化它。如果问题仍然存在,请联系PayPal技术团队以获得更好的&快速帮助:

https://www.paypal-techsupport.com