传入的JSON请求与API请求不匹配

15

在创建支付时,我遇到了一个错误,错误信息如下:

-url: "https://api.sandbox.paypal.com/v1/payments/payment" -data: "{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/doc ▶" #message: "Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment."

我尝试通过将其添加到列表中,然后将其添加到交易中来创建付款。

    // type of paypal payment (redirect)
    $payer = PayPal::Payer();
    $payer->setPaymentMethod('paypal');

    // iniate the item
    $item = Paypal::item();

    // iniate the ammount
    $amount = PayPal:: Amount();

    switch($type)
    {            
        case 'job':
            $item
                ->setName('Job advertisement on '. Config('app.name'))
                ->setDescription('Job advertisement on '. Config('app.name'))
                ->setCurrency('GBP')
                ->setQuantity(1)
                ->setTax(0.2)
                ->setPrice(Config('settings.job_price'));

            $details = Paypal::Details();
            $details->setShipping(1.2)
                ->setTax(1.3)
                ->setSubtotal(17.50);

            $amount->setCurrency('GBP')
                ->setDetails($details)
                ->setTotal(Config('settings.job_price'));
        break;
        case 'course':   
            $item
                ->setName('Course advertisement on '. Config('app.name'))
                ->setDescription('Course advertisement on '. Config('app.name'))
                ->setCurrency('GBP')
                ->setQuantity(1)
                ->setTax(0.2)
                ->setPrice(Config('settings.course_price'));

            $details = Paypal::Details();
            $details->setShipping(1.2)
                ->setTax(1.3)
                ->setSubtotal(17.50);

            $amount->setCurrency('GBP')
                ->setDetails($details)
                ->setTotal(Config('settings.course_price'));
        break;
    }

    // geerate the item list
    $itemList = Paypal::itemList();
    $itemList->setItems($item);

    // now create the transactions
    $transaction = Paypal::transaction();
    $transaction
        ->setAmount($amount)
        ->setItemList($itemList)
        ->setDescription("Payment description")
        ->setInvoiceNumber(uniqid());

    // setup the redirect uri's'
    $redirectUrls = PayPal:: RedirectUrls();
    $redirectUrls
        ->setReturnUrl(route('payment.complete'))
        ->setCancelUrl(route('payment.cancelled'));

    // set up the person's information
    $payment = PayPal::Payment();
    $payment
        ->setIntent('authorize')
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions([$transaction]);
    print_r($payment);
    try{
        dd($payment->create($this->_apiContext));
        $response = $payment->create($this->_apiContext);
    } catch (\PayPal\Exception\PayPalConnectionException $ex) {
       dd($ex);
    } catch (Exception $ex) {
        die($ex);
    }
    $redirectUrl = $response->links[1]->href;

    // redirect to paypal for payment
    return Redirect::to( $redirectUrl );

发送的数据如下:

PayPal\Api\Payment Object ( 
    [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
        [intent] => authorize [payer] => PayPal\Api\Payer Object ( 
            [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                [payment_method] => paypal 
            ) 
        ) 
        [redirect_urls] => PayPal\Api\RedirectUrls Object ( 
            [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                [return_url] => http://localhost/payment/payment-done 
                [cancel_url] => http://localhost/payment/payment-cancelled 
            ) 
        ) 
        [transactions] => Array ( 
            [0] => PayPal\Api\Transaction Object ( 
                [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                    [amount] => PayPal\Api\Amount Object ( 
                        [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                            [currency] => GBP 
                            [details] => PayPal\Api\Details Object ( 
                                [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                                    [shipping] => 1.20 
                                    [tax] => 1.30 
                                    [subtotal] => 17.50 
                                ) 
                            ) 
                            [total] => 125 
                        ) 
                    ) 
                    [item_list] => PayPal\Api\ItemList Object ( 
                        [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                            [items] => PayPal\Api\Item Object ( 
                                [_propMap:PayPal\Common\PayPalModel:private] => Array ( 
                                    [name] => Job advertisement on Laravel 
                                    [description] => Job advertisement on Laravel 
                                    [currency] => GBP 
                                    [quantity] => 1 
                                    [tax] => 0.20 
                                    [price] => 125 
                                ) 
                            ) 
                        ) 
                    ) 
                    [description] => Payment description 
                    [invoice_number] => 599cd8d03b626 
                ) 
            ) 
        ) 
    ) 
)

我该如何修复这个问题?


你能展示一下你生成的 JSON 字符串吗? - Muhammad Akber Khan
4个回答

7

这已经足够了

$payment->setIntent('authorize')

相较于

$payment->setIntent('sale')

并且消除执行

 $execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext);

然后,在此之后。
$payment->create

我使用了来自 "href" 的链接。
$payment->links

要进行重定向。一切都很完美。

传入的JSON请求与API请求不匹配


4

请将下面的代码更改为:

$transaction->setAmount(config('settings.job_price'))

即可。

$amount =   new Amount();
$amount->setCurrency('USD')  // set to your currency
        ->setTotal($total);

$transaction->setAmount($amount)

我认为,setAmount方法的参数必须是Amount类的成员。

0

在我的案例中,我正在使用Laravel。 我从另一个函数调用此脚本,然后它显示相同的错误。 当我直接调用此函数的POST方法时,问题就解决了。


0

了解更多有关此问题的信息;

请访问Paypal Git,该问题已在此处报告并提供了修补程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接