Hook into order, charge, refund, and invoice events
surecart/order_created
Show properties
add_action( 'surecart/order_created', function( $order, $data ) { // Log new order error_log( sprintf( 'New order created: %s', $order ) ); }, 10, 2 );
surecart/order_updated
add_action( 'surecart/order_updated', function( $order, $data ) { // Sync order status changes if ( $order->status === 'paid' ) { mark_order_complete_in_erp( $order->id ); } }, 10, 2 );
surecart/charge_created
add_action( 'surecart/charge_created', function( $charge, $data ) { // Log successful payment error_log( sprintf( 'Payment received: %s for %d cents', $charge->id, $charge->amount ) ); // Track revenue track_revenue( $charge->amount, $charge->currency ); }, 10, 2 );
surecart/refund_created
add_action( 'surecart/refund_created', function( $refund, $data ) { // Send refund notification wp_mail( get_option( 'admin_email' ), 'Refund Processed', sprintf( 'A refund of %d cents has been processed.', $refund->amount ) ); }, 10, 2 );
surecart/invoice_created
add_action( 'surecart/invoice_created', function( $invoice, $data ) { // Send invoice to accounting system send_to_quickbooks( $invoice ); }, 10, 2 );
add_action( 'surecart/charge_created', function( $charge, $data ) { global $wpdb; // Store in custom revenue table $wpdb->insert( $wpdb->prefix . 'revenue_tracking', [ 'charge_id' => $charge->id, 'amount' => $charge->amount, 'currency' => $charge->currency, 'created_at' => current_time( 'mysql' ), ] ); }, 10, 2 );
add_action( 'surecart/refund_created', function( $refund, $data ) { // Update accounting record_refund_in_books( $refund->amount, $refund->currency ); // Notify admin wp_mail( get_option( 'admin_email' ), 'Refund Processed', sprintf( 'A refund of $%s has been processed for refund ID: %s', $refund->display_amount, $refund->id ) ); }, 10, 2 );
add_action( 'surecart/invoice_created', function( $invoice, $data ) { // Note: This only fires for manually created invoices. // For syncing all revenue, use surecart/charge_created instead. // Fetch the invoice with checkout and customer relations loaded. $invoice = \SureCart\Models\Invoice::with(['checkout', 'checkout.customer'])->find( $invoice->id ); $checkout = $invoice->checkout ?? null; if ( empty( $checkout ) ) { return; } // Send to QuickBooks, Xero, FreshBooks, etc. wp_remote_post( 'https://api.quickbooks.com/v3/invoice', [ 'body' => json_encode([ 'Line' => [[ 'Amount' => ( $checkout->total_amount ?? 0 ) / 100, 'Description' => 'SureCart Invoice ' . $invoice->id, ]], 'CustomerRef' => [ 'value' => $checkout->customer->id ?? '', ], ]), 'headers' => [ 'Authorization' => 'Bearer ' . QUICKBOOKS_TOKEN, 'Content-Type' => 'application/json', ], ]); }, 10, 2 );
Was this page helpful?