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

File based Configurations

ygottschalk edited this page Apr 15, 2017 · 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. You could alternatively copy the completed file here : third.php and sdk_config.ini

  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, 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. The sdk_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__);
    }
  3. We will repeat step 3 and 4 as before to create a credit card object and make create call.

    // After Step 2
    $creditCard = new \PayPal\Api\CreditCard();
    $creditCard->setType("visa")
        ->setNumber("4417119669820331")
        ->setExpireMonth("11")
        ->setExpireYear("2019")
        ->setCvv2("012")
        ->setFirstName("Joe")
        ->setLastName("Shopper");
  4. Make a Create Call and Print the Card

    // After Step 3
    try {
        //ApiContext is not required anymore as it get loaded from file
        $creditCard->create();
        echo $creditCard;
    }
    catch (\PayPal\Exception\PayPalConnectionException $ex) {
        echo $ex;
    }
  5. Just before executing the script, we need to create sdk_config.ini. Please download the file 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:

    > 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"
            }
        ]
    }

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.

Next Step