Created
October 18, 2017 16:24
-
-
Save uakhan/d96e31ed310aa02ec18902c24d9b4a3c to your computer and use it in GitHub Desktop.
This file contains 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
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