使用PayPal签到PHP SDK时,是否可以获取买家的地址?
我有此代码用于处理我的网站上的PayPal付款:
$environment = new \PayPalCheckoutSdk\Core\SandboxEnvironment(PAYPAL_ID, PAYPAL_SECRET);
$client = new \PayPalCheckoutSdk\Core\PayPalHttpClient($environment);
$authorizationId = $_POST['authorizationId'];
$request = new \PayPalCheckoutSdk\Payments\AuthorizationsGetRequest($authorizationId);
$authorizationResponse = $client->execute($request);
$orderId = $authorizationResponse->result->supplementary_data->related_ids->order_id;
$request = new OrdersGetRequest($orderId);
$orderResponse = $client->execute($request);
var_dump($orderResponse->result->payer->address);
当我var_dump地址sdtobject时,我只得到这个代码,
public 'address' =>
object(stdClass)[74]
public 'country_code' => string 'FR' (length=2)
我只是想在完成订单后将其地址存储在我的数据库中,因为他们是在我的数据库中,因为他们是作为“客人购买的” ”。
即使您使用PayPal的卡选项付款,但他们要求您放置您的运输地址,也会发生这种情况。
该订单是这样创建的
$order = json_encode([
'purchase_units' => [
[
'description' => 'some desc...',
'items' => array_map(function ($product) {
return [
'name' => $product['name'] . " (n°" . $product['num'] . " | " . strtoupper($product['language']) . ")",
'quantity' => $_SESSION['cart'][$product['id']],
'unit_amount' => [
'value' => number_format((float)$product['price'], 2, '.', ''),
'currency_code' => 'USD',
]
];
}, $products),
'amount' => [
'currency_code' => 'USD',
'value' => $total > 200 ? $total : $total + 8,
'breakdown' => [
'item_total' => [
'currency_code' => 'USD',
'value' => $total
],
'shipping' => [
'currency_code' => 'USD',
'value' => $total > 200 ? '0.00' : '8.00',
]
]
]
]
]
]);
,然后像这样将其传递给PayPal的JS SDK:
paypal.Buttons({
createOrder: (data, actions) => {
return actions.order.create(<?= $order; ?>);
},
onApprove: async (data, actions) => {
const authorization = await actions.order.authorize()
const authorizationId = authorization.purchase_units[0].payments.authorizations[0].id
const response = await fetch('<?= base_url('cart/process'); ?>', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
authorizationId
})
})
let responseText = await response.text();
$('.site-content').html(responseText);
},
onCancel: function(data, actions) {
alert("Order cancelled successfully!");
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
创建的订单的意图是授权
有人可以帮助我吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ ordersponse
包含运输地址:print_r($ ordersponse-&gt;结果
-

var_dump();
当项目离主要一个太远时,它将“ ...”:因为这是我发现的唯一
address> array/object
narray/object在$ unerizationResponse
中,但仅包含country_code
。The
$orderResponse
contains the shipping address :print_r($orderResponse->result->purchase_units[0]->shipping->address);
I was using

var_dump();
which when the item is too far from the main one puts "..." :Because of that the only
address
array/object I found was in the$authorizationResponse
but it would of contain only thecountry_code
.