传入的JSON请求不会映射到API请求"

时间:2017-07-14 15:12:59

标签: paypal

我在尝试创建付款时遇到错误,我得到的错误是:

  

-url:" https://api.sandbox.paypal.com/v1/payments/payment" -数据:   " {"名称":" MALFORMED_REQUEST","消息":"传入的JSON请求不   映射到API   请求"" information_link":" https://developer.paypal.com/webapps/developer/doc   ▶" #message:"访问时获得Http响应代码400   https://api.sandbox.paypal.com/v1/payments/payment"

我正在尝试创建这样的付款,方法是将其添加到列表中,然后将其添加到事务

transform(array: any[]) {

    if (!array) return;      
    console.log(array.length);        
}

发送的数据如下:

    // 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 );

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:7)

这已经足够了

$payment->setIntent('authorize')

而不是

$payment->setIntent('sale')

并取消执行

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

然后,

之后
$payment->create

我使用了

中的“href”
$payment->links

进行重定向。一切都很顺利

Incoming JSON request does not map to API request

答案 1 :(得分:4)

更改以下代码

$交易 - > setAmount(配置( 'settings.job_price'))

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

$transaction->setAmount($amount)

我认为,setAmount的参数必须是Class Amount的成员。

答案 2 :(得分:0)

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

访问Paypal Git,报告此问题并提供补丁。

答案 3 :(得分:0)

在我的情况下,我使用laravel 我从另一个函数调用此脚本,然后显示相同的错误。 当我调用此函数直接发布方法。这是固定的。