Sharan Mohandas
Sharan Mohandas

Reputation: 31

Integrating Stripe in Codeigniter

I've been trying pretty much all ways to Integrate Stripe Library onto my Codeigniter App

I Tried Placing the Downloaded lib in application/libraries/Stripe & system/libraries/Stripe

Download Link : https://github.com/stripe/stripe-php..

There is not much Info on how to load the Library..

Below is the Code straight from Stripe's PHP Documentation https://stripe.com/docs/tutorials/charges

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
try {
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
    ));
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}

How do i Load this library because as of Now i get an error while i try to load it with the above code

Any help?

EDIT

This is how i included the Lib in Controller

require_once(APPPATH.'libraries/stripe/init.php');

Upvotes: 0

Views: 2532

Answers (1)

The Stripe documentation can get a little squirrely until you get used to it. I find that the endpoint PHP docs are much better. I opted to write my own library to do things with Stripe rather than using their download SDK. If you compare some of what's below with their documentation, it may get you pointed in the right direction to expound upon.

/application/libaries/stripe.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Stripe {

    const PROCESS_URL = 'https://api.stripe.com/v1';
    const API_KEY = '{YOUR-STRIPE-API-KEY}'; 

    public $CI;
    public $db;

    /*
    |--------------------------------------------
    | STRIPE | CONSTRUCT
    | (In case you need access to the CI Instance
    |--------------------------------------------
    */
    public function __construct()
    {
        $this->CI =   &get_instance();
        $this->db =   $this->CI->db;
    }

    /*
    |-------------------------------------------
    | STRIPE | CREATE CUSTOMER
    |-------------------------------------------
    */
    public function create_customer($request) {
        $process_url = self::PROCESS_URL.'/customers';     
        return $this->post_request($process_url, $request); 
    }

    /*
    |-------------------------------------------
    | STRIPE | GET CUSTOMER
    |-------------------------------------------
    */
    public function get_customer($cus_id) {
        $process_url = self::PROCESS_URL.'/customers/'.$cus_id;     
        $customer_obj = $this->post_request($process_url); 
        $customer_arr = json_decode($customer_obj);

        return $customer_arr;

    }

    /*
    |-------------------------------------------
    | STRIPE | GET CUSTOMER
    |-------------------------------------------
    */
    public function get_default_source($cus_id) {
        $process_url = self::PROCESS_URL.'/customers/'.$cus_id;     
        $customer_obj = $this->post_request($process_url); 
        $customer_arr = json_decode($customer_obj);
        $card_id = $customer_arr->default_source;

        return $card_id;

    }

    /*
    |-------------------------------------------
    | STRIPE | GET CARD
    |-------------------------------------------
    */
    public function get_card($cus_id,$card_id) {
        $process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/cards/'.$card_id;     
        $card_obj = $this->post_request($process_url); 

        return $this->decode_response($card_obj);

    }

    /*
    |-----------------------------------------------
    | STRIPE | ADD CARD
    | Params: Stripe Customer ID, Card Data Object
    |-----------------------------------------------
    */
    public function add_card($cus_id,$card_data) {
        $request = array(
            'source' => $card_data
        );
        $process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/sources';     
        $card_json = $this->post_request($process_url,$request); 
        $card_obj = json_decode($card_json);

        // successful?
        if(isset($card_obj->id)) {
            $ret_arr = array(
                'status' => true,
                'response' => $card_obj
            );
        } else {
            $ret_arr = array(
                'status' => false,
                'error' => $card_obj->error->message
            );   
        }

        return $ret_arr;

    }

    /*
    |-------------------------------------------
    | STRIPE | ADD CARD
    |-------------------------------------------
    */
    public function subscribe_customer($cus_id,$plan_id,$source) {
        $request['plan'] = $plan_id;
        if($source) { $request['source'] = $source; }
        $process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/subscriptions';     
        $plan_json = $this->post_request($process_url,$request); 
        $plan_obj = json_decode($plan_json);

        // successful?
        if(isset($plan_obj->id)) {
            $ret_arr = array(
                'status' => true,
                'response' => $plan_obj
            );
        } else {
            $ret_arr = array(
                'status' => false,
                'error' => $plan_obj->error->message
            );   
        }

        return $ret_arr;

    }

    /*
    |-------------------------------------------
    | STRIPE | DECODE JSON RESPONSE
    |-------------------------------------------
    */
    public function decode_response($response) {
        return json_decode($response);
    }


    /*
    |-------------------------------------------
    | STRIPE | POST REQUESTS
    |-------------------------------------------
    */
    public function post_request($url, $data = NULL)
    {
        $ch = curl_init($url);

        if (is_array($data))
        {
            $data = http_build_query($data);
        }

        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        return $this->do_curl_request($ch);
    }

    /*
    |-------------------------------------------
    | STRIPE | CURL REQUEST
    |-------------------------------------------
    */
    private function do_curl_request($ch)
    {
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_USERPWD, self::API_KEY);

        $response = curl_exec($ch);
        $error = curl_error($ch);

        if($response) {

        } else {
            var_dump($error);    
        }

        curl_close($ch);
        return $response;
    }

}

Create a New Customer

// Create Stripe Customer
$request = array(
    "description" => "{DESCRIPTION}",
    "metadata" => array(
        'First Name' => "{FIRST NAME}",
        'Last Name' => "{LAST NAME}",
        'Company Name' => "{COMPANY NAME}"
    ),
    "shipping" => array(
        "name" => "{NAME}",
        "address" => array (
            "city" => "{CITY}",
            "state" => "{STATE}",
            "postal_code" => "{POSTAL CODE}",
            "line1" => "{STREET ADDRES}",
            "line2" => "{STREET ADDRESS 2}",
        )
    ),
    "email" => "{SOME EMAIL ADDRESS}"
);

$this->load->library( 'stripe' );
$response = $this->stripe->create_customer( $request );
$response_arr = $this->stripe->decode_response( $response );
$data['stripe_customer_id'] = $response_arr->id;

Add a Payment Source

$card_object = array(
    "object"        => 'card',

    "address_city"  => "{BILLING CITY}",
    "address_state" => "{BILLING STATE}",
    "address_zip"   => "{BILLING ZIP}",
    "address_line1" => "{BILLING ADDRESS}",
    "address_line2" => "{BILLING ADDRESS 2}",

    "name"          => "{NAME ON CARD}",
    "number"        => "{CARD NUMBER}",
    "exp_month"     => "{EXPIRATION MONTH}",
    "exp_year"      => "{EXPIRATION YEAR}",
);

$this->load->library( 'stripe' );
$return = $this->stripe->add_card({STRIPE_CUSTOMER_ID},$card_object);

Hopefully that helps a bit in understanding how the stripe API works.

Upvotes: 1

Related Questions