0 - 访问https://api.sandbox.paypal.com/v1/payments/payment时获得Http响应代码400

时间:2014-04-25 18:29:07

标签: paypal

我正在使用paypal rest API,但是我收到了一个错误。这是我的代码:

function makePaymentUsingPayPal($total, $currency, $paymentDesc, $returnUrl, $cancelUrl) {

    // set billing address
    $addr = new Address();
    $addr->setLine1('fh52 N Main ST');
    $addr->setCity('Johnstownfhf');
    $addr->setCountry_code('UK');
    $addr->setPostal_code('35345');
    $addr->setState('DF');

    // set credit card information
    $card = new CreditCard();
    $card->setNumber('4111111111111111');
    $card->setType('visa');
    $card->setExpire_month('12');
    $card->setExpire_year('2015');
    $card->setCvv2('123');
    $card->setFirst_name('dgdg');
    $card->setLast_name('dgdgdShopper');
    $card->setBilling_address($addr);

    $fi = new FundingInstrument();
    $fi->setCredit_card($card);

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

    //$payer = new Payer();
    //$payer->setPayment_method('credit_card');
    //$payer->setFunding_instruments(array($fi));

    // Specify the payment amount.        
    $amountDetails = new Details();
    $amountDetails->setSubtotal('57.41');
    $amountDetails->setTax('0.06');
    $amountDetails->setShipping('0.06');

    $amount = new Amount();
    $amount->setCurrency('USD');
    $amount->setTotal('5.47');
    $amount->setDetails($amountDetails);

    // ###Transaction
    // A transaction defines the contract of a
    // payment - what is the payment for and who
    // is fulfilling it. Transaction is created with
    // a `Payee` and `Amount` types
    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription('sdgdfg This is the payment transaction description.');

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl($returnUrl);
    $redirectUrls->setCancelUrl($cancelUrl);

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

    //print_r($payment);exit; 
    $payment->create($apiContext);

    // return $payment;
}

在我致电$payment->create($apiContext);

之前,一切正常

然后显示此错误:

  

0 - 访问https://api.sandbox.paypal.com/v1/payments/payment时获得Http响应代码400。

4 个答案:

答案 0 :(得分:15)

如果将调用包装在try / catch块中并捕获PPConnectionException,则可以检查数据以确切了解错误:

// ...
try {
    $response = $pp_payment->create();
} catch (PayPal\Exception\PPConnectionException $pce) {
    // Don't spit out errors or use "exit" like this in production code
    echo '<pre>';print_r(json_decode($pce->getData()));exit;
}

答案 1 :(得分:11)

我认为400的原因是你的subTotal + tax + shipping总计没有加起来。你应该能够在你得到的答案中检查原因,它应该包含一系列错误。

subTotal + tax + shipping = total

57.41 + 0.06 + 0.06!= 5.47

答案 2 :(得分:2)

有同样的问题。问题是我在 returnurl 中传递的参数中的space

  

确保 $ returnUrl $ cancelUrl urlencoded   如果你传递任何特殊字符。

答案 3 :(得分:0)

您应该仔细检查小计和总数是否正确。 在我的情况下,我使用沙盒,我没有注意总数和小计不正确。一些美分产生差异并输出错误,只修复了值并且它正常工作。