PayPal 订单 API:使用 SET_PROVIDED_ADDRESS 时出现 MISSING_SHIPPING_ADDRESS 错误

问题描述 投票:0回答:3

我正在编写 WP 插件,将 paypal 按钮添加到页面。有一个 js 文件(来自 paypal 网站的示例):

    paypal.Buttons({
            createOrder() {   
                return fetch( dir + '/includes/api/create-paypal-order.php', {           
                method: "POST",
                headers: {
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  cart: [
                    {
                      sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
                      quantity: "YOUR_PRODUCT_QUANTITY",
                    },
                  ],
                }),
                referrerPolicy: "origin-when-cross-origin" 
              })
              .then((response) => response.json())
              .then((order) => order.id)
              .then((order) => {console.log(order); return order;} );
            },
            onApprove(order) {  
             ...
            },
          }).render('#paypal-button-container');   

还有 php 文件(create-paypal-order.php):

function create_order()
{
    $ch = false;
    $client_id = "Ac9...Fq";    
    $client_secret = "ECn...fH";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
    curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . $client_secret);

    $headers = array();
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    $json = json_decode($result);

    $access_token = $json->access_token;
    print_r( $access_token );
    curl_close($ch);

    $ch = false;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{
        "intent": "CAPTURE",
        "purchase_units": 
            [{    
                "reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",    
                "amount": 
                    {        
                    "currency_code": "USD",      
                    "value": "10.00"      
                    }    
                }  
            ], 
        "payment_source": {    
            "paypal": 
                {      
                    "experience_context": 
                    {        
                    "payment_method_preference": "IMMEDIATE_PAYMENT_REQUIRED",        
                    "payment_method_selected": "PAYPAL",        
                    "brand_name": "EXAMPLE INC",        
                    "locale": "en-US",        
                    "landing_page": "LOGIN",
                    "shipping_preference": "SET_PROVIDED_ADDRESS",
                    "user_action": "PAY_NOW",       
                    "return_url": "https://example.com/returnUrl",        
                    "cancel_url": "https://example.com/cancelUrl"      
                    }    
                }  
        }
    }');
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Paypal-Request-Id: 7b92603e-77ed-4896-8e78-5dea2050476a';
    $headers[] = 'Authorization: Bearer ' . $access_token;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    curl_close($ch);
    $json = json_encode($result);
    print_r($result);
    return $result;
}
create_order();

当我单击 paypal 按钮时,它尝试打开带有表单的模式窗口,但它立即消失。
我查看了浏览器开发工具“网络”选项卡,观察单击按钮时路由“/includes/api/create-paypal-order.php”返回的内容,并看到错误“UNPROCESSABLE_ENTITY”:
{“名称”:“UNPROCESSABLE_ENTITY”,“详细信息”:[{“字段”:“/purchase_units/@reference_id=='d9f80740-38f0-11e8-b467-0ed5f89f718b'/送货/地址”,“问题”:“MISSING_SHIPPING_ADDRESS ","description":"当

shipping_preference=SET_PROVIDED_ADDRESS
时需要送货地址。"}],"message":"请求的操作无法执行、语义不正确或业务验证失败。","debug_id":"c41ec50f69e7d ","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-MISSING_SHIPPING_ADDRESS","rel":"information_link","method":"获取“}]}

我正在测试这个用过的贝宝沙箱。我在 paypal 沙箱中制作了应用程序,并使用来自该站点的客户端 ID 和客户端密钥。我的插件位于本地主机(我使用 XAMPP)。

我不知道为什么会出现这个错误以及如何修复它。

php paypal
3个回答
0
投票

如果您使用 WordPress 插件编辑结帐字段,并且从插件中禁用它们,则必须先修改它才能使其余部分正常工作,该字段需要从“必需”更改为“不需要”。然后您只需删除支票即可。


0
投票

设定值:

"shipping_preference": "NO_SHIPPING"

来源:Paypal API 文档https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context


0
投票

哇,终于找到答案了。

如果您销售数字下载。

别忘了标记“虚拟”框enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.