I have an error using the PayPal REST API but I can't find a solution anywhere. I'm trying to send the user to PayPal to pay for the specified items in a session array. I have tried changing the payment method, the intent and the URLs.
$this->setupCurl();
$this->setupPayPal();
$this->payer = new Payer();
$this->details = array();
$this->amount = array();
$this->transaction = array();
$this->redirectUrls = new RedirectUrls();
$this->payment = new Payment();
$this->payer->setPaymentMethod('paypal'); // TODO paypal . credit_card
foreach ($cart as $id => $item) {
$this->details[$id] = new Details();
$this->amount[$id] = new Amount();
$this->transaction[$id] = new Transaction();
$this->details[$id]->setShipping('0.00')
->setTax('0.00')
->setSubtotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2)); // TODO
$this->amount[$id]->setCurrency('AUD')
->setTotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2))
->setDetails($this->details[$id]);
$this->transaction[$id]->setAmount($this->amount[$id])
->setDescription($this->products[$id]['name']); // TODO
}
$this->redirectUrls->setReturnUrl('https://warsentech.com/pay?approved=true')
->setCancelUrl('https://warsentech.com/pay?approved=false');
$this->payment->setIntent('authorize') // TODO sale . buy
->setPayer($this->payer)
->setRedirectUrls($this->redirectUrls)
->setTransactions($this->transaction); // TODO - add transactions into array from the cart items
try {
$this->payment->create($this->paypal);
$hash = md5($this->payment->getId());
$_SESSION['paypal_hash'] = $hash;
if ($this->databaseConnection()) {
$result = $this->db_connection->prepare('INSERT INTO transactions_paypal (user_id, payment_id, hash, complete) VALUES (:user_id, :payment_id, :hash, 0)');
$result->execute([
'user_id' => $_SESSION['user_id'],
'payment_id' => $this->payment->getId(),
'hash' => $hash
]);
}
} catch (Exception $e) {
die(paypalError($e));
}
foreach ($this->payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
}
}
Full error message:
exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:177 Stack trace: #0 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"auth...') #1 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"auth...', NULL) #2 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php(424): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"auth...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 /var/www/html/classes/PayPal.php(168): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 /var/www/html/classes/PayPal.php(50): PayPal->pay(Array) #5 /var/www/html/checkout.php(39): PayPal->__construct() #6 {main}