Skip to content

Instantly share code, notes, and snippets.

@lewayotte
Last active July 24, 2017 23:44
Show Gist options
  • Save lewayotte/928ea3c7cd0b03167bb0 to your computer and use it in GitHub Desktop.
Save lewayotte/928ea3c7cd0b03167bb0 to your computer and use it in GitHub Desktop.
Stripe Payments - WordCamp ATL 2015
<?php
// Custom forms: https://stripe.com/docs/tutorials/forms
function stripe_payment_button() {
$payment_image = false;
$publishable_key = 'key';
$transaction_return_page = get_permalink( 1 ); //Whatever page ID you're using for your transaction return page
$description = 'S-Mart';
$image = 'http://domain.tld/assets/image.png'; //If you want a custom image to appear on the Stripe popup
$payment_form = '<form action="' . $transaction_return_page .'" method="POST">';
$payment_form .= '<input type="hidden" name="transaction-method" value="stripe" />';
$payment_form .= wp_nonce_field( 'stripe-checkout', '_stripe_nonce', true, false );
$payment_form .= '<script';
$payment_form .= ' src="https://checkout.stripe.com/checkout.js" class="stripe-button"';
$payment_form .= ' data-key="' . esc_js( $publishable_key ) . '"';
$payment_form .= ' data-amount="' . esc_js( number_format( cart_total(), 2, '', '' ) ) . '"';
$payment_form .= ' data-name="' . esc_js( strip_tags( $company_name ) ) . '"';
$payment_form .= ' data-description="' . esc_js( strip_tags( $description ) ) . '"';
$payment_form .= ' data-image="' . esc_js( strip_tags( $image ) ) . '">';
$payment_form .= '</script>';
$payment_form .= '</form>';
return $payment_form;
}
<?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';
}
<?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