Paypal单笔付款 - 'Json请求格式错误'

时间:2016-09-25 02:09:33

标签: php curl post paypal paypal-sandbox

我正在尝试使用Paypal Sandbox进行单笔付款。我得到的错误是:

  

'MALFORMED_REQUEST_ERROR' - 'Json请求格式错误。'

成功收到令牌后,我设置了单个付款详细信息数组并对json进行编码,然后使用curl发布,但没有运气。代码如下:

if ($token) {
    $ch = curl_init();

    $data = [
        'sender_batch_header' => [
            'email_subject' => "You have a payment",
            'sender_batch_id' => '184328423'
        ],
        'items' => [
            'recipient_type' => "EMAIL",
            'amount' => [
                'value' => 12.00,
                'currency' => "USD"
            ],
            'receiver' => 'test123test@hotmail.com',
            'note' => 'Hello World',
            'sender_item_id' => "123"
        ],
    ];


    $headers = [
        'Content-Type:application/json',
        'Authorization:Bearer ' . $token,
    ];



    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payouts?sync_mode=true");
    // curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $result = curl_exec($ch);

    if(empty($result))die("Error: No response.");
    else
    {
        $json = json_decode($result);
        print_r($json);
    }

    curl_close($ch);

}

1 个答案:

答案 0 :(得分:2)

我花了一些时间才注意到它。答案是:

$data = [
    'sender_batch_header' => [
        'email_subject' => "You have a payment",
        'sender_batch_id' => $randId

    ],
    'items' => [
        [
            'recipient_type' => "EMAIL",
            'amount' => [
                'value' => "12.00",
                'currency' => "USD"
            ],
            'receiver' => 'test123test@hotmail.com',
            'note' => 'Hello World',
            'sender_item_id' => "A123"
        ],
    ],
];