Skip to content

Instantly share code, notes, and snippets.

@carldanley
Created July 26, 2013 20:52
Show Gist options
  • Save carldanley/6092151 to your computer and use it in GitHub Desktop.
Save carldanley/6092151 to your computer and use it in GitHub Desktop.
<?php
/**
* Class WP_Stripe
* Author: Carl Danley
*
* This class handles processing any Stripe actions by letting the WP HTTP API do all of the heavy lifting.
*
*/
class WP_Stripe {
/**
* The base URL that will be used to connect to Stripe
*
* @var string
*/
protected static $_api_base_url = 'https://api.stripe.com/v1/';
/**
* The API key that will be used when connecting to the Stripe API
*
* @var string
*/
protected static $_api_key = '';
/**
* The API version that will be used when connecting to the Stripe API
*
* @var string
*/
protected static $_api_version = '1.8.1';
/**
* An empty constructor function
*/
public function __construct() {}
/** CHARGES = https://stripe.com/docs/api/curl#charges */
public static function create_charge( $params = array() ) {
return self::_execute_request( $params, 'charges', 'post' );
}
public static function refund_charge( $charge_id = '', $params = array() ) {
return self::_execute_request( $params, 'charges/' . $charge_id . '/refund', 'post' );
}
public static function capture_charge( $charge_id = '', $params = array() ) {
return self::_execute_request( $params, 'charges/' . $charge_id . '/capture', 'post' );
}
public static function get_charge( $charge_id = '' ) {
return self::_execute_request( array(), 'charges/' . $charge_id, 'get' );
}
public static function get_all_charges( $params = array() ) {
return self::_execute_request( $params, 'charges/', 'get' );
}
/** CUSTOMERS = https://stripe.com/docs/api/curl#customers */
public static function create_customer( $params = array() ) {
return self::_execute_request( $params, 'customers', 'post' );
}
public static function update_customer( $customer_id = '', $params = array() ) {
return self::_execute_request( $params, 'customers/' . $customer_id, 'post' );
}
public static function delete_customer( $customer_id = '' ) {
return self::_execute_request( array(), 'customers/' . $customer_id, 'delete' );
}
public static function get_customer( $customer_id = '' ) {
return self::_execute_request( array(), 'customers/' . $customer_id, 'get' );
}
/** CARDS = https://stripe.com/docs/api/curl#cards */
public static function create_card( $customer_id = '', $params = array() ) {
return self::_execute_request( $params, 'customers/' . $customer_id . '/cards', 'post' );
}
public static function delete_card( $customer_id = '', $card_id = '' ) {
return self::_execute_request( array( 'id' => $card_id ), 'customers/' . $customer_id . '/cards', 'delete' );
}
public static function update_card( $customer_id = '', $params = array() ) {
return self::_execute_request( $params, 'customers/' . $customer_id, 'post' );
}
public static function get_card( $customer_id = '', $card_id = '' ) {
return self::_execute_request( array( 'id' => $card_id ), 'customers/' . $customer_id . '/cards', 'get' );
}
/** SUBSCRIPTIONS = https://stripe.com/docs/api/curl#subscriptions */
public static function update_subscription( $customer_id = '', $params = array() ) {
return self::_execute_request( $params, 'customers/' . $customer_id . '/subscription', 'post' );
}
public static function cancel_subscription( $customer_id = '', $params = array() ) {
return self::_execute_request( $params, 'customers/' . $customer_id . '/subscription', 'delete' );
}
/** PLANS = https://stripe.com/docs/api/curl#plans */
public static function create_plan( $params = array() ) {
return self::_execute_request( $params, 'plans', 'post' );
}
public static function delete_plan( $plan_id = '' ) {
return self::_execute_request( array(), 'plans/' . $plan_id, 'delete' );
}
public static function update_plan( $plan_id = '', $params = array() ) {
return self::_execute_request( $params, 'plans/' . $plan_id, 'post' );
}
public static function get_plan( $plan_id = '' ) {
return self::_execute_request( array(), 'plans/' . $plan_id, 'get' );
}
/** COUPONS = https://stripe.com/docs/api/curl#coupons */
public static function create_coupon( $params = array() ) {
return self::_execute_request( $params, 'coupons', 'post' );
}
public static function delete_coupon( $coupon_id = '' ) {
return self::_execute_request( array(), 'coupons/' . $coupon_id, 'delete' );
}
public static function update_coupon( $coupon_id = '', $params = array() ) {
return self::_execute_request( $params, 'coupons/' . $coupon_id, 'post' );
}
public static function get_all_coupons( $params = array() ) {
return self::_execute_request( $params, 'coupons/', 'get' );
}
/** DISCOUNTS = https://stripe.com/docs/api/curl#discounts */
public static function delete_discount( $customer_id = '' ) {
return self::_execute_request( array(), 'customers/' . $customer_id . '/discount', 'delete' );
}
/** INVOICES = https://stripe.com/docs/api/curl#invoices */
public static function get_invoice( $invoice_id = '' ) {
return self::_execute_request( array(), 'invoices/' . $invoice_id, 'get' );
}
public static function get_invoice_items( $invoice_id = '', $params = array() ) {
return self::_execute_request( $params, 'invoices/' . $invoice_id . '/lines', 'get' );
}
public static function create_invoice( $customer_id = '' ) {
return self::_execute_request( array( 'customer' => $customer_id ), 'invoices', 'post' );
}
public static function pay_invoice( $invoice_id = '' ) {
return self::_execute_request( array(), 'invoices/' . $invoice_id . '/pay', 'post' );
}
public static function update_invoice( $invoice_id = '', $params = array() ) {
return self::_execute_request( $params, 'invoices/' . $invoice_id, 'post' );
}
public static function get_all_invoices( $params = array() ) {
return self::_execute_request( $params, 'invoices', 'get' );
}
public static function get_upcoming_invoice( $customer_id = '' ) {
return self::_execute_request( array( 'customer' => $customer_id ), 'invoices/upcoming', 'get' );
}
/** INVOICE ITEMS = https://stripe.com/docs/api/curl#invoiceitems */
public static function create_invoice_item( $params = array() ) {
return self::_execute_request( $params, 'invoiceitems', 'post' );
}
public static function get_invoice_item( $invoice_item_id = '' ) {
return self::_execute_request( array(), 'invoiceitems/' . $invoice_item_id, 'get' );
}
public static function update_invoice_item( $invoice_item_id = '', $params = array() ) {
return self::_execute_request( $params, 'invoiceitems/' . $invoice_item_id, 'post' );
}
public static function delete_invoice_item( $invoice_item_id = '' ) {
return self::_execute_request( array(), 'invoiceitems/' . $invoice_item_id, 'delete' );
}
public static function get_all_invoice_items( $params = array() ) {
return self::_execute_request( $params, 'invoiceitems/', 'get' );
}
/** DISPUTES = https://stripe.com/docs/api/curl#disputes */
public static function update_dispute( $charge_id = '', $params = array() ) {
return self::_execute_request( $params, 'charges/' . $charge_id . '/dispute', 'post' );
}
/** TRANSFERS = https://stripe.com/docs/api/curl#transfers */
public static function create_transfer( $params = array() ) {
return self::_execute_request( $params, 'transfers', 'post' );
}
public static function get_transfer( $transfer_id = '' ) {
return self::_execute_request( array(), 'transfers/' . $transfer_id, 'get' );
}
public static function cancel_transfer( $transfer_id = '' ) {
return self::_execute_request( array(), 'transfers/' . $transfer_id . '/cancel', 'post' );
}
public static function get_all_transfers( $params = array() ) {
return self::_execute_request( $params, 'transfers', 'get' );
}
/** RECIPIENTS = https://stripe.com/docs/api/curl#recipients */
public static function create_recipient( $params = array() ) {
return self::_execute_request( $params, 'recipients', 'post' );
}
public static function get_recipient( $recipient_id = '' ) {
return self::_execute_request( array(), 'recipients/' . $recipient_id, 'get' );
}
public static function update_recipient( $recipient_id = '', $params = array() ) {
return self::_execute_request( $params, 'recipients/' . $recipient_id, 'post' );
}
public static function delete_recipient( $recipient_id = '' ) {
return self::_execute_request( array(), 'recipients/' . $recipient_id, 'delete' );
}
public static function get_all_recipients( $params = array() ) {
return self::_execute_request( $params, 'recipients', 'get' );
}
/** ACCOUNT = https://stripe.com/docs/api/curl#account */
public static function get_account() {
return self::_execute_request( array(), 'account', 'get' );
}
/** BALANCE = https://stripe.com/docs/api/curl#balance */
public static function get_balance() {
return self::_execute_request( array(), 'balance', 'get' );
}
public static function get_balance_history( $params = array() ) {
return self::_execute_request( $params, 'balance/history', 'get' );
}
/** EVENTS = https://stripe.com/docs/api/curl#events */
public static function get_event( $event_id = '' ) {
return self::_execute_request( array(), 'events/' . $event_id, 'get' );
}
public static function get_all_events( $params = array() ) {
return self::_execute_request( $params, 'events', 'get' );
}
/** TOKENS = https://stripe.com/docs/api/curl#tokens */
public static function create_token( $params = array() ) {
return self::_execute_request( $params, 'tokens', 'post' );
}
public static function get_token( $token_id = array() ) {
return self::_execute_request( array(), 'tokens/' . $token_id, 'get' );
}
/**
* Executes the remote request using WP HTTP API to handle all of the heavy-lifting.
*
* @param array $params
* @param string $action
* @param string $method
* @return array|bool|mixed
*/
protected static function _execute_request( $params = array(), $action = '', $method = 'post' ) {
$api_url = self::$_api_base_url . $action;
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . self::$_api_key,
),
'timeout' => 12,
'sslverify' => false,
'body' => $params,
'method' => strtoupper( $method )
);
$response = wp_remote_request( $api_url, $args );
$response_code = wp_remote_retrieve_response_code( $response );
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
if( $return_error = self::_detect_api_error( $response_body, $response_code ) )
return $return_error;
return $response_body;
}
/**
* Detects if the data passed back from the API is an error
*
* @param array $data
* @return bool
*/
public static function is_error( $data = array() ) {
return isset( $data[ 'error' ] );
}
/**
* Detects if the HTTP code was a success or if there was an error. If there was an array, we return the data
* associated to it. Otherwise, return false
*
* @param array $result
* @param int $result_code
* @return bool|array
*/
protected static function _detect_api_error( $result = array(), $result_code = 0 ) {
if( $result_code >= 200 && $result_code < 300 )
return false;
$data = array();
if( isset( $result[ 'message' ] ) )
$data[ 'message' ] = $result[ 'message' ];
if( isset( $result[ 'param' ] ) )
$data[ 'param' ] = $result[ 'param' ];
if( isset( $result[ 'code' ] ) )
$data[ 'code' ] = $result[ 'code' ];
if( 404 === $result_code || 400 === $result_code )
$data[ 'error' ] = 'invalid-request';
else if( 401 === $result_code )
$data[ 'error' ] = 'authentication';
else if( 402 === $result_code )
$data[ 'error' ] = 'card';
else
$data[ 'error' ] = 'unknown';
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment