PayPal REST API 格式错误的 JSON 错误

Posted

技术标签:

【中文标题】PayPal REST API 格式错误的 JSON 错误【英文标题】:PayPal REST API Malformed JSON error 【发布时间】:2014-03-10 19:12:08 【问题描述】:

我正在尝试让“使用 PayPal 帐户付款”与 php 一起使用,但我不断收到响应“传入的 JSON 请求未映射到 API 请求”。 我已经检查了我使用 jsonlint 发送的 JSON,它是有效的 JSON。它似乎也与此付款类型的示例中发送的内容相匹配。

require __DIR__ . '/../bootstrap.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
session_start();

// ### Payer
$payer = new Payer();
$payer->setPaymentMethod("paypal");

// ### Itemized information
$item1 = new Item();
$item1->setName($item1Name)
    ->setCurrency($currency)
    ->setQuantity($item1Quantity)
    ->setPrice($item1Price);
$item2 = new Item();
$item2->setName($item2Name)
    ->setCurrency($currency)
    ->setQuantity($item2Quantity)
    ->setPrice($item2Price);

$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));

// ### Additional payment details
$details = new Details();
$details->setShipping($shipping)
    ->setTax($tax)
    ->setSubtotal($subtotal);

// ### Amount
$amount = new Amount();
$amount->setCurrency($currency)
    ->setTotal(number_format(($subtotal + ($shipping + $tax)), 2))
    ->setDetails($subtotal);

// ### Transaction
$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription("Payment description");

// ### Redirect urls
$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
    ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");

// ### Payment
$payment = new Payment();
$payment->setIntent("sale")
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions(array($transaction));

// ### Create Payment
$payment->create($apiContext);
 catch (PayPal\Exception\PPConnectionException $ex) 
    echo "Exception: " . $ex->getMessage() . PHP_EOL;
    var_dump($ex->getData());   
    exit(1);


// ### Get redirect url
foreach($payment->getLinks() as $link) 
    if($link->getRel() == 'approval_url') 
        $redirectUrl = $link->getHref();
        break;
    


// ### Redirect buyer to PayPal website
$_SESSION['paymentId'] = $payment->getId();
if(isset($redirectUrl)) 
    header("Location: $redirectUrl");
    exit;

PayPal.log 包含以下内容:

PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/oauth2/token
PayPal\Core\PPHttpConnection: Payload grant_type=client_credentials
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=********************;machine=x86_64;openssl=**********;curl=7.15.5)
PayPal\Core\PPHttpConnection: Adding header Authorization: Basic ***********************************
PayPal\Core\PPHttpConnection: Adding header Accept: */*
PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/payments/payment
PayPal\Core\PPHttpConnection: Payload "intent":"sale","payer":"payment_method":"paypal","redirect_urls":"return_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=true","cancel_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=false","transactions":["amount":"currency":"EUR","total":"554.00","details":"550.00","item_list":"items":["name":"Item 1 Name","currency":"EUR","quantity":50,"price":"7.00","name":"Item 2 Name","currency":"EUR","quantity":20,"price":"10.00"],"description":"Payment description"]
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=*******************;machine=x86_64;openssl=************;curl=7.15.5)
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer gSJ0P0foNQcWg3V76VjvSietNLejlF4-kfSFNkTcyCk
PayPal\Core\PPHttpConnection: Adding header PayPal-Request-Id: 773226502471885139220716063968

非常感谢任何帮助。

提前致谢。

【问题讨论】:

我已经想通了。我将一个数字传递给 Amount->SetDetails 而不是所需的对象。 // ### 金额 $amount = new Amount(); $amount->setCurrency($currency) ->setTotal(number_format(($subtotal + ($shipping + $tax)), 2)) ->setDetails($subtotal);帮助我弄清楚的一件事是将 PayPal 示例 (/sample/PayPal.log) 的日志与我自己尝试的日志进行比较。避免了灾难! 【参考方案1】:

我想通了。我将一个数字传递给 Amount->SetDetails 而不是所需的对象。

// ### Amount
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal(number_format(($subtotal + ($shipping + $tax)), 2))
->setDetails($subtotal);

帮助我弄清楚的一件事是将 PayPal 示例 (/sample/PayPal.log) 的日志与我自己尝试的日志进行比较。

避免了灾难!

【讨论】:

以上是关于PayPal REST API 格式错误的 JSON 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 paypal/rest-api-sdk-php 的 laravel paypal 集成错误

PayPal REST API - '发生内部服务错误'

PayPal REST API - 已退款交易错误

paypal rest api执行错误

Paypal REST API 沙盒 - SSL 连接错误

PayPal REST API - RATE LIMIT REACHED 错误