/stripe-integration-as-models

Some basic Stripe credit card charging integration as models for CodeIgniter 2.1x

Primary LanguagePHPMIT LicenseMIT

stripe-integration-as-models

Some basic Stripe credit card charging integration as models for CodeIgniter 2.1x

One critical thing to rememeber is that Stripe transactions are in USD and PENNIES. So you must normalize your charges to pennies instead of a dollar/cents value. Stripe will return an error for a transaction amount of (for example) 10.50

While I am using these models myself, the reason I am releasing this is to give examples on how I integrated with Stripe. The examples they gave were pretty bare bone examples, with none of the heavy lifting done, so I wanted to give other people some idea of what is going on when using them, so they can build their own integration.

Though, feel free to use these.

config

In the charge config file, load your secret key into the $config['stripe_secret_key'] variable

usage

I tried to keep the access methods to be as close to standard style model access, so you just put the in your models folder and include them as standard models.

Each one assumes that you have installed the stripe PHP api files in the APPPATH.'third_party' directory. with the stripe.php file and the folders it needs there.

stripe_trans

For handling transactions.

function init($config = array())

allows you to override the secret key defaulted in the charge config file

function get($transaction_id)

retrieves details about a given transaction, where $transaction_id is a stripe charge token

function get_all($num_charges = 100, $offset = 0)

Get last (n) charges up to 100 (default). Cannot return more than 100 at a time due to Stripe limits. Data returned is sorted with most recent first.

function count_all()

Return a count of every transaction, up to the last 100. Limit is due to Stripe limits

function insert($token, $description, $amount)

inserts a transaction into the system. i.e. a charge of a card

$token token generated by a call to stripe to create a token, usually by the javascript library $description name/email or whatever you want to use to describe this charge $amount Amount in PENNIES to charge

@return array of information regarding charge, and/or error flag (array['error'] is TRUE or FALSE, if TRUE, array['message'] has the error message returned from Stripe)

function charge($token, $description, $amount)

wrapper for insert() in case you prefer to use the word charge

function charge_customer($customer, $description, $amount)

charges using customer token instead of card token. $customer customer token from Stripe $description name/email or whatever you want to use to describe this charge $amount Amount in PENNIES to charge @return is the same as insert()

function insert_many($data)

Similar to insert(), just passing an array to insert multiple rows at once. Returns an array of insert IDs. $data Array of arrays to insert

Be aware that there may be significant delay when passing multiple transactions, and this may go past the PHP time limit on processing. This is due to each transaction being handled individually

function get_limit($limit, $offset = 0)

Returns array of all transactions dictated by $limit and $offset

function refund($transaction_id, $amount = 'all')

Refunds a transaction $transaction_id Stripe transaction token $amount amount in PENNIES to refund, or 'ALL' to indicate a full refund of remaining money on transaction

stripe_card

Handles card creation and retrieval.

function init($config = array())

allows you to override the secret key defaulted in the charge config file

function get($card_id)

Get a single card record $card_id Stripe card token

function insert($card_info)

creates a card token. Generally not called, since the Stripe recommended method is to use their javascript library to bypass sending credit card information to your server.

$card_info array of number, exp_month, exp_year, cvc, name, address_line1, address_line2, address_zip, address_state, address_country only number, exp_month, exp_year are required

function insert_many($data)

Similar to insert(), just passing an array to insert multiple rows at once. Returns an array of insert IDs. $data Array of arrays to insert

stripe_cust

Handles customer token and information. Customers are not required for charging, and you can handle charging cards just fine without them. If you need subscription charging, you will likely need them. I haven't had to deal with subscription stuff, so I haven't implemented it or tested it.

function init($config = array())

allows you to override the secret key defaulted in the charge config file

function get($customer_id)

Retrieves a single customer record from stripe

function update($customer_id, $data)

updates given data fields on a customer. Especially useful for changing card used to charge with.

$customer_id Stripe customer token array of optional items: email, description (I use this typically for name), card.

Card can be card token from Stripe or array of these fields: number, exp_month, exp_year, cvc, name, address_line1, address_line2, address_zip, address_state, address_country of which number, exp_month, and exp_year are the only required fields in this array

function delete($customer_id)

deletes a customer. But not really. Stripe deactivates the customer so that subscriptions and further charges are canceled and not allowed. Customer record is retained by them $customer_id Stripe token id of customer

function get_all($num_customers = 100, $offset = 0)

Get last (n) charges up to 100 (default). Cannot return more than 100 at a time. Data returned is sorted with most recent first.

function returns associative array from stripe Limit of 100 is Stripe based.

function count_all()

Return a count of every transaction, up to the last 100! Stripe limits this call to 100 max

function insert($name, $email, $card = NULL)

creates a new customer

$name used as description of customer, could be anything you like to describe customer $email email address of customer $card can be card token from Stripe OR array of these fields: number, exp_month, exp_year, cvc, name, address_line1, address_line2, address_zip, address_state, address_country with number, exp_month, and exp_year are the only required fields in this array

@return array array with error flag and id of customer object if successful (array['error'] = TRUE or FALSE, if TRUE then array['message'] contains error message)

create($name, $email, $card)

wrapper for insert()

function insert_many($data)

Similar to insert(), just passing an array to insert multiple rows at once. Returns an array of insert IDs. Be aware this could take some time since each customer is sent individually to Stripe, and could exceed processing time if you have too many. $data Array of arrays to insert

function get_limit($limit, $offset = 0)

Returns up to 100 customers Limit is imposed by Stripe

changelog

2012-05-17 Added stripe_plans.php (documentation to follow, but the functions are documented in the file) Fixed a bug in card in one of the functions.