Skip to content

Instantly share code, notes, and snippets.

@lerasah
Last active April 3, 2018 19:11
Show Gist options
  • Save lerasah/3fbc2f31d763da8a8ea09f276f445949 to your computer and use it in GitHub Desktop.
Save lerasah/3fbc2f31d763da8a8ea09f276f445949 to your computer and use it in GitHub Desktop.
Using Stripe PHP API to create functions with meaningful outputs and using them in controller
<?php
# Sachethana Pathirage
# git clone https://github.com/stripe/stripe-php into helpers folder
# load the function using $this->load->helper('ci_stripe_helper');
# tested on CodeIgniter V3.0.0
include("stripe-php-master/init.php");
$stripe = array(
"secret_key" => "sk_test_XXX",
"publishable_key" => "pk_test_XXX"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
function stripe2_createCustomer($email, $token, $plan)
{
return $customer = \Stripe\Customer::create(array(
'email' => $email,
'card' => $token,
'plan' => $plan
));
}
function stripe2_getCustomer($customer_id)
{
return \Stripe\Customer::retrieve($customer_id);
}
function stripe2_isAccountActive($customer_id)
{
$customer = stripe2_getCustomer($customer_id);
if (isset($customer['subscriptions']['data'][0]['ended_at'])) {
if ($customer['subscriptions']['data'][0]['ended_at'] == null) {
return true;
}
}
return false;
}
function stripe2_addCard($customer_id, $token)
{
$customer = \Stripe\Customer::retrieve($customer_id);
return $customer->sources->create(array(
"source" => $token
));
}
function stripe2_removeCard($customer_id, $token)
{
$customer = \Stripe\Customer::retrieve($customer_id);
return $customer->sources->retrieve($token)->delete();
}
function stripe2_changeplan($customer_id, $plan, $subscription_id)
{
$subscription = \Stripe\Subscription::retrieve($subscription_id);
$itemID = $subscription->items->data[0]->id;
return \Stripe\Subscription::update($subscription_id, array(
"items" => array(
array(
"id" => $itemID,
"plan" => $plan
)
)
));
}
//cancel subscription
function stripe2_unsubscribe($customer_id, $subscription_id)
{
$at_period_end = TRUE;
$sub = \Stripe\Subscription::retrieve($subscription_id);
return $sub->cancel(array(
'at_period_end' => $at_period_end
));
}
?>
<?
require(APPPATH.'/libraries/REST_Controller.php');
# Example controller
class Example extends REST_Controller{
function __construct() {
parent::__construct();
$this->load->model('Security_model');
$this->load->model('Accounts_model');
$this->load->helper('string');
$this->load->helper('ci_stripe_helper');
}
# Example GET method
public function heartbeat_get(){
$token = $this->input->get_request_header('Token', TRUE);
$username = strtolower($this->input->get_request_header('Username', TRUE));
$session = $this->Security_model->validate_session($token);
if($session != FALSE && $username){
$user_to_login = $this->Account_model->get_user($Username);
if($user_to_login==FALSE){
$this->response(array(
"Message"=>"We can't find your account. Please login again.",
"Action"=>"logout",
"Status"=>false,
"Timestamp" => date('m-d-Y H:i:s')
));
exit;
}
if($user_to_login['Active'] != 1){
$this->response(array(
"Message"=>"Your AppName account is not active.",
"Action"=>"logout",
"Status"=>false,
"Timestamp" => date('m-d-Y H:i:s'));
exit;
}
if(isset($user_to_login['stripe_user'])){
//stripe handles it's own errors :)
$this->response(array(
"Status"=>true,
"Active"=>stripe2_isAccountActive($user_to_login['stripe_user']),
"Timestamp" => date('m-d-Y H:i:s')));
}else{
$this->response(
array(
"Message"=>"You haven't subscribed to any plans in AppName. Please login again to select a plan.",
"Action"=>"subscribe",
"Status"=>false,
"Timestamp" => date('m-d-Y H:i:s')
));
}
}else{
$this->response(array(
"Message"=>"Your session has expired. You need to login again",
"Action"=>"logout",
"Status"=>false,
"Timestamp" => date('m-d-Y H:i:s'));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment