Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active September 6, 2023 17:04
Show Gist options
  • Save xlplugins/9665cf6602184e0f0acd3305b9d80a32 to your computer and use it in GitHub Desktop.
Save xlplugins/9665cf6602184e0f0acd3305b9d80a32 to your computer and use it in GitHub Desktop.
Stripe webhook custom processing order
/**
* attaching the single schedule action on webhook event
* this will take care of few edge cases where we have the stripe payment went through fine but in wc no activity shows up
*/
add_action( 'fkwcs_webhook_event_intent_succeeded', function ( $charge, $order ) {
if ( as_has_scheduled_action( 'fkwcs_custom_single_order', [ 'order' => $order->get_id() ] ) ) {
return;
}
as_schedule_single_action( time() + ( MINUTE_IN_SECONDS * 3 ), 'fkwcs_custom_single_order', [ 'order' => $order->get_id() ] );
}, 10, 2 );
/**
* attaching action to the schedule hook
* we here check if we have order unpaid after the scheduled time, we might need to run our order success methods to take care of this order
*/
add_action( 'fkwcs_custom_single_order', function ( $order_id ) {
if ( is_null( $order_id ) ) {
return;
}
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return;
}
if ( $order->is_paid() ) {
return;
}
if ( $order->has_status( 'wfocu-pri-order' ) ) {
return;
}
FKWCS\Gateway\Stripe\Helper::log( 'Processing order ID in schedule ' . $order_id );
$gateway = WC()->payment_gateways()->payment_gateways()['fkwcs_stripe'];
$stripe_api = $gateway->get_client();
$intent_secret = $order->get_meta( '_fkwcs_intent_id' );
if ( ! empty( $intent_secret ) ) {
$secret = $intent_secret;
$response = $stripe_api->payment_intents( 'retrieve', [ $secret['id'] ] );
if ( $response['success'] && ( 'succeeded' === $response['data']->status || 'success' === $response['data']->status ) ) {
$gateway->save_payment_method( $order, $response['data'] );
$gateway->process_final_order( end( $response['data']->charges->data ), $order->get_id() );
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment