Skip to content

Instantly share code, notes, and snippets.

@jaypatel512
Last active September 5, 2022 03:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaypatel512/94e626b684f6fefa5078 to your computer and use it in GitHub Desktop.
Save jaypatel512/94e626b684f6fefa5078 to your computer and use it in GitHub Desktop.
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
// 2. Define PP_CONFIG_PATH directory
if(!defined("PP_CONFIG_PATH")) {
define("PP_CONFIG_PATH", __DIR__);
}
// 3. Lets try to create a Payment
// https://developer.paypal.com/docs/api/payments/#payment_create
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal('1.00');
$amount->setCurrency('USD');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/your_redirect_url.html")
->setCancelUrl("https://example.com/your_cancel_url.html");
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
// 4. Make a Create Call and print the values
try {
$payment->create();
echo $payment;
echo "\n\nRedirect user to approval_url: " . $payment->getApprovalLink() . "\n";
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// This will print the detailed information on the exception.
//REALLY HELPFUL FOR DEBUGGING
echo $ex->getData();
}
@kdrezo
Copy link

kdrezo commented Dec 6, 2016

hello sorry for my poor english
but I think
$creditCard->create($apiContext);
must be,
$creditCard->create();

@jaypatel512
Copy link
Author

You are right @kdrezo. Will update that.

@seucolega
Copy link

I believe you have missed $payment = new \PayPal\Api\Payment(); before $payment->setIntent('sale') (line 25).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment