===========================================
This is a simple component that interfaces a CakePHP app with Stripe's PHP API library. Pass the component an array containing at least an amount and a Stripe token id, it will attempt the charge and return an array of the fields you want. You can create a single one time charge or you can create a customer and subscribe them to a plan you have created in your Stripe account.
Tested with CakePHP 2.2.x, though should work with any 2.x version. The required Stripe PHP API library requires PHP 5 with cURL support.
You will need the component (packaged as a plugin), and Stripe's PHP library (not included). The Stripe library needs to be in this plugin's Vendor directory and must be named 'Stripe'. Using git, something like this:
git clone git@github.com:chadwick37/CakePHP-StripeComponent-Plugin.git APP/Plugin/Stripe
git clone git://github.com/stripe/stripe-php.git APP/Plugin/Stripe/Vendor/Stripe
All configuration is in APP/Config/bootstrap.php.
Required: Load the plugin:
CakePlugin::load('Stripe', array('bootstrap' => false, 'routes' => false));
Required: Set your Stripe secret API keys (both testing and live):
Configure::write('Stripe.TestSecret', 'yourStripeTestingAPIKeyHere');
Configure::write('Stripe.LiveSecret', 'yourStripeLiveAPIKeyHere');
Optional: Set Stripe mode, either 'Live' or 'Test'. Defaults to Test if not set.
Configure::write('Stripe.mode', 'Test');
Optional: Set the currency. Defaults to 'usd'. Currently Stripe supports usd only.
Configure::write('Stripe.currency', 'usd');
Optional: fields for the component to return mapped to => Stripe charge object response fields.
Defaults to 'stripe_id' => 'id'
. See the Stripe API docs for Stripe_Charge::create() for available fields. For example:
Configure::write('Stripe.fields', array(
'stripe_id' => 'id',
'stripe_last4' => array('card' => 'last4'),
'stripe_address_zip_check' => array('card' => 'address_zip_check'),
'stripe_cvc_check' => array('card' => 'cvc_check'),
'stripe_amount' => 'amount'
));
Optional: map fields for the Stripe customer object response. The keys are whatever you choose (probably matching fields in your db) and the values are the fields from Stripe.
Configure::write('Stripe.custfields', array(
'stripe_id' => 'id',
'last4' => array('active_card' => 'last4'),
'email' => 'email',
'fingerprint' => array('active_card' => 'fingerprint'),
'client_id' => 'description'
));
See Usage below if Stripe.fields
is confusing.
Optional: add a logging config:
CakeLog::config('stripe', array(
'engine' => 'FileLog',
'types' => array('info', 'error'),
'scopes' => array('stripe'),
'file' => 'stripe',
));
Make a payment form however you want, see the Stripe docs for sample code. Add the component to your controller:
public $components = array(
'Stripe.Stripe'
);
Stripe Token This is an example of getting the Stripe Token.
Stripe::setApiKey(Configure::read('Stripe.TestSecret'));
$token = Stripe_Token::create(array(
'card' => array(
'number' => $this->request->data['Payment']['creditcard'],
'exp_month' => $this->request->data['Payment']['exp_month']['month'],
'exp_year' => $this->request->data['Payment']['exp_year']['year'],
'cvc' => $this->request->data['Payment']['cvc']
)));
Format your form data so you can send the component an array containing at least:
$data = array(
'amount' => '7.59',
'stripeToken' => 'tok_0NAEASV7h0m7ny'
);
If you are creating a customer and subscribing them to a plan format your data like the following example. Note: you must be subscribing the customer to a plan you have created in your stripe account. This component does not create subscription plans.
$customer = Stripe_Customer::create(array(
"card" => $token->id,
"plan" => "gold",
"email" => "foo@bar.com",
"description" => "additional info"
));
Optionally you can include a description
field as well, which according to Stripe docs is:
An arbitrary string which you can attach to a charge object. It is displayed when in the web interface alongside the charge. It's often a good idea to use an email address as a description for tracking later.
For example:
$data = array(
'amount' => '7.59',
'stripeToken' => 'tok_0NAEASV7h0m7ny',
'description' => 'Casi Robot - casi@robot.com'
);
Attempt a charge:
One time charge:
`$result = $this->Stripe->charge($data);`
Subscribing a customer (Note: in this instance the charge has already been made and the result was returned in the $customer object. This functions simply formats the data using the fields you set in Stripe.custfields):
`$result = $this->Stripe->_formatCustomer($customer);`
If the charge was successful, $result
will be an array as described by the configuration value
of Stripe.fields
for one time charges and Stripe.custfields
for subscriptions. If Stripe.fields
or Stripe.custfields
is not set:
$result = array(
'stripe_id' => 'ch_0NXLLCydWzSIeE'
);
If Stripe.fields
is set, using the example described above in the Configuration section would
give you:
$result = array(
'stripe_id' => 'ch_0NXLLCydWzSIeE',
'stripe_last4' => '4242',
'stripe_address_zip_check' => 'pass',
'stripe_cvc_check' => 'pass',
'stripe_amount' => 769
);
If the charge was not successful, $result
will be a string containing an error message, and
log the error.