Paypal credit card vault to store credit card details on paypal side.

@mit $ingh. January 03, 2015 Comments

Store the credit card details by using the PayPal api SDK

Click on below link to download the paypal SDK

Download PayPal PHP SDK

Process of using the paypal api


Include the autoload file to load the required class files.

require('library/PayPal-SDK/vendor/autoload.php');

use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\CreditCard;
use PayPal\Api\CreditCardToken;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Payer;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;	
use PayPal\Api\ItemList;
use PayPal\Api\Item;

Now get PayPal App clientid and secredid from PayPal App

$clientId = 'AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd';
$secredId = 'EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX';
$sdkConfig = array( "mode" => "sandbox" );

$cred = new OAuthTokenCredential($clientId, $secredId, $sdkConfig);

$apiContext = new ApiContext($cred, 'Request' . time());

$apiContext->setConfig($sdkConfig);

$card = new CreditCard();
$card->setType("visa");
$card->setNumber("4446283280247004");
$card->setExpireMonth("03");
$card->setExpireYear("2020");
$card->setFirstName("Amit");
$card->setLastName("Singh");

$response = $card->create($apiContext);

In the response you will get the credit card id by which you can do the paypment.

$response->id;

 

Now you can make the payment by using credit card id given by the PayPal.

$cred = new OAuthTokenCredential($clientId, $secredId, $sdkConfig);
$apiContext = new ApiContext($cred, 'Request' . time());

$apiContext->setConfig($sdkConfig);

$creditCardToken = new CreditCardToken();
$creditCardToken->setCreditCardId($response->id);

$fundingInstrument = new FundingInstrument();
$fundingInstrument->setCreditCardToken($creditCardToken);

$payer = new Payer();
$payer->setPaymentMethod("credit_card");
$payer->setFundingInstruments(array($fundingInstrument));

$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal(20);

$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription("creating a payment with saved credit card");

You can set item details by the below example.

$item1 = new Item();
$item1->setQuantity(1)
	  ->setName('Product 1')
	  ->setPrice(10)
	  ->setCurrency('USD');

$item2 = new Item();
$item2->setQuantity(1)
	  ->setName('Product 2')
	  ->setPrice(15)
	  ->setCurrency('USD');

In the SDK there in no any methods available to send the discount price. For sending discount price you need to set the price in negative as you can see the below example.

$item3 = new Item();
$item3->setQuantity(1)
	  ->setName('Coupon Discount')
	  ->setPrice(-5)
	  ->setCurrency('USD');

Now you have to pass the Items object in an array in argument of setItems method of the ItemList class.

$itemList = new ItemList();
$itemList->setItems(array($item1, $item2, $item3)); 
$transaction->setItemList($itemList)
			->setInvoiceNumber(uniqid());

In the setIntent method you pass "sale" for final payment and "authorize" for aurthorizing.

$payment = new Payment();
$payment->setIntent("authorize");
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));

Now you will get the payment response by calling the create method of Payment class.

$finalPayment = $payment->create($apiContext);

print_r(json_decode($finalPayment));

 

The above code is enough for storing the credit card details and make payment by using the credit card id.

  • paypal-vault
  • creditcard