PayPal PHP SDK - 取消发票功能不执行任何操作

PayPal PHP SDK - cancel invoice function does nothing

我正在为 PHP 使用 PayPal SDK,我正在尝试取消发票,返回的结果是 "true",没有返回异常,但发票没有取消。如果我的代码有错误,你能告诉我吗?

$Invoice = new Invoice();

try {
    $invoice = $Invoice->get($id_invoice, $apiContext);
    $notify = new CancelNotification();
    $notify->setSubject("Past due")
           ->setNote("Canceling invoice")
           ->setSendToMerchant(true)
           ->setSendToPayer(true);
    $result = $Invoice->cancel($notify, $apiContext);

} catch (Exception $ex) {
    $result = self::getException($ex);
}

return $result;

首先像这样获取发票对象:

$invoice = Invoice::get($invoiceId, $apiContext);

然后,您可以执行以下操作来取消它。

// ### Cancel Notification Object
// This would send a notification to both merchant as well
// the payer about the cancellation. The information of
// merchant and payer is retrieved from the invoice details
$notify = new CancelNotification();
$notify
    ->setSubject("Past due")
    ->setNote("Canceling invoice")
    ->setSendToMerchant(true)
    ->setSendToPayer(true);
// ### Cancel Invoice
// Cancel invoice object by calling the
// static `cancel` method
// on the Invoice class by passing a valid
// notification object
// (See bootstrap.php for more on `ApiContext`)
$cancelStatus = $invoice->cancel($notify, $apiContext);

另外,要测试代码,您可以随时 运行 samples,然后自己测试,只需单击一个按钮。

我运行 sample to cancel 一张发票,然后使用提供的类似信息得到发票回复:

"metadata": {
    "created_date": "2015-02-04 13:12:33 PST",
    "first_sent_date": "2015-02-04 13:12:34 PST",
    "last_sent_date": "2015-02-04 13:12:34 PST",
    "payer_view_url": "https://www.sandbox.paypal.com/cgi_bin/webscr?cmd=_pay-inv&viewtype=altview&id=INV2-6S46-MLLN-3FEA-VLZE"
}

打开 URL 显示发票已取消,如下所示: