gpt4 book ai didi

php - PayPal REST API (PHP) 未完成交易

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:32 24 4
gpt4 key购买 nike

我对 PayPal 的 REST API 比较陌生,所以我可能在这里遗漏了一个关键元素,但我所做的是设置一个类似于他们在 GitHub 中的示例:

https://github.com/paypal/rest-api-sdk-php/blob/master/sample/payments/CreatePaymentUsingPayPal.php

我非常接近这一点,因为我将 API 上下文设置为我在 PayPal 的开发者网站上创建的应用程序中的客户端 ID 和 secret (首先使用沙箱,现在使用实时沙箱)。这是我创建 apiContext 后的部分:

            $payer = new Payer();
$payer->setPayment_method("paypal");

$item = new Item();
$item -> setName($itemDescription)
-> setCurrency("USD")
-> setQuantity(1)
-> setPrice($itemPrice);

$itemList = new ItemList();
$itemList->setItems(array($item));

$amount = new Amount();
$amount->setCurrency("USD")
->setTotal($itemPrice);

$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($itemDescription);

$redirectURLs = new RedirectUrls();
$redirectURLs->setReturnUrl($successURL);
$redirectURLs->setCancelUrl($cancelURL);

$payment = new Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectURLs);
$payment->setTransactions(array($transaction));

try {
$payment->create($apiContext);
}
catch (PayPal\Exception\PPConnectionException $ex) {

print "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
exit(1);
}

# get the redirect URL to pass the user on to PayPal for payment processing
$redirectURL = "";
foreach ( $payment->getLinks() as $link ) {
if ( $link->getRel() == "approval_url" ) {
$redirectURL = $link->getHref();
break;
}
}

// if we get a URL, redirect the user to PayPal's website
if ( $redirectURL != "" ) {

$ppPayID = $payment->getId();

# write the ID to the DB
$dbQuery = "update members set ppPayID = '" . dbAccess("sanitize", $ppPayID) . "' where memberID = $memberID;";

# update the DB
$dbResults = dbAccess("query", $dbQuery);

//header("Location: $redirectURL");
print "<script language='JavaScript'>location.href = '$redirectURL';</script>";
exit;
}
else {
return false;
}

一切似乎都正常:我在传递到 PayPal 时看到了商品信息,我可以取消交易并通过取消 URL 返回我的 PHP 应用程序,我什至可以使用真实账户提交付款并通过成功 URL 返回到我的应用程序。这是它的样子;

http://example.com/scripts.php?pp=success&memberID=1&token=EC-7G861271R3854####&PayerID=77PAUYJCFJ###

(#s 也是字母数字字符,以防万一)

我遇到的问题是,虽然交易看起来很成功,但交易并没有出现在我的商家账户中,也没有出现在我的个人账户中。知道这里发生了什么吗?

最佳答案

因此,该示例缺少交易流程的关键部分:执行付款。对于其他为此苦苦挣扎的人,这里有一段必要的代码来实现这一点:

https://developer.paypal.com/docs/api/#execute-an-approved-paypal-payment

注意:您需要同时捕获 $payment->getID,就像我在数据库中所做的那样,然后将 URL PayerID 发回。

关于php - PayPal REST API (PHP) 未完成交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25272377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com