【发布时间】:2020-05-12 08:44:50
【问题描述】:
我正在使用 PHP REST APIv1(我相信)与 paypal 集成。
我的请求代码如下:
include '../../classes/db.connect.php';
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
require 'bootstrap.php';
if (empty($_POST['buy_now'])) {
throw new Exception('This script should not be called directly, expected post data');
}
$setitem = strip_tags($_POST['item']);
$stmt=$db->prepare("SELECT * FROM shopping WHERE id = :id");
$stmt->bindParam(':id', $setitem);
$stmt->execute();
$row = $stmt->fetch();
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$currency = 'USD';
$amountPayable = $row['price'];
$item1 = new Item();
$item1->setName($row['name'])
->setCurrency('USD')
->setQuantity('1')
->setPrice($amountPayable);
$invoiceNumber = uniqid();
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($amountPayable);
$itemList = new ItemList();
if (isset($item1) && isset($item2)) {
$itemList->setItems(array($item1, $item2));
} elseif (isset($item1)) {
$itemList->setItems(array($item1));
} elseif (isset($item2)) {
$itemList->setItems(array($item2));
}
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Website '.$row['name'].' Purchase')
->setInvoiceNumber($invoiceNumber)
->setNotifyUrl("https://REDACTED/notify.php")
->setCustom($username);
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($paypalConfig['return_url'])
->setCancelUrl($paypalConfig['cancel_url']);
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction])
->setRedirectUrls($redirectUrls);
try {
$payment->create($apiContext);
} catch (Exception $e) {
throw new Exception('Unable to create link for payment');
}
header('location:' . $payment->getApprovalLink());
付款顺利进行,但 paypal 不调用 notify_url。根本没有错误。用户可以付款,付款通过,用户返回setReturnUrl,但setNotifyUrl似乎被忽略了。
我在$transaction 上做了一个var_dump,notify_url 被发送到了贝宝,但贝宝没有调用它——因此我没有收到交易通知/数据库中没有任何更新。
我已经完成了我能想到的最基本的测试,以确保贝宝没有调用notify_url。文件中的php简单设置为:
include '../../classes/db.connect.php';
$stmt = $db->prepare("INSERT INTO test (info) VALUES ('1122')");
$stmt->execute();
我意识到 REST 旨在与 webhook 一起使用 - 但出于多种原因,我不想使用它们。我的理解是设置 notify_url 将允许贝宝将该 url 用作 IPN。但是我发现贝宝只是忽略了这个变量。
另外我应该提到我已经尝试过启用 IPN 并禁用 IPN 状态更新,我尝试过启用 IPN 并启用状态更新,并且我尝试过禁用 IPN。所有三种方式都有相同的结果 - 没有错误,一切正常,但贝宝没有发送或接收任何通知。
我在贝宝上的 IPN 故事页面完全是空的。
我做错了吗?误会了什么?
我找不到任何关于此的可靠信息。
【问题讨论】:
-
“我的理解是设置一个 notify_url 将允许贝宝使用该 url 作为 IPN。但是我发现贝宝只是忽略了这个变量。”你应该联系贝宝的支持来询问这个问题,因为只有他们会知道在特定情况下发生了什么。