-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoocommerce-trexle-subscriptions-deprecated.php
245 lines (198 loc) · 8.98 KB
/
woocommerce-trexle-subscriptions-deprecated.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Gateway_Trexle_Payments_Subscriptions class (Deprecated - for Subscriptions v1.x)
*
* @extends WC_Gateway_Trexle_Payments_Subscriptions
*/
class WC_Gateway_Trexle_Payments_Subscriptions_Deprecated extends WC_Gateway_Trexle_Payments_Subscriptions {
function __construct() {
parent::__construct();
add_action( 'scheduled_subscription_payment_' . $this->id, array( $this, 'process_scheduled_subscription_payment' ), 10, 3 );
add_action( 'woocommerce_subscriptions_renewal_order_meta_query', array( $this, 'remove_renewal_order_meta' ), 10, 4 );
add_action( 'woocommerce_subscriptions_changed_failing_payment_method_'.$this->id, array($this, 'update_failing_payment_method' ), 10, 3 );
}
/**
* Process the payment and return the result
*
* @since 1.0.0
*/
function process_payment( $order_id ) {
global $woocommerce;
if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
$order = new WC_Order( $order_id );
$card_token = isset( $_POST['card_token'] ) ? woocommerce_clean( $_POST['card_token'] ) : '';
$customer_token = false;
// Are we paying by customer token?
if ( isset( $_POST['Trexle_customer_token'] ) && $_POST['Trexle_customer_token'] !== 'new' && is_user_logged_in() ) {
$customer_tokens = get_user_meta( get_current_user_id(), '_Trexle_customer_token', false );
if ( isset( $customer_tokens[ $_POST['Trexle_customer_token'] ]['customer_token'] ) ) {
$customer_token = $customer_tokens[ $_POST['Trexle_customer_token'] ]['customer_token'];
} else {
wc_add_notice(__('Invalid card. ', 'woo_Trexle_payments'),'error');
return;
}
} elseif (empty($card_token)) {
wc_add_notice(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woo_Trexle_payments' ),'error');
return;
}
if (method_exists('WC_Subscriptions_Order','get_total_initial_payment')) {
$initial_payment = WC_Subscriptions_Order::get_total_initial_payment( $order );
} else {
$initial_payment = WC_Subscriptions_Order::get_sign_up_fee( $order ) + WC_Subscriptions_Order::get_price_per_period( $order );
}
$customer_response = $this->add_customer_to_order( $order, $customer_token, $card_token );
if ($initial_payment > 0)
$payment_response = $this->process_subscription_payment($order,$initial_payment);
if (is_wp_error($customer_response)) {
wc_add_notice($customer_response->get_error_message(),'error');
return;
} elseif (isset($payment_response) && is_wp_error($payment_response)) {
wc_add_notice($payment_response->get_error_message(),'error');
return;
} else {
// Payment complete
$order->payment_complete();
$woocommerce->cart->empty_cart();
// Activate subscriptions
WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );
if ( $customer_token )
update_post_meta( $order->id, '_Trexle_customer_token', $customer_token );
if (is_woocommerce_pre_2_1()) {
$redirect = add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id'))));
} else { // WC 2.1+
$redirect = $this->get_return_url($order);
}
return array(
'result' => 'success',
'redirect' => $redirect
);
}
} else {
return parent::process_payment($order_id);
}
}
/**
* scheduled_subscription_payment function.
*
* @param $amount_to_charge float The amount to charge.
* @param WC_Order $renewal_order A WC_Order object created to record the renewal payment.
* @access public
* @return void
*/
function process_scheduled_subscription_payment( $amount_to_charge, $order, $product_id) {
$result = $this->process_subscription_payment( $order, $amount_to_charge );
if ( is_wp_error( $result ) ) {
WC_Subscriptions_Manager::process_subscription_payment_failure_on_order( $order, $product_id );
} else {
WC_Subscriptions_Manager::process_subscription_payments_on_order( $order );
}
}
/**
* process_subscription_payment function.
*
* @access public
* @param mixed $order
* @param int $amount (default: 0)
* @return void
*/
function process_subscription_payment( $order = '', $amount = 0 ) {
global $woocommerce;
$ip_address = isset( $_POST['ip_address'] ) ? woocommerce_clean( $_POST['ip_address'] ) : '';
$order_items = $order->get_items();
$product = $order->get_product_from_item( array_shift( $order_items ) );
$subscription_name = sprintf( __( '%s - Order #%s', 'woocommerce' ), esc_html( get_bloginfo( 'name', 'display' ) ), $order->get_order_number() );
$customer_token = get_post_meta( $order->id, '_Trexle_customer_token', true );
if ( ! $customer_token )
return new WP_Error( 'Trexle_error', __( 'Customer token is missing.', 'woo_Trexle_payments' ) );
$currency = get_post_meta($order->id,'_order_currency',true);
if (!$currency || empty($currency)) $currency = get_woocommerce_currency();
$post_data = array(
'email'=>$order->billing_email,
'description'=>$subscription_name,
'amount'=>number_format( (float)$amount * 100, 0, '.', '' ),
'currency'=>$currency,
'ip_address'=>$ip_address,
'customer_token'=>$customer_token
);
$result = $this->call_Trexle($post_data,'charges');
if ( is_wp_error($result) ) {
return $result;
} elseif (!isset($result->response->success)) {
return new WP_Error( 'Trexle_error', sprintf(__('Trexle Payment error: %s','woo_Trexle_payments', 'woo_Trexle_payments' ),$result->error_description));
} elseif (isset($result->response->success) && $result->response->success != 1) {
return new WP_Error( 'Trexle_error', sprintf(__('Trexle Payment error: %s', 'woo_Trexle_payments') ,$result->response->error_message));
} else {
$order->payment_complete($result->response->token);
$order->add_order_note(sprintf(__('Trexle Payments subscription payment completed (Charge ID: %s)','woo_Trexle_payments'),$result->response->token));
return true;
}
}
/**
* add_customer_to_order function.
*
* @access public
* @param mixed $order
* @param string $customer_token (default: '')
* @param string $card_token (default: '')
* @return void
*/
function add_customer_to_order( $order, $customer_token = false, $card_token = false ) {
// If we have a customer id, use it for the order
if ( $customer_token ) {
update_post_meta( $order->id, '_Trexle_customer_token', $customer_token );
}
// If we have a token, we can create a customer with it
elseif ( $card_token ) {
$post_data = array(
'email'=>$order->billing_email,
'card_token'=>$card_token
);
$result = $this->call_Trexle($post_data,'customers');
if ( is_wp_error($result) ) {
return $result;
} elseif (isset($result->response->token) && !empty($result->response->token)) {
$order->add_order_note( sprintf( __('Trexle customer added: %s', 'woo_Trexle_payments' ), $result->response->token ) );
if ( is_user_logged_in() ) {
$customer_token = array(
'customer_token'=>$result->response->token,
'display_number'=>$result->response->card->display_number,
'scheme'=>$result->response->card->scheme,
'email'=>$result->response->email
);
add_user_meta( get_current_user_id(), '_Trexle_customer_token', $customer_token);
}
update_post_meta( $order->id, '_Trexle_customer_token', $result->response->token );
return $result->response->token;
}
}
}
/**
* Don't transfer Trexle Payments customer/token meta when creating a parent renewal order.
*
* @access public
* @param array $order_meta_query MySQL query for pulling the metadata
* @param int $original_order_id Post ID of the order being used to purchased the subscription being renewed
* @param int $renewal_order_id Post ID of the order created for renewing the subscription
* @param string $new_order_role The role the renewal order is taking, one of 'parent' or 'child'
* @return void
*/
function remove_renewal_order_meta( $order_meta_query, $original_order_id, $renewal_order_id, $new_order_role ) {
if ( 'parent' == $new_order_role )
$order_meta_query .= " AND `meta_key` NOT LIKE '_Trexle_customer_token' ";
return $order_meta_query;
}
/**
* Update the customer token IDs for a subscription after a customer used the gateway to successfully complete the payment
* for an automatic renewal payment which had previously failed.
*
* @param WC_Order $original_order The original order in which the subscription was purchased.
* @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
* @return void
*/
function update_failing_payment_method( $original_order, $new_renewal_order ) {
update_post_meta( $original_order->id, '_Trexle_customer_token', get_post_meta( $new_renewal_order->id, '_Trexle_customer_token', true ) );
}
}