-
Notifications
You must be signed in to change notification settings - Fork 103
File based Configurations
Now as we are able to make first call, add configuration, and found different ways to add data to object, this page explains if we could add configurations differently.
In PayPal SDK, you can also create a .ini configuration file, and use it to pass configurations to our SDK.
First, create a file called third.php
in your project root directory. You could alternatively copy the completed file here : third.php and sdk_config.ini
-
Autoload the SDK Package. This will include all the files and classes to your autoloader. Please note, If your downloaded our SDK using composer, replace
PayPal-PHP-SDK
withvendor
. This applies for all sample code in our SDK.// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
-
Now, instead of creating an instance of $apiContext like we did while creating first call, we will define a constant
PP_CONFIG_PATH
pointing to the directory where we will createsdk_config.ini
. Replace DIR with another location as desired. Thesdk_config.ini
file should not be publicly accessible, so it is advised to put the file in a non-public directory (e.g. not web accessible and not accessible by other non-permitted users on the system).// 2. Define PP_CONFIG_PATH directory if(!defined("PP_CONFIG_PATH")) { define("PP_CONFIG_PATH", __DIR__); }
-
We will repeat step 3 and 4 as before to create a payment object and make create call.
// After Step 2 $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);
-
Make a Create Call and Print the Card
// After Step 3 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(); }
-
Just before executing the script, we need to create
sdk_config.ini
. Please download the file in your project root directory. -
Run
php -f third.php
from command line. This will successfully create a payment resource. The output would look similar to this:> php -f third.php { "intent": "sale", "payer": { "payment_method": "paypal" }, "transactions": [ { "amount": { "total": "1.00", "currency": "USD" }, "related_resources": [] } ], "redirect_urls": { "return_url": "https://example.com/your_redirect_url.html", "cancel_url": "https://example.com/your_cancel_url.html" }, "id": "PAY-3MC96102SY030652JLHXXPMA", "state": "created", "create_time": "2017-10-24T17:26:07Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-3MC96102SY030652JLHXXPMA", "rel": "self", "method": "GET" }, { "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1NT485541R0509947", "rel": "approval_url", "method": "REDIRECT" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-3MC96102SY030652JLHXXPMA/execute", "rel": "execute", "method": "POST" } ] } Redirect user to approval_url: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1NT485541R0509947
Basically, we substituted apiContext object, with sdk_config.ini
. This allows you to provide your configurations by file based .ini file. The most important benefit to this is it eliminates the need to create an apiContext object, and pass it on for each call.
Getting Started
Using Our SDK
Configurations
Extras
External Links