Forked from a-r-m-i-n/ PayPal Rest API - Simple Transaction
Created
April 19, 2019 15:17
-
-
Save devuri/c9122b317eeaa2615acb0aec46c100d4 to your computer and use it in GitHub Desktop.
PayPal Rest API - Simple Transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This example uses the Rest API PHP SDK of Paypal. To get it, just use composer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"paypal/rest-api-sdk-php": "*", | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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