Skip to content

Instantly share code, notes, and snippets.

@jaypatel512
Last active July 8, 2021 07:01
Show Gist options
  • Save jaypatel512/3861355780aedd694b89 to your computer and use it in GitHub Desktop.
Save jaypatel512/3861355780aedd694b89 to your computer and use it in GitHub Desktop.
<?php
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
// 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
// https://developer.paypal.com/webapps/developer/applications/myapps
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS', // ClientID
'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL' // ClientSecret
)
);
// 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 = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
// 4. Make a Create Call and print the values
try {
$payment->create($apiContext);
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();
}
@PhilippeI
Copy link

PhilippeI commented Jan 21, 2019

Hi , thanks for this code,
I m new on paypal .. i try to implement the server rest solution on web site.
Question : the '\PayPal\Auth\OAuthTokenCredential(' on Sandbox context is for the seller .. ? [email protected] ?
Or the Buyer credential .. in this case your script is an 'Pay Button' emulation .. isn't it ?

@xicom
Copy link

xicom commented Aug 21, 2019

Hi,
When I am using the api with live credentials it gives me error like "Access token does not have required scope". Is there any setting that I need to do in merchant account?
My code is below -
$apiContext = new \PayPal\Rest\ApiContext(new \PayPal\Auth\OAuthTokenCredential('client id', 'secreat key'));
$apiContext->setConfig(
array(
'mode' => 'live',
)
);

Please help
Thanks

@vonuyvico
Copy link

h

@wgrnihao
Copy link

wgrnihao commented Jul 8, 2021

666

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