0
votes

My paypal sdk version is ^1.13

I'm having errors in my paypal implementation. I followed this tutorial https://www.youtube.com/watch?v=BD1dOWIABe0

<?php
require 'vendor/autoload.php';
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Api\Details;
use PayPal\Rest\ApiContext;
Use PayPal\Auth\OAuthTokenCredential;



if(!isset($_POST['product'], $_POST['price'])){
die();
}

$clientId = 'client-id';
$clientSecret = 'client-secret';

$apiContext = new ApiContext(
new OAuthTokenCredential(
$clientId,
$clientSecret
)
);

$product = $_POST['product'];
$price = $_POST['price'];
$tax = 2.00;

$total = $price + $tax;

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

$item = new Item();
$item->setName($product)
->setCurrency('USD')
->setQuantity(1)
->setPrice($price);

$itemList = new ItemList();
$itemList->setItems([$item]);

$details = new Details();
$details->setShipping($tax)
->setSubtotal($price)
->setTax(2.00);

$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($product);

$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription('dsds')
    ->setInvoiceNumber(uniqid());

$baseUrl = "http://www.localhost/xc";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/pay.php?success=true")
->setCancelUrl("$baseUrl/pay.php?success=false");

$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);


try{

$payment->create($apiContext);

}
catch(PayPal\Exception\PayPalConnectionException $ex){
echo $ex;
die($ex);
}

echo $approvalUral = $payment->getApprovalLink(); 

I've tried to search about the error but I can't seem to find an answer so I need some help. What have I done wrong ? Do I need to downgrade ? because the video in tutorial made 2 years ago so I think I need to downgrade but is it possible ?

exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:202
Stack trace:
#0 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(78): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...')
#1 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(104): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL)
#2 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(577): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL)
#3 C:\xampp\htdocs\ladea\checkout.php(78): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext))
#4 {main}exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalHttpConnection.php:202 Stack trace: #0 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(78): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(104): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL) #2 C:\xampp\htdocs\ladea\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(577): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 C:\xampp\htdocs\ladea\checkout.php(78): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 {main}

Thank you and Regards.

2
Rather than following a 2 year old video tutorial, why not refer to the actual documentation? - Phil
Well I actually refer to it. But still It didn't work. I also copied the actual example documentation codes. - Rongiro

2 Answers

0
votes

Remove your *.paypal.com cookies. This an unfortunate bug in the PayPal system, where the cookie gets too large and the server errors out on it. You can avoid the problem by using two different browsers, e.g. IE for PayPal Live and FF for the PayPal Sandbox. Check with incognito mode also.

0
votes

I have provided this information in your GitHub issue but will provide an answer here to help.

I updated the exception output of your code sample with the information in the wiki which gave some additional information, the 400 was a Malformed Request https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing.

$details = new Details();
$details->setShipping($tax)
->setSubtotal($price)
->setTax(2.00);

$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($product);

If your total is tax and price of the item, then don't set shipping. Also amount->setDetails should be details.

$details = new Details();
$details->setSubtotal($price)
->setTax(2.00);

$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($details);