Stripe Apex Toolkit

Thursday, October 6, 2011 by Aslam - The Alexendra
Hi All,
Today I came to know about 'Stripe' APIs. Stripe is a simple, developer-friendly way to accept payments online. According to Stripe :-
"We believe that enabling transactions on the web is a problem rooted in code, not finance, and we want to help put more websites in business.
Complexity and opacity have traditionally been hallmarks of online payment processing. We want to fix that."


So, as usual, here I came up with the solution of Apex Wrapper for Stripe APIs. I developed this API library with my colleague Sanjay. The library gives basic api methods to call and use as follows:

//1) CreateCharge
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCharge('4242424242424242', '2013', '10', '123', '2000', 'usd', 'testing');
if(!response.isError){
system.debug(
'Transaction ID :: ' + response.id);
system.debug(
'Transaction Fee :: ' + response.fee);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//Create charge using customer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCharge('cus_EPTWuvf7EXDb4g', '2000', 'usd', 'testing');
if(!response.isError){
system.debug(
'Transaction ID :: ' + response.id);
system.debug(
'Transaction Fee :: ' + response.fee);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//2) CreateCustomer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCustomer('4242424242424242', '10','2013', '123', 'Aslam', '', '', '', '', 'IN', 'aslam.bari@gmail.com', 'test');
if(!response.isError){
system.debug(
'Customer ID :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//3) RetrieveCustomer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retrieveCustomer('cus_EPTWuvf7EXDb4g');
if(!response.isError){
system.debug(
'Customer Country :: ' + response.card.country);
system.debug(
'Customer Last4 CC :: ' + response.card.last4);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//4) CreateInvoice
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createInvoice('cus_EPTWuvf7EXDb4g', '2000', 'usd', 'test');
if(!response.isError){
system.debug(
'Invoice Id :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//5) RetrieveInvoice
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retreiveInvoice('ii_W19Xp6cxrqVABH');
if(!response.isError){
system.debug(
'Invoice Date :: ' + response.created);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//6) CreatePlan
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createPlan('AMX_101', '2000', 'usd', 'month' , 'AMX Testing');
if(!response.isError){
system.debug(
'Plan Id :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//7) RetreivePlan
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retrievePlan('AMX_101');
if(!response.isError){
system.debug(
'Plan Label :: ' + response.name);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}


You can download the code from github here:
https://github.com/aslambari/Stripe-Apex-Toolkit

Or from my website here:
http://www.aslambari.com/stripe_apex_toolkit.html

There are many more methods which are under development like CreateSubscription, ListAllCustomers etc, we are working on them. And will back soon with full set of methods. Till, try this library and let us know if that works fine for you and revert back us for enhancements.

Thanks
Aslam Bari

Showing Salesforce Images/Attachments on PHP/JSP page

Saturday, October 1, 2011 by Aslam - The Alexendra
Hi All,
Today I am going to demo you how to fetch images (attachments) from salesforce instance in to your PHP script and how you can show those imags on page as a portal or catalog way.

1) index.php script
The below code shows how you can query to attachment object in salesforce to fetch some attachment records. For example i have query the pics (attachment) for my two SFDC contact records in sample code. The concept is simple. You need to query the Id of attachment in your main page, then you will have one another php script which will fetch the SFDC attachment (image) binary data and show the image on screen as embedded. We can wrap/embed that image in <img /> tag directly.

<?php
error_reporting(E_ALL);
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

$ids = array();
$ids[] = '00390000006IUVK';
$ids[] = '00390000001lwbd';
getPhotos(
$ids);


function getPhotos($contactids){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name from Attachment Where ParentId in (";
foreach($contactids as $id){
$query .= "'". $id . "',";
}
$query = substr($query , 0, strlen($query) - 1);
$query .= ")";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;
if(count($records) > 0){
echo "<table cellspacing='5' cellpadding='5'><tr>";
foreach($records as $rec){
echo "<td style='text-align:center;font-size:12px;'>".
"<img src='image.php?id=".$rec->Id."' ".
"style='width:200px;height:200px;border:1px solid #ccc;padding:10px'/> <br/>"
. $rec->fields->Name.
"</td>";
}
echo "</table>";
}
return NULL;
}
?>


2) image.php script
This code simply accepts one Id of the attachment record and fetch the binary body of attachment, then it shows that image on the screen as image content. To view this image in other page you simply need to wrap/embed its call into <img /> html tag. Like below:

<img src="image.php?id=00P9000000185ioEAA" />


<?php
error_reporting(E_ALL);
header('content-type: image/jpeg');
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

getPhotos(
$_GET['id']);


function getPhotos($id){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;

print_r(base64_decode($records[0]->fields->Body));
return NULL;
}

?>



Here is the demo of the scripts where i am fetching two contact's images from Salesforce and showing on simple PHP page.

http://labs.aslambari.com/sfdc_photos_demo/


Thanks
Aslam Bari