Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devuri/c9122b317eeaa2615acb0aec46c100d4 to your computer and use it in GitHub Desktop.
Save devuri/c9122b317eeaa2615acb0aec46c100d4 to your computer and use it in GitHub Desktop.
PayPal Rest API - Simple Transaction
This example uses the Rest API PHP SDK of Paypal. To get it, just use composer.
{
"require": {
"paypal/rest-api-sdk-php": "*",
}
}
<?php
// paypal/rest-api-sdk-php required
require_once('vendor/autoload.php');
// You need to create a Rest App to get credentials
// URL: https://developer.paypal.com/webapps/developer/applications/myapps
$apiContext = new \PayPal\Rest\ApiContext(new \PayPal\Auth\OAuthTokenCredential(
'AXr4rtLcA1CekAhwlIvdH4jZfSi_Mo1rF-llhnHYA-30GvO726NhIdFKYNIdgCIwIzJuScljsTPgkslX',
'Ey1UxSC2-e-l_GXe6kNj1PyelczBtY2TCAiA91phIn3RJlwTayf68qtSkDS9JHNmjZLXCkiKrY9o_dFq'
));
<?php
// paypal/rest-api-sdk-php required
require_once('vendor/autoload.php');
require_once('config.php');
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setCurrency('EUR');
$amount->setTotal(4.95);
$transaction = new \PayPal\Api\Transaction();
$transaction->setDescription('Test Transaction');
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl('http://domain.com/step2.php?success=1');
$redirectUrls->setCancelUrl('http://domain.com/step2.php?success=0');
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$payment->create($apiContext);
$appovalLink = $payment->getApprovalLink();
// We request in this file a approvalLink from Paypal. After that we need to redirect the customer to this link.
echo "<a href='$appovalLink'>$appovalLink</a>";
<?php
// paypal/rest-api-sdk-php required
require_once('vendor/autoload.php');
require_once('config.php');
$payment = \PayPal\Api\Payment::get($_GET['paymentId'], $apiContext);
$execution = new \PayPal\Api\PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
$payment->execute($execution, $apiContext);
var_dump($payment->getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment