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

Making First Call

japatel edited this page Jan 10, 2015 · 14 revisions

To write an app that uses the SDK, we will create our first PHP script that will store a Credit Card in our Vault.

Instructions

First, Create file first.php in our project root location

  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. Provide the ClientId and Secret. Optionally, Replace the given one with [[your app clientId, and Secret | https://developer.paypal.com/webapps/developer/applications/myapps]]
    ```php
// After Step 1
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS',     // ClientID
        'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL'      // ClientSecret
    )
);
  1. Lets try to save a credit card to Vault using Vault API mentioned here

// 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
    ```php
// After Step 3
try {
    $creditCard->create($apiContext);
    echo $creditCard;
}
catch (\PayPal\Exception\PPConnectionException $ex) {
    echo $ex;
}

    ```

5. Run `php -f first.php` from command line. This will successfully create a credit card in your vault. The output would look similar to this:
    ```sh
> php -f first.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"
        }
    ]
}
    ```

6. You can view the completed file here : [[ first.php | https://gist.github.com/jaypatel512/3861355780aedd694b89 ]]

##### NOTE

* To reduce redundant code and minimize security risks, you could create a file `bootstrap.php` and move step 1 and 2 there, and just `include` bootstrap.php.
* All the calls made currently are defaulted to sandbox environment. Sandbox Environment replicates the exact behavior you get from our PayPal APIs, but here, it allows you to create dummy accounts and test your integration, before running your code on live PayPal APIs. For more information visit [[ Developer Portal | https://developer.paypal.com/webapps/developer/ ]]

## Next Step

* [[ Adding Configurations | Adding-Configurations ]]