如何将JSON数据发布到Paypal Adaptive

时间:2012-06-19 10:33:01

标签: php curl paypal

我正在尝试使用curl的Paypal Adaptive API。

Docs告诉我可以用JSON格式发送参数,但我总是遇到Invalid Request (580001)错误。

这是我正在做的要求:
接头

[X-PAYPAL-SECURITY-USERID] => XXXXXX
[X-PAYPAL-SECURITY-PASSWORD] => XXXXXX
[X-PAYPAL-SECURITY-SIGNATURE] => XXXXXX
[X-PAYPAL-REQUEST-DATA-FORMAT] => JSON
[X-PAYPAL-RESPONSE-DATA-FORMAT] => JSON
[X-PAYPAL-APPLICATION-ID] => APP-80W284485P519543T   //APP-ID for Sandbox

这是我建立的json数据:

{"endingDate":"2012-06-11T12:20:02+00:00",
 "startingDate":"2012-06-20T12:20:02+00:00",
 "maxTotalAmountOfAllPayments":"1000.00",
 "currencyCode":"EUR",
 "cancelUrl":"http:\/\/localhost\/xx\/pledge?id=221&step=fail",
 "returnUrl":"http:\/\/localhost\/xx\/pledge?id=221&step=done",
 "pinType":"NOT_REQUIRED",
 "requestEnvelope":{"detailLevel":"ReturnAll","errorLanguage":"en_US"},
 "clientDetails":[]
}

我想我没有正确发送JSON数据。 我正在设置这样的JSON数据(使用 curl ):

curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $json_data );

这是发送json数据的正确方法吗?

1 个答案:

答案 0 :(得分:3)

好的,我找到了解决方案:

我很遗憾在标题和Content-Type中指定Content-Length 所以结果是:

$json_data = json_encode( $json_fields );

$this->headers = array(
// Authentication
'X-PAYPAL-SECURITY-USERID'  => $this->_username,
'X-PAYPAL-SECURITY-PASSWORD'    => $this->_password,
'X-PAYPAL-SECURITY-SIGNATURE'   => $this->_signature,

// Data format
'X-PAYPAL-REQUEST-DATA-FORMAT'  => 'JSON',
'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'JSON',

// Application and Device identification
'X-PAYPAL-APPLICATION-ID'   => $this->_app_id,
'X-PAYPAL-DEVICE-IPADDRESS' => $this->_client_ip,
'Content-Type'          => 'application/json',
    'Content-Length'                => strlen( $json_data )
  );