Skip to content

Instantly share code, notes, and snippets.

@uakhan
Created October 18, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uakhan/d96e31ed310aa02ec18902c24d9b4a3c to your computer and use it in GitHub Desktop.
Save uakhan/d96e31ed310aa02ec18902c24d9b4a3c to your computer and use it in GitHub Desktop.
public function capture_payment( $order_id ) {
$order = wc_get_order( $order_id );
if ( 'stripe' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->payment_method : $order->get_payment_method() ) ) {
$charge = get_post_meta( $order_id, '_stripe_charge_id', true );
$captured = get_post_meta( $order_id, '_stripe_charge_captured', true );
if ( $charge && 'no' === $captured ) {
$result = WC_Stripe_API::request( array(
'amount' => $order->get_total() * 100,
'expand[]' => 'balance_transaction',
), 'charges/' . $charge . '/capture' );
if ( is_wp_error( $result ) ) {
$order->add_order_note( __( 'Unable to capture charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() );
} else {
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) );
update_post_meta( $order_id, '_stripe_charge_captured', 'yes' );
// Store other data such as fees
update_post_meta( $order_id, 'Stripe Payment ID', $result->id );
update_post_meta( $order_id, '_transaction_id', $result->id );
if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) {
// Fees and Net needs to both come from Stripe to be accurate as the returned
// values are in the local currency of the Stripe account, not from WC.
$fee = ! empty( $result->balance_transaction->fee ) ? self::format_number( $result->balance_transaction, 'fee' ) : 0;
$net = ! empty( $result->balance_transaction->net ) ? self::format_number( $result->balance_transaction, 'net' ) : 0;
update_post_meta( $order_id, 'Stripe Fee', $fee );
update_post_meta( $order_id, 'Net Revenue From Stripe', $net );
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment