我对PayPal的REST api比较陌生,所以我可能在这里缺少一个关键元素,但我所做的是在GitHub中设置一个类似于他们的例子:
https://github.com/paypal/rest-api-sdk-php/blob/master/sample/payments/CreatePaymentUsingPayPal.php我非常接近这一点,因为我将API上下文设置为我在PayPal的开发者网站上创建的应用程序中的客户端ID和Secret(首先使用沙箱,现在使用live)。下面是我创建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,令牌= EC-7G861271R3854 # # # #, PayerID = 77 pauyjcfj # # #
(其中#s也是去掉的字母数字字符,以防万一)
我的问题是,虽然它似乎是成功的,交易没有显示在我的商户账户的任何地方,也没有出现在我的个人账户。知道这是怎么回事吗?
因此,该示例缺少事务流程的关键部分:执行支付。对于其他遇到这个问题的人,这里有一段必要的代码来实现这一点:
https://developer.paypal.com/docs/api/execute-an-approved-paypal-payment
注意:您需要捕获$payment->getID,就像我在数据库中所做的那样,并发送URL PayerID返回。
这个例子有两个部分。第二部分是接收成功返回的URL,并处理请求。
正如您在https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/ExecutePayment.php中看到的那样,您需要使用从请求返回的ID执行支付。
根据您的用例,我将继续,看看我是否可以合并那个页面与这个页面,以便它变得更明显的人。如果没有,我希望在示例代码中非常非常清楚地说明,在该示例中不止有php文件