Chargify | Single php function for all Chargify API calls

|
| By Webner

Following single function can be used for all Chargify API calls whether API calls belong to test site (site for test transactions) or live site (live transactions):

<?php
function sendRequest($uri, $method, $data = '')
{
$apiKey = 'XOTUsyy7pJ6yLgmMa233AtJ';
$subdomain = 'abccompany-test';
$username = $apiKey;
$password = 'x';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" .$subdomain. ".chargify.com/".$uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_USERPWD,$username . ':' . $password);
if($method == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
elseif($method == 'PUT')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if($method == 'GET' || $method == 'DELETE')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result->response = curl_exec($ch);
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}

Four examples will be explained here to describe how to use above function for all types of Chargify API calls like create the subscription, update subscription, cancel/delete subscription and show list of transactions for particular customer. There are many more API calls, for which above same function can be used.

First of all, you should be aware of what is subscription. Subscriptions tie the customers to plans/products on chargify and customers are billed recurringly and charged. Subscription is basically description about product/plan purchased by customer, payment details of customer etc. on chargify.

Example 1: To charge a customer having subscription id = 9657148:

<?php
$subscriptionId = 9657148;
$uri = 'subscriptions/'.$subscriptionId.'/charges.json';
$method = 'POST';
$chargeData = array('charge' => array("amount" => 20, "memo" => "This is one time charge"));
$data = json_encode($chargeData);
$chargeResponse = sendRequest($uri, $method, $data);
if($chargeResponse->code == 201)
echo "customer is charged successfully";
else
echo "not enable to charge customer";
?>

Example 2: Update a credit card information of a customer having subscription id = 9657148:

<?php
$subscriptionId = 9657148;
$uri = 'subscriptions/'.$subscriptionId.'.json';
$cardData = array('subscription' => array(
"credit_card_attributes" => array(
"full_number" => "5555555555554444",
"cvv"=>"123",
"first_name"=>"Abcfirst",
"last_name"=>"Abclast",
"expiration_month"=>03,
"expiration_year"=>2018
)));
$data = json_encode($cardData);
$method = "PUT";
$updateResponse = sendRequest($uri, $method, $data);
if($updateResponse->code == 200)
echo "customer's credit card information updated successfully";
else
echo "customer's credit card information not updated.";
?>

Example 3: List Transactions belonging to a subscription or given customer:

<?php
$subscriptionId = 9657148;
$uri = 'subscriptions/'.$subscriptionId.'/transactions.json';
$method = "GET";
$transactionResponse = sendRequest($uri, $method);
if($transactionResponse->code == 200)
{
echo "; print_r(json_decode($transactionResponse->response, true))";
}
else
echo "customer's credit card information not updated.";
?>

Example 4: Delete/Cancel subscription of the customer from Chargify:

<?php
$subscriptionId = 9924958;
$uri = 'subscriptions/'.$subscriptionId.'.json';
$method = 'DELETE';
$deleteResponse = sendRequest($uri, $method);
if($deleteResponse->code == 200)
echo "subscription of customer deleted successfully";
else
echo "customer not deleted";

Leave a Reply

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