【发布时间】:2018-05-06 00:20:42
【问题描述】:
我试图使用 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);
当我进入沙盒时,我收到“抱歉,我们目前无法完成您的购买”,并带有一个返回我的网站的按钮
【问题讨论】:
-
你能记录下 JSOn 请求响应吗?
-
@RahulDighe 请问我该怎么做?
-
你可以通过单步执行这个语句进行调试 $payment->create($api);在某些时候,必须生成 JSON。另外,您的代码在哪里添加类似这样的项目 - paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/… ?