Skip to content

Instantly share code, notes, and snippets.

@supercleanse
Created June 3, 2013 20:40
Show Gist options
  • Select an option

  • Save supercleanse/5701212 to your computer and use it in GitHub Desktop.

Select an option

Save supercleanse/5701212 to your computer and use it in GitHub Desktop.
This is the request wrapper I used with the Stripe gateway interface in MemberPress. It does some basic error handling and automatically decodes the json returned from stripe. You'd just wrap this function in a try / catch block for error handling ... but if alls good it should return a data structure.
<?php
function send_stripe_request( $endpoint,
$args=array(),
$method='post',
$domain='https://api.stripe.com/v1/',
$blocking=true ) {
$uri = "{$domain}{$endpoint}";
$arg_array = array( 'method' => strtoupper($method),
'body' => $args,
'timeout' => 15,
'blocking' => $blocking,
'sslverify' => false, // We assume the cert on stripe is trusted
'headers' => array(
'Authorization' => "Basic " . base64_encode("{$this->settings->secret_key}:")
)
);
$resp = wp_remote_request( $uri, $arg_array );
// If we're not blocking then the response is irrelevant
// So we'll just return true.
if( $blocking==false )
return true;
if( is_wp_error( $resp ) ) {
throw new MeprHttpException( sprintf( __( 'You had an HTTP error connecting to %s' , 'memberpress'), $this->name ) );
}
else {
if( null !== ( $json_res = json_decode( $resp['body'], true ) ) ) {
if( isset($json_res['error']) )
throw new MeprRemoteException( "{$json_res['error']['message']} ({$json_res['error']['type']})" );
else
return $json_res;
}
else // Un-decipherable message
throw new MeprRemoteException( sprintf( __( 'There was an issue with the credit card processor. Try again later.', 'memberpress'), $this->name ) );
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment