Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

File based Configurations

Nguimjeu edited this page Jan 28, 2015 · 16 revisions

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.

Instructions

First, create a file called third.php in your project root directory.

  1. 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 with vendor. 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';

2. Now, instead of creating an instance of $apiContext we did while [creating first call](Making-First-Call), we will define a constant `PP_CONFIG_PATH` pointing to the directory where we will create `sdk_config.ini`. Replace __DIR__ with another location as desired.

    ```php
// 2. Define PP_CONFIG_PATH directory
if(!defined("PP_CONFIG_PATH")) {
    define("PP_CONFIG_PATH", __DIR__);
}
    ```

3. We will repeat [ step 3 and 4 ](Making-First-Call#instructions) as before to create a credit card object and make create call.
    ```php
// After Step 2
$creditCard = new \PayPal\Api\CreditCard();
$creditCard->setType("visa")
    ->setNumber("4417119669820331")
    ->setExpireMonth("11")
    ->setExpireYear("2019")
    ->setCvv2("012")
    ->setFirstName("Joe")
    ->setLastName("Shopper");
  1. Make a Create Call and Print the Card

// After Step 3 try { $creditCard->create($apiContext); echo $creditCard; } catch (\PayPal\Exception\PayPalConnectionException $ex) { echo $ex; }


5. Just before executing the script, we need to create [`sdk_config.ini`](https://gist.github.com/jaypatel512/0af613871ea499985022). Please [download the file](https://gist.github.com/jaypatel512/0af613871ea499985022) in your project root directory.

6. Run `php -f third.php` from command line. This will successfully create a credit card in your vault. The output would look similar to this:
    ```sh
> php -f third.php
{
    "type": "visa",
    "number": "xxxxxxxxxxxx0331",
    "expire_month": "11",
    "expire_year": "2019",
    "cvv2": "012",
    "first_name": "Joe",
    "last_name": "Shopper",
    "id": "CARD-53D88620CF909614MKSYZJ5Q",
    "state": "ok",
    "valid_until": "2018-01-09T00:00:00Z",
    "create_time": "2015-01-10T21:09:10Z",
    "update_time": "2015-01-10T21:09:10Z",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-53D88620CF909614MKSYZJ5Q",
            "rel": "self",
            "method": "GET"
        },
        {
            "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-53D88620CF909614MKSYZJ5Q",
            "rel": "delete",
            "method": "DELETE"
        },
        {
            "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-53D88620CF909614MKSYZJ5Q",
            "rel": "patch",
            "method": "PATCH"
        }
    ]
}
    ```

7. You can view the completed file here : [[ third.php | https://gist.github.com/jaypatel512/94e626b684f6fefa5078 ]] and [sdk_config.ini](https://gist.github.com/jaypatel512/0af613871ea499985022)

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 crete an apiContext object, and pass it on for each call.