Completed
Push — master ( e3a5fe...3adadd )
by Roy
05:10
created

WC_Stripe_Pre_Orders_Compat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Compatibility class for Pre-Orders.
8
 *
9
 */
10
class WC_Stripe_Pre_Orders_Compat extends WC_Stripe_Payment_Gateway {
11
	public $saved_cards;
12
13
	public function __construct() {
14
		$this->saved_cards = WC_Stripe_Helper::get_settings( 'stripe', 'saved_cards' );
15
	}
16
17
	/**
18
	 * Is $order_id a pre-order?
19
	 * @param  int  $order_id
20
	 * @return boolean
21
	 */
22
	public function is_pre_order( $order_id ) {
23
		return WC_Pre_Orders_Order::order_contains_pre_order( $order_id );
24
	}
25
26
	/**
27
	 * Remove order meta
28
	 * @param object $order
29
	 */
30
	public function remove_order_source_before_retry( $order ) {
31
		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
32
		delete_post_meta( $order_id, '_stripe_source_id' );
33
		// For BW compat will remove in the future.
34
		delete_post_meta( $order_id, '_stripe_card_id' );
35
	}
36
37
	/**
38
	 * Remove order meta
39
	 * @param  object $order
40
	 */
41
	public function remove_order_customer_before_retry( $order ) {
42
		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
43
		delete_post_meta( $order_id, '_stripe_customer_id' );
44
	}
45
46
	/**
47
	 * Process the pre-order when pay upon release is used.
48
	 * @param int $order_id
49
	 */
50
	public function process_pre_order( $order_id ) {
51
		try {
52
			$order = wc_get_order( $order_id );
53
54
			// This will throw exception if not valid.
55
			$this->validate_minimum_order_amount( $order );
56
57
			$prepared_source = $this->prepare_source( get_current_user_id(), true );
58
59
			// We need a source on file to continue.
60
			if ( empty( $prepared_source->customer ) || empty( $prepared_source->source ) ) {
61
				throw new WC_Stripe_Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) );
62
			}
63
64
			$this->save_source_to_order( $order, $prepared_source );
65
66
			// Remove cart
67
			WC()->cart->empty_cart();
68
69
			// Is pre ordered!
70
			WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
71
72
			// Return thank you page redirect
73
			return array(
74
				'result'   => 'success',
75
				'redirect' => $this->get_return_url( $order ),
76
			);
77
		} catch ( WC_Stripe_Exception $e ) {
78
			wc_add_notice( $e->getLocalizedMessage(), 'error' );
79
			WC_Stripe_Logger::log( 'Pre Orders Error: ' . $e->getMessage() );
80
81
			return array(
82
				'result'   => 'success',
83
				'redirect' => $order->get_checkout_payment_url( true ),
84
			);
85
		}
86
	}
87
88
	/**
89
	 * Process a pre-order payment when the pre-order is released.
90
	 * @param WC_Order $order
91
	 * @return void
92
	 */
93
	public function process_pre_order_release_payment( $order ) {
94
		try {
95
			// Define some callbacks if the first attempt fails.
96
			$retry_callbacks = array(
97
				'remove_order_source_before_retry',
98
				'remove_order_customer_before_retry',
99
			);
100
101
			while ( 1 ) {
102
				$source   = $this->prepare_order_source( $order );
103
				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) );
104
105
				if ( ! empty( $response->error ) ) {
106
					if ( 0 === sizeof( $retry_callbacks ) ) {
107
						throw new Exception( $response->error->message );
108
					} else {
109
						$retry_callback = array_shift( $retry_callbacks );
110
						call_user_func( array( $this, $retry_callback ), $order );
111
					}
112
				} else {
113
					// Successful
114
					$this->process_response( $response, $order );
115
					break;
116
				}
117
			}
118
		} catch ( Exception $e ) {
119
			/* translators: error message */
120
			$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() );
121
122
			// Mark order as failed if not already set,
123
			// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
124
			if ( ! $order->has_status( 'failed' ) ) {
125
				$order->update_status( 'failed', $order_note );
126
			} else {
127
				$order->add_order_note( $order_note );
128
			}
129
		}
130
	}
131
}
132