Simplify Commerce Basics Using PHP SDK

24 Nov

Simplify Commerce Basics Using PHP SDK

Simplify Commerce is a payment gateway brought to you by MasterCard.
They have SDK for most of the programming languages. Here i am going to explain about PHP SDK of the Simplify Commerce. Using Simplify Commerce we can do payments using token. With this approach we don’t have to pass card number and also we are not storing the cc number in the database.

First of all you have to download the SDK and include it in your code as shown below. Link to download the SDK is
https://www.simplify.com/commerce/sdk/php/simplifycommerce-sdk-php-1.6.0.tgz

require_once 'Simplify.php'

In your Simplify account, you will have public key and private key for both live and sandbox. For testing use sandbox keys and when you go live have to use the live keys.

        require_once 'Simplify.php'
        if($is_test_mode){
                $pub_key = $sandbox_pubkey;
                $prive_key $sandbox_privkey;
        } else {
                $pub_key = $live_pubkey;
                $prive_key = $live_privkey;
        }
        try{
                Simplify::$publicKey = $pub_key;
                Simplify::$privateKey = $prive_key;
        }catch (Simplify_ApiException $e) {
                die($e->getMessage());
        }


How to get Simplify token?

You can create a token that can be used only once using the below code. You won’t be able to use the token more than once.

  $card = array(
      'addressState' => $state,
      'expMonth' => $exp_m,
      'expYear' => $exp_y,
      'addressCity' => $city,
      'cvc' => $cvc,
      'number' => $card_num
  );
  try {
        $cardToken =  Simplify_CardToken::createCardToken(
          array(
              'card' => $card 
          )
        );
        $token = $cardToken->id;
  } catch (Simplify_ApiException $e) {
        die( $e->getMessage());
  }

How to create a customer using the simplify token?
You can create a customer using the below code. You can see that we are using the token created. The below API will return a customer id
and we can use that for multiple transactions. Even though the token id can be used only once, the customer id can be used many times.

  
try {
    $data = array(
        'token' => $token,
        'email' => $email,
        'name' =>  $name,
        'reference' => $reference
    );
    $customer = Simplify_Customer::createCustomer(
        $data
    );
    $customer_id = $customer->id;
}catch (Simplify_ApiException $e){
    die( $e->getMessage());
}

How to make a payment using the simplify customer id?
The sample payment code is below. One important point to remember is, if you want to make a payment of $10, then we have to pass amount as
10 * 100, not 10. So for $10, we have pass amount as 1000 and currency as USD. We have to pass the $customer_id we generated in the previous step. We can pass anything as reference. Normally we pass order id or invoice id as reference.

  
$payment_data = array(
    'amount' =>  $amount * 100,
    'customer' => $customer_id,
    'description' => $description,
    'currency' => 'USD',
    'reference' => $invoice_id
);
try {
   $payment = Simplify_Payment::createPayment(
       $payment_data
   );
} catch (Simplify_ApiException $e) {
   die( $e->getMessage());
}

How to add a monthly subscription for the simplify customer id?
You can add a recurring payment from a customer as below.
amount and customer id, we already discussed in the above step.
If the customer will pay monthly, we have to set frequency as MONTHLY and frequencyPeriod as 1. If the customer will pay bimonthly then frequency should be MONTHLY and frequencyPeriod should be 2.
For semiannual payments set frequency=MONTHLY, frequencyPeriod=6.

Possible values for frequency are DAILY, WEEKLY, MONTHLY and YEARLY.

  
$subscription_data = array(
   'amount' =>  $amount * 100,
   'customer' => $customer_id,
   'frequency' => $frequency,
   'name' => $name,
   'frequencyPeriod' => $frequencyPeriod
);
try {
   $subscription  = Simplify_Subscription::createSubscription(
      $subscription_data
   );
} catch (Simplify_ApiException $e) {
   die( $e->getMessage());
}

Developed a WHMCS payment gateway module using the Simplify Commerce PHP SDK. Please contact me if you need it.

Leave a Reply

Your email address will not be published. Required fields are marked *