MALFORMED_REQUEST尝试与paypal沟通

时间:2014-11-27 21:57:45

标签: php curl

我将第一次API调用发送到PayPal时遇到了问题。

我希望它只是简单的语法,我错了。

我得到的错误:

"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

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

    $sAccessToken = $json->access_token;
    $sTokenType = $json->token_type;
    $sAppID = $json->app_id;
    print_r($json);
}
curl_close($ch);

$ch2 = curl_init();

//curl -v https://api.sandbox.paypal.com/v1/payments/payment 
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");

//-H "Content-Type:application/json" \
//-H "Authorization: Bearer <Access-Token>" \
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
    "Content-Type:application/json",
    "Authorization: Bearer ".$sAccessToken,
));



$payer = array(
    'payment_method'        => 'credit_card',
    'funding_instruments'   => array(
                    'credit_card'       => array(

                            'number'            => "4417119669820331",
                            "type"              => "visa",
                            "expire_month"      => "11",
                            "expire_year"       => "2018",
                            "cvv2"              => "874",
                            "first_name"        => "Betsy",
                            "last_name"         => "Buyer",
                            "billing_address"   => array(
                                    "line1"             => "111 First Street",
                                    "city"              => "Saratoga",
                                    "state"             => "CA",
                                    "postal_code"       => "95070",
                                    "country_code"      => "US"
                            ),
                    ),
            ),
);

$params = array(
    'intent'    => 'sale',
    'payer'     => $payer,
    'transactions'  => array(array(
                "amount"         =>"7.47",
                "currency"      =>"USD",
                "details"       => array(
                                    "subtotal"=>"7.41",
                                    "tax"=>"0.03",
                                    "shipping"=>"0.03"
                ),
                "description" => "This is the payment transaction description."
    )
));

curl_setopt($ch2, CURLOPT_POST, true );
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true );

$response2 = curl_exec( $ch2 );
$errrors2 = curl_error($ch2);

print_r($response2." / ");
print_r($errrors2);
curl_close($ch2);

0 个答案:

没有答案