-
-
Save jimboobrien/8e6a4dbcfd97d78f45af3302ff55f83b to your computer and use it in GitHub Desktop.
Stripe Payments - WordCamp ATL 2015
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 stripe_process_transaction( $status, $transaction_object ) { | |
// Verify nonce | |
if ( ! empty( $_REQUEST['_stripe_nonce'] ) && ! wp_verify_nonce( $_REQUEST['_stripe_nonce'], 'stripe-checkout' ) ) { | |
error_log( 'Transaction Failed, unable to verify security token.' ); | |
return false; | |
} | |
// Make sure we have the correct $_POST argument | |
if ( ! empty( $_POST['stripeToken'] ) ) { | |
try { | |
$secret_key = 'secret-key'; | |
$customer_email = ''; | |
Stripe::setApiKey( $secret_key ); | |
// Set stripe token | |
$token = $_POST['stripeToken']; | |
$customer_array = array( | |
'email' => $customer_email, | |
'card' => $token, | |
); | |
$stripe_customer = Stripe_Customer::create( $customer_array ); | |
// Now that we have a valid Customer ID, charge them! | |
$args = apply_filters( 'it_exchange_stripe_addon_charge_args', array( | |
'customer' => $stripe_customer->id, | |
'amount' => number_format( cart_total(), 2, '', '' ), //no decimal! | |
'currency' => 'usd', | |
'description' => $description, | |
) ); | |
$charge = Stripe_Charge::create( $args ); | |
return $charge->id; | |
} | |
catch ( Exception $e ) { | |
error_log( $e->getMessage() ); | |
return false; | |
} | |
} | |
return false; | |
} | |
function cart_total() { | |
return '50.00'; | |
} |
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 stripe_process_webhook( $request ) { | |
$body = @file_get_contents('php://input'); | |
$stripe_payload = json_decode( $body ); | |
if ( !empty( $stripe_payload->id ) ) { | |
try { | |
$secret_key = 'secret-key'; | |
Stripe::setApiKey( $secret_key ); | |
$stripe_event = Stripe_Event::retrieve( $stripe_payload->id ); | |
$stripe_object = $stripe_event->data->object; | |
//https://stripe.com/docs/api#event_types | |
switch( $stripe_event->type ) { | |
case 'charge.succeeded' : | |
//Record the transaction for the customer... | |
break; | |
case 'charge.failed' : | |
//Record the failure for the customer... | |
break; | |
case 'charge.refunded' : | |
//Record the refund for the customer... | |
break; | |
case 'charge.dispute.created' : | |
case 'charge.dispute.updated' : | |
case 'charge.dispute.closed' : | |
//Record the dispute for the customer... | |
break; | |
case 'customer.deleted' : | |
//Delete the Stripe ID from the customer... | |
break; | |
} | |
} | |
catch( Exception $e ) { | |
error_log( $e->getMessage() ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment