如何将金额字段设置为 PayPal SDK
How can I set amount field to PayPal SDK
我正在使用 PayPal Express 并希望将金额字段值发送到服务器文件 (payment.php)
从客户端脚本中获取它的值到服务器端文件。
这是客户端脚本
<script src="https://www.paypal.com/sdk/js?client-id=<client-id>"></script>
<script>
paypal.Buttons({
createOrder: function() {
return fetch('payment.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.id;
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
window.location = "paypal-transaction-complete.php?&orderID="+data.orderID;
});
}
}).render('#paypal-button-container');
</script>
<div id="paypal-button-container"></div>
这里是 payment.php 我想获取金额字段值的地方
<?php
namespace Sample\CaptureIntentExamples;
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\OrdersCreateRequest;
require 'paypal-client.php';
class CreateOrder
{
public static function createOrder($debug=false)
{
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = self::buildRequestBody();
// 3. Call PayPal to set up a transaction
$client = PayPalClient::client();
$response = $client->execute($request);
echo json_encode($response->result, JSON_PRETTY_PRINT);
return $response;
}
private static function buildRequestBody()
{
return array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/cancel'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'USD',
'value' => '20.00'
)
)
)
);
}
}
if (!count(debug_backtrace()))
{
CreateOrder::createOrder(true);
}
?>
请告诉我该怎么做?谢谢!
使用 PHP 最简单的方法是使用 GET 字符串。
return fetch('payment.php?amount=' + document.getElementById('amount').value , {
^ 您可能想要验证或清理该输入或 URL 对值进行编码,但在大多数情况下它应该可以工作。
array(
'currency_code' => 'USD',
'value' => $_GET['amount']
)
它不是现代的,但对你来说 PHP,解析 JSON 可能有点矫枉过正。
你不应该在服务器上创建时使用 actions.order.capture()
,如果它能工作我会感到惊讶。在您的服务器上实施捕获订单路由,并使用具有正确错误处理的此 JS 示例:https://developer.paypal.com/demo/checkout/#/pattern/server
我正在使用 PayPal Express 并希望将金额字段值发送到服务器文件 (payment.php) 从客户端脚本中获取它的值到服务器端文件。
这是客户端脚本
<script src="https://www.paypal.com/sdk/js?client-id=<client-id>"></script>
<script>
paypal.Buttons({
createOrder: function() {
return fetch('payment.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.id;
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
window.location = "paypal-transaction-complete.php?&orderID="+data.orderID;
});
}
}).render('#paypal-button-container');
</script>
<div id="paypal-button-container"></div>
这里是 payment.php 我想获取金额字段值的地方
<?php
namespace Sample\CaptureIntentExamples;
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\OrdersCreateRequest;
require 'paypal-client.php';
class CreateOrder
{
public static function createOrder($debug=false)
{
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = self::buildRequestBody();
// 3. Call PayPal to set up a transaction
$client = PayPalClient::client();
$response = $client->execute($request);
echo json_encode($response->result, JSON_PRETTY_PRINT);
return $response;
}
private static function buildRequestBody()
{
return array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/cancel'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'USD',
'value' => '20.00'
)
)
)
);
}
}
if (!count(debug_backtrace()))
{
CreateOrder::createOrder(true);
}
?>
请告诉我该怎么做?谢谢!
使用 PHP 最简单的方法是使用 GET 字符串。
return fetch('payment.php?amount=' + document.getElementById('amount').value , {
^ 您可能想要验证或清理该输入或 URL 对值进行编码,但在大多数情况下它应该可以工作。
array(
'currency_code' => 'USD',
'value' => $_GET['amount']
)
它不是现代的,但对你来说 PHP,解析 JSON 可能有点矫枉过正。
你不应该在服务器上创建时使用 actions.order.capture()
,如果它能工作我会感到惊讶。在您的服务器上实施捕获订单路由,并使用具有正确错误处理的此 JS 示例:https://developer.paypal.com/demo/checkout/#/pattern/server