PHP |贝宝休息 API | java.lang.NullPointerException

Posted

技术标签:

【中文标题】PHP |贝宝休息 API | java.lang.NullPointerException【英文标题】:PHP | Paypal Rest API | java.lang.NullPointerException 【发布时间】:2017-02-01 00:50:48 【问题描述】:

我正在使用 Express Checkout REST API 在 Paypal 上付款。我在 php 中使用 CURL 成功检索了我的令牌。但是,实际付款的下一部分不起作用。

我收到以下错误:

java.lang.NullPointerException(我相信这个错误来自 paypal?)

我尝试过查看代码,但我不明白我做错了什么,因为它都是简单的结构。

我的代码:

$payment_result_json = array(
                       "intent" => "sale", 
                       "redirect_urls" => array("return_url" => "http://paypal/paypal_return.php", "cancel_url" => "http://paypal/paypal_cancel.php"),
                       "payer" => array("payment_method" => "paypal"),
                       "transactions" => array(

                            "amount" => array("amount" => array(
                                "total" => "1", "currency" => "USD", "details" => array(
                                    "subtotal" => "1", "shipping" => "0", "tax" => "1", "shipping_discount" => "0"
                                    )
                            )),

                            "item_list" => array("quantity" => "1", "name" => "Item 1", "price" => "1", "currency" => "USD", "description" => "item1 desc","tax" => "1"),
                            "description" => "Tutoring lesson"
                       ));

$post_headers = array("Authorization: Bearer $retreived_access_token");

$ch = curl_init("https://api.sandbox.paypal.com/v1/payments/payment");

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $post_headers);

while(empty($payment_result))
    $payment_result = curl_exec($ch);


if(empty($payment_result))
    print curl_error($ch);
else
    print $payment_result;


curl_close($ch);

$payment_result_json = json_decode($payment_result);

print_r($payment_result_json);

【问题讨论】:

【参考方案1】:

我昨天遇到了同样的问题。我解决了它添加这个标题: $post_headers = array("Authorization: Bearer $retreived_access_token", "Content-Type: application/json");

希望对你有帮助

【讨论】:

以上是关于PHP |贝宝休息 API | java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法设置参考交易贝宝的休息 API

Node.js 贝宝休息 sdk

使用休息的贝宝交易

贝宝休息访问令牌调用的主体是啥格式和内容类型?

PayPal API 休息和商家电子邮件帐户

必填字段缺失。 PHP 贝宝 REST API