【发布时间】:2020-05-10 22:54:57
【问题描述】:
案例:通过智能支付按钮设置订阅时出错
备注:在 Live 环境中,我确实在我的帐户中收取了交易费用
错误:有一个捕获错误,从 Sandbox 和 Live 案例的 3 天测试来看,没有找到一个解决方案
对于沙盒模式,我发现了一些关于完全相同错误的参考资料,但对于那些人来说,这是一个错误。似乎在一夜之间消失了 2。那些不是订阅模式,而是常规购买模式。 以下是脚本,它不应该像我制作的那么难,我们在几年前建立了一个类似的计费环境,并且几乎立即生效,但这不是订阅。
更多详情: - 我确实在作曲家文件中设置了正确的环境设置。 - 产品在那里 - 计划在那里 - 我们使用基于座位的定价(0.01 美分,然后我们将总金额乘以美元 *100)
////////////////////////////////////
// Error 500
////////////////////////////////////
// 通过控制台 发布https://www.paypal.com/smart/api/order/9VU587...34202/capture 500
// 通过网络
{ack: "error", message: "Unhandled api error", meta: {calc: "4ac27dc9b8a70",…},…}
////////////////////////////////////
// Smart Button Script
////////////////////////////////////
<script src="https://www.paypal.com/sdk/js?vault=true&client-id=<?= $paypal_sandbox_id ?>¤cy=<?php echo $currency ?? "USD"; ?>&debug=false"></script>
<script>
paypal.Buttons({
// Set up the subscription
createSubscription: function (data, actions) {
return actions.subscription.create({
'plan_id': 'P-6NH76920JR31236564LYU3X4Y',
'quantity': total_billed_vat * 100
});
},
// Finalize the transaction
onApprove: function (data, actions) {
console.log('onApprove', data);
// Authorize the transaction
return actions.order.capture().then(function (details) {
console.log('capture', details);
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
// Call your server to save the transaction
return fetch('../api/paypal/paypal-transaction-complete.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
}).then(function (response) {
// Show a success message to the buyer
alert('actions.order.capture done ' + details.payer.name.given_name + '!');
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert('Feel free to retry when you are ready');
}
}).render('#paypal-button-container');
</script>
PHP 服务器端脚本:
////////////////////////////////////
// ../api/paypal/paypal-transaction-complete.php
////////////////////////////////////
<?php
namespace Sample;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
/**
*Enable the following line to print complete response as JSON.
*/
//print json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
}
/**
*This driver function invokes the getOrder function to retrieve
*sample order details.
*
*To get the correct order ID, this sample uses createOrder to create a new order
*and then uses the newly-created order ID with GetOrder.
*/
if (!count(debug_backtrace()))
{
GetOrder::getOrder($data->orderID, true);
}
用于 PayPal 集成 v2 的 SDK。
////////////////////////////////////
// SDK Installed in ../api/paypal/
////////////////////////////////////
{
"require": {
"paypal/paypal-checkout-sdk": "^1.0"
}
}
二手手册来源:https://developer.paypal.com/docs/subscriptions/integrate/ 发现的问题资源之一:https://www.paypal-community.com/t5/REST-APIs/BASIC-Smart-Payment-buttons-integration-help/td-p/1844051
【问题讨论】:
-
嗨@Kim K。我正在使用新的智能按钮,但我遇到了同样的错误。你解决过这个问题吗?
-
@AndresSK 之类的,我在回复 Preston 对帖子的回复时发布了该内容。 Quote: 我发现对于订阅模型,“actions.order.capture()”行不被接受或不受欢迎。似乎较新的 Smart Button 物流已经解决了所有这些问题,并且只要购买良好就可以退货。到目前为止一切都很好,测试都返回了积极的结果。待处理的“真实”交易。
标签: paypal paypal-sandbox