Created
June 3, 2013 20:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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