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

Making First Call

Javier Alfonso edited this page Apr 17, 2017 · 14 revisions

Once you have completed the Installation, we could make the first call. 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. You could alternatively copy the completed file here : first.php

  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.
<?php
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
// Used for composer based installation
require __DIR__  . '/vendor/autoload.php';
// Use below for direct download installation
// require __DIR__  . '/PayPal-PHP-SDK/autoload.php';
  1. Provide the ClientId and Secret. Optionally, Replace the given one with your app clientId, and Secret
// 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");
  1. Make a Create Call and Print the Card
// After Step 3
try {
        $creditCard->create($apiContext);
        echo $creditCard;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
        // This will print the detailed information on the exception. 
        //REALLY HELPFUL FOR DEBUGGING
        echo $ex->getData();
}
  1. 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:
> 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"
        }
    ]
}
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

Next Step