【发布时间】:2019-11-09 09:17:52
【问题描述】:
我正在尝试迁移我们的 Paypal PHP 集成以利用最新的 SDK v2。
我们目前的 NVP 设置只需要 2 个 paypal 方法
- 设置ExpressCheckOut
- 返回交易令牌(EC-825147XXXXXXXXXXX)
- GetExpressCheckout 详细信息
- return token & PayerID (PayerID: MYT3NHXXXXXX)
我已经设法更新了第一个方法“SetExpressCheckout”方法, 现在看起来像这样..
$token = $paypal->SetExpressCheckOut($products, $charges);
echo json_encode( array('token'=>$token ) );
只有 2 行,其中包含我们的一系列产品和任何其他运输/处理费用并返回了交易令牌,但新方法很疯狂,它只返回相同的交易 ID....
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal($cartTotal);
$amount->setCurrency('GBP');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://www.paypal.com/checkoutnow/error")
->setCancelUrl("https://www.paypal.com/checkoutnow/error");
$payment = new \PayPal\Api\Payment();
$payment->setIntent('authorize')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
try {
// $apiContext, set within our bootstrap file
$payment->create($apiContext);
// need to dig through the response 'approval_url'
// Locate token in links array,
// object "rel" == "approval_url":
/*
{
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXXXXXX",
"rel": "approval_url",
"method": "REDIRECT"
}
*/
// looking for token: EC-XXXXXXXXXXXXXXXXX
$res_data = json_decode($payment, true);
$urlInfo = parse_url($res_data['links'][1]['href']);
parse_str($urlInfo['query'], $queryParams);
// PASS THE TOKEN BACK TO JS FILE
echo json_encode(
array(
'token'=>$queryParams['token'],
'id' => $res_data['id']
)
);
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// Print detailed information on the exception.
// HELPFUL FOR DEBUGGING
echo $ex->getData();
}
但尝试复制旧的 GetExpressCheckoutDetails 以返回 payerID 是有问题的。
我的旧 GetExpressCheckoutDetails 方法看起来像这样..
$cartTotal = Context::getContext()->cart->getOrderTotal(true);
echo json_encode($paypal->GetExpressCheckoutDetails($cartTotal));
/*
return(
'res' => 'Success'
'token' => 'EC-XXXXXXXXXXXX',
'payerID' => 'MYT3NHXXXXXX'
)
*/
2行代码就搞定了!..
但我尝试了各种组合,试图获得相同的返回值,但没有成功,并四处寻找适合这种旧方法的替代品。
paypal-rest-api-still-lacking-an-equivalent-of-getexpresscheckoutdetails
如果有人能阐明如何在付款被捕获后返回 payerID,那就太好了!..
非常感谢
马蒂
【问题讨论】:
标签: php paypal sdk integration