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.
In the charge config file, load your secret key into the $config['stripe_secret_key'] variable
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.
For handling transactions.
allows you to override the secret key defaulted in the charge config file
retrieves details about a given transaction, where $transaction_id is a stripe charge token
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.
Return a count of every transaction, up to the last 100. Limit is due to Stripe limits
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)
wrapper for insert() in case you prefer to use the word charge
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()
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
Returns array of all transactions dictated by $limit and $offset
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
Handles card creation and retrieval.
allows you to override the secret key defaulted in the charge config file
Get a single card record $card_id Stripe card token
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
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
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.
allows you to override the secret key defaulted in the charge config file
Retrieves a single customer record from stripe
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
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
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.
Return a count of every transaction, up to the last 100! Stripe limits this call to 100 max
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)
wrapper for insert()
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
Returns up to 100 customers Limit is imposed by Stripe
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.