2014-04-25 11 views
8

Tôi đang sử dụng API phần còn lại paypal, nhưng tôi gặp lỗi. Đây là mã của tôi:0 - Có mã phản hồi Http 400 khi truy cập https://api.sandbox.paypal.com/v1/payments/payment

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; 
} 

Tất cả mọi thứ hoạt động tốt cho đến khi tôi gọi $payment->create($apiContext);

Sau đó nó cho thấy lỗi này:

0 - Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment .

+0

Tôi gặp vấn đề tương tự, có ai đó đã tìm ra điều này không? –

Trả lời

10

Tôi nghĩ lý do cho 400 là SUBTOTAL + thuế + vận chuyển không cộng thêm vào tổng số. Bạn sẽ có thể kiểm tra lý do trong phản hồi bạn nhận được, nó sẽ chứa một mảng lỗi.

thuế + SUBTOTAL + vận chuyển = tổng

57,41 + 0,06 + 0,06! = 5.47

2

Đã cùng một vấn đề. Vấn đề là một số space trong thông số tôi đã chuyển qua trong số returnurl.

Make sure your $returnUrl and $cancelUrl are urlencoded if you are passing any special characters.

14

Nếu bạn quấn cuộc gọi của bạn trong một khối try/catch và bắt PPConnectionException, bạn có thể kiểm tra các dữ liệu để xem chính xác những gì lỗi là:

// ... 
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; 
} 
+6

Điều này đã giúp mặc dù không gian tên ngoại lệ là: 'PayPal \ Exception \ PayPalConnectionException' – Iwazaru

+0

Cả câu trả lời và nhận xét của @ Iwazaru đều rất hữu ích. Không gian tên ngoại lệ cho bản phát hành 1.6.2 của PHP PHP SDK giờ đây là 'PayPal \ Exception \ PayPalConnectionException' –

0

Bạn nên kiểm tra lại nếu tổng phụ và tổng số là chính xác. Trong trường hợp của tôi, tôi đã sử dụng sandbox, và tôi đã không chú ý tổng số và tổng phụ không chính xác. Một số xu đã tạo ra sự khác biệt và tạo ra lỗi, chỉ cần sửa các giá trị và nó hoạt động đúng.

Các vấn đề liên quan