1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Payment_Charge_Local extends WC_Stripe_Payment_Charge {
public function process_payment( $order ) {
if ( ! $this->payment_method->has_order_lock( $order ) ) {
try {
if ( ( $source_id = $this->payment_method->get_new_source_token() ) ) {
$source = $this->gateway->sources->mode( wc_stripe_order_mode( $order ) )->retrieve( $source_id );
$this->save_order_data( $source_id, $order );
if ( is_wp_error( $source ) ) {
return $source;
}
if ( 'pending' === $source->status ) {
$source = $this->gateway->sources->mode( wc_stripe_order_mode( $order ) )->update( $source_id, $this->payment_method->get_update_source_args( $order ) );
}
} else {
if ( $this->payment_method->use_saved_source() ) {
$source_id = $this->payment_method->get_saved_source_id();
$source = $source = $this->gateway->sources->mode( wc_stripe_order_mode( $order ) )->retrieve( $source_id );
} else {
$args = $this->payment_method->get_source_args( $order );
$args['metadata']['order_id'] = $order->get_id();
$args['metadata']['created'] = time();
$source = $this->gateway->sources->mode( wc_stripe_order_mode( $order ) )->create( $args );
}
}
if ( is_wp_error( $source ) ) {
if ( false !== strpos( strtolower( $source->get_error_message() ), 'update of a chargeable source for single use not allowed' ) ) {
return (object) array(
'complete_payment' => false,
'redirect' => $this->payment_method->get_return_url( $order )
);
}
throw new Exception( $source->get_error_message() );
}
$this->save_order_data( $source->id, $order );
if ( $source->status === 'chargeable' ) {
$this->payment_method->set_order_lock( $order );
$this->payment_method->set_new_source_token( $source->id );
return $this->process_payment( $order );
}
return (object) array(
'complete_payment' => false,
'redirect' => $this->payment_method->get_source_redirect_url( $source, $order ),
);
} catch ( Exception $e ) {
return new WP_Error( 'source-error', $e->getMessage() );
}
} else {
return parent::process_payment( $order );
}
}
private function save_order_data( $source_id, $order ) {
$order->update_meta_data( WC_Stripe_Constants::MODE, wc_stripe_mode() );
$order->update_meta_data( WC_Stripe_Constants::SOURCE_ID, $source_id );
$order->save();
}
}