class-wc-payments-invoice-service.php
13.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Class WC_Payments_Invoice_Service
*
* @package WooCommerce\Payments
*/
use WCPay\Exceptions\API_Exception;
use WCPay\Exceptions\Rest_Request_Exception;
use WCPay\Logger;
defined( 'ABSPATH' ) || exit;
/**
* Class handling any subscription invoice functionality.
*/
class WC_Payments_Invoice_Service {
/**
* Subscription meta key used to store subscription's last invoice ID.
*
* @const string
*/
const PENDING_INVOICE_ID_KEY = '_wcpay_pending_invoice_id';
/**
* Meta key used to store invoice IDs on orders.
*
* @const
*/
const ORDER_INVOICE_ID_KEY = '_wcpay_billing_invoice_id';
/**
* Client for making requests to the WooCommerce Payments API.
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;
/**
* Product Service
*
* @var WC_Payments_Product_Service
*/
private $product_service;
/**
* Constructor.
*
* @param WC_Payments_API_Client $payments_api_client WooCommerce Payments API client.
* @param WC_Payments_Product_Service $product_service Product Service.
* @param WC_Payment_Gateway_WCPay $gateway WC payments Payment Gateway.
*/
public function __construct(
WC_Payments_API_Client $payments_api_client,
WC_Payments_Product_Service $product_service,
WC_Payment_Gateway_WCPay $gateway
) {
$this->payments_api_client = $payments_api_client;
$this->product_service = $product_service;
$this->gateway = $gateway;
add_action( 'woocommerce_order_payment_status_changed', [ $this, 'maybe_record_invoice_payment' ], 10, 1 );
add_action( 'woocommerce_renewal_order_payment_complete', [ $this, 'maybe_record_invoice_payment' ], 11, 1 );
}
/**
* Gets the subscription last invoice ID from WC subscription.
*
* @param WC_Subscription $subscription The subscription.
*
* @return string Invoice ID.
*/
public static function get_pending_invoice_id( $subscription ) : string {
return $subscription->get_meta( self::PENDING_INVOICE_ID_KEY, true );
}
/**
* Gets the invoice ID from a WC order.
*
* @param WC_Order $order The order.
* @return string Invoice ID.
*/
public static function get_order_invoice_id( WC_Order $order ) : string {
return $order->get_meta( self::ORDER_INVOICE_ID_KEY, true );
}
/**
* Gets the invoice ID from a WC subscription.
*
* @param WC_Subscription $subscription The subscription.
*
* @return string Invoice ID.
*/
public static function get_subscription_invoice_id( $subscription ) {
return $subscription->get_meta( self::ORDER_INVOICE_ID_KEY, true );
}
/**
* Gets the WC order ID from the invoice ID.
*
* @param string $invoice_id The invoice ID.
* @return int The order ID.
*/
public static function get_order_id_by_invoice_id( string $invoice_id ) {
$query_args = [
'status' => 'any',
'type' => 'shop_order',
'limit' => 1,
'return' => 'ids',
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
[
'key' => self::ORDER_INVOICE_ID_KEY,
'value' => $invoice_id,
],
],
];
// On HPOS environments we can pass meta_query directly to get_orders() as WC doesn't override it.
if ( WC_Payments_Utils::is_hpos_tables_usage_enabled() ) {
$order_ids = wc_get_orders( $query_args );
} else {
$meta_query = $query_args['meta_query'];
unset( $query_args['meta_query'] );
$add_meta_query = function ( $query ) use ( $meta_query ) {
$query['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
return $query;
};
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', $add_meta_query, 10, 2 );
$order_ids = wc_get_orders( $query_args );
remove_filter( 'woocommerce_order_data_store_cpt_get_orders_query', $add_meta_query, 10 );
}
return (int) array_shift( $order_ids );
}
/**
* Sets a pending invoice ID meta for a subscription.
*
* @param WC_Subscription $subscription The subscription to set the invoice on.
* @param string $invoice_id The invoice ID.
*/
public function mark_pending_invoice_for_subscription( WC_Subscription $subscription, string $invoice_id ) {
$this->set_pending_invoice_id( $subscription, $invoice_id );
}
/**
* Removes pending invoice id meta from subscription.
*
* @param WC_Subscription $subscription The Subscription.
*/
public function mark_pending_invoice_paid_for_subscription( WC_Subscription $subscription ) {
$this->set_pending_invoice_id( $subscription, '' );
}
/**
* Marks a subscription invoice as paid after a parent or renewal order is completed.
*
* When a subscription's parent order goes from a pending payment status to a payment completed status,
* or when a subscription with no corresponding Stripe subscription is manually renewed,
* make sure the invoice is marked as paid (without charging the customer since it was charged on checkout).
*
* @param int $order_id The WC order ID.
* @throws API_Exception If the request to mark the invoice as paid fails.
*/
public function maybe_record_invoice_payment( int $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || self::get_order_invoice_id( $order ) ) {
return;
}
foreach ( wcs_get_subscriptions_for_order( $order, [ 'order_type' => [ 'parent', 'renewal' ] ] ) as $subscription ) {
$invoice_id = self::get_subscription_invoice_id( $subscription );
if ( ! $invoice_id ) {
continue;
}
try {
// Set the invoice status to paid but don't charge the customer by using paid_out_of_band parameter.
$this->payments_api_client->charge_invoice( $invoice_id, [ 'paid_out_of_band' => 'true' ] );
} catch ( API_Exception $e ) {
// If the invoice was already paid, silently handle that error. Throw all other exceptions.
if ( WP_Http::BAD_REQUEST === $e->get_http_code() && false !== stripos( $e->getMessage(), 'invoice is already paid' ) ) {
Logger::info( sprintf( 'Invoice for subscription #%s has already been paid.', $subscription->get_id() ) );
} else {
throw $e;
}
}
if ( $subscription->is_manual() ) {
$subscription->set_requires_manual_renewal( false );
$subscription->set_payment_method( WC_Payment_Gateway_WCPay::GATEWAY_ID );
// Copy the payment token used to pay for the order to the subscription.
WC_Payments::get_gateway()->update_failing_payment_method( $subscription, $order );
$subscription->save();
}
}
}
/**
* Validates a WCPay invoice.
*
* @param array $wcpay_item_data The WCPay invoice items.
* @param array $wcpay_discount_data The WCPay invoice discounts.
* @param WC_Subscription $subscription The WC Subscription object.
*
* @throws API_Exception If updating the WCPay subscription or items fails.
* @throws Rest_Request_Exception If WCPay invoice items do not match WC subscription items.
*/
public function validate_invoice( array $wcpay_item_data, array $wcpay_discount_data, WC_Subscription $subscription ) {
$item_data = $this->get_repair_data_for_wcpay_items( $wcpay_item_data, $subscription );
if ( ! empty( $item_data ) ) {
foreach ( $item_data as $id => $data ) {
$this->payments_api_client->update_subscription_item( $id, $data );
}
}
$discount_data = $this->get_repair_data_for_wcpay_discounts( $wcpay_discount_data, $subscription );
if ( isset( $discount_data ) ) {
$response = $this->payments_api_client->update_subscription(
WC_Payments_Subscription_Service::get_wcpay_subscription_id( $subscription ),
[ 'discounts' => $discount_data ]
);
WC_Payments_Subscription_Service::set_wcpay_discount_ids( $subscription, $response['discounts'] );
}
}
/**
* Sets the subscription's last invoice ID meta for WC subscription.
*
* @param WC_Order $order The order.
* @param string $invoice_id The invoice ID.
*/
public function set_order_invoice_id( WC_Order $order, string $invoice_id ) {
$order->update_meta_data( self::ORDER_INVOICE_ID_KEY, $invoice_id );
$order->save();
}
/**
* Sets the subscription's last invoice ID meta for WC subscription.
*
* @param WC_Subscription $subscription The subscription.
* @param string $parent_invoice_id The parent order invoice ID.
*/
public function set_subscription_invoice_id( WC_Subscription $subscription, string $parent_invoice_id ) {
$subscription->update_meta_data( self::ORDER_INVOICE_ID_KEY, $parent_invoice_id );
$subscription->save();
}
/**
* Retrieves the intent object and adds its data to the order.
*
* @param WC_Order $order The order to update.
* @param string $intent_id The intent ID.
*/
public function get_and_attach_intent_info_to_order( $order, $intent_id ) {
try {
$intent_object = $this->payments_api_client->get_intent( $intent_id );
} catch ( API_Exception $e ) {
$order->add_order_note( __( 'The payment info couldn\'t be added to the order.', 'woocommerce-payments' ) );
return;
}
$charge = $intent_object->get_charge();
$this->gateway->attach_intent_info_to_order(
$order,
$intent_id,
$intent_object->get_status(),
$intent_object->get_payment_method_id(),
$intent_object->get_customer_id(),
$charge ? $charge->get_id() : null,
$intent_object->get_currency()
);
}
/**
* Sets the subscription last invoice ID meta for WC subscription.
*
* @param WC_Subscription $subscription The subscription.
* @param string $invoice_id The invoice ID.
*/
private function set_pending_invoice_id( $subscription, string $invoice_id ) {
$subscription->update_meta_data( self::PENDING_INVOICE_ID_KEY, $invoice_id );
$subscription->save();
}
/**
* Gets repair data for WCPay invoice items.
*
* @param array $wcpay_item_data The WCPay invoice items.
* @param WC_Subscription $subscription The WC Subscription object.
*
* @return array Repair data.
*
* @throws Rest_Request_Exception WCPay invoice items do not match WC subscription items.
*/
private function get_repair_data_for_wcpay_items( array $wcpay_item_data, WC_Subscription $subscription ) : array {
$repair_data = [];
$wcpay_items = [];
$subscription_items = $subscription->get_items( [ 'line_item', 'fee', 'shipping', 'tax' ] );
foreach ( $wcpay_item_data as $item ) {
$wcpay_subscription_item_id = $item['subscription_item'];
$wcpay_items[ $wcpay_subscription_item_id ] = [
'unit_amount' => $item['price']['unit_amount_decimal'],
'billing_period' => $item['price']['recurring']['interval'],
'billing_interval' => $item['price']['recurring']['interval_count'],
'currency' => $item['price']['currency'],
'quantity' => $item['quantity'],
];
}
// Generate any repair data necessary to update the WCPay Subscription so it matches the WC subscription.
foreach ( WC_Payments_Subscriptions::get_subscription_service()->get_recurring_item_data_for_subscription( $subscription ) as $recurring_item_data ) {
$item_id = $recurring_item_data['metadata']['wc_item_id'];
$item = $subscription_items[ $item_id ];
$wcpay_item_id = WC_Payments_Subscription_Service::get_wcpay_subscription_item_id( $item );
if ( ! isset( $wcpay_items[ $wcpay_item_id ] ) ) {
$message = __( 'The WCPay invoice items do not match WC subscription items.', 'woocommerce-payments' );
Logger::error( $message );
throw new Rest_Request_Exception( $message );
}
// Check the quantity matches between WC and WCPay.
if ( $item->is_type( 'line_item' ) && $wcpay_items[ $wcpay_item_id ]['quantity'] !== $recurring_item_data['quantity'] ) {
$repair_data[ $wcpay_item_id ]['quantity'] = $recurring_item_data['quantity'];
}
// Confirm the line item amount matches between WC and WCPay.
$unit_amounts_match = (string) $wcpay_items[ $wcpay_item_id ]['unit_amount'] === (string) $recurring_item_data['price_data']['unit_amount_decimal'];
if ( ! $unit_amounts_match ) {
$price_data = $recurring_item_data['price_data'];
// We need to maintain the WCPay subscription's billing terms and currency. ie We cannot update the recurring period, interval or currency mid term.
$price_data['currency'] = $wcpay_items[ $wcpay_item_id ]['currency'];
$price_data['recurring'] = [
'interval' => $wcpay_items[ $wcpay_item_id ]['billing_period'],
'interval_count' => $wcpay_items[ $wcpay_item_id ]['billing_interval'],
];
$repair_data[ $wcpay_item_id ]['price_data'] = $price_data;
}
}
return $repair_data;
}
/**
* Gets repair data for WCPay invoice discounts.
*
* @param array $wcpay_discount_data The WCPay discounts.
* @param WC_Subscription $subscription The WC Subscription object.
*
* @return mixed
*/
private function get_repair_data_for_wcpay_discounts( array $wcpay_discount_data, WC_Subscription $subscription ) {
$repair_data = null;
$subscription_discount_ids = WC_Payments_Subscription_Service::get_wcpay_discount_ids( $subscription );
if ( ! empty( $subscription_discount_ids ) || ! empty( $wcpay_discount_data ) ) {
if ( count( $subscription_discount_ids ) !== count( $wcpay_discount_data ) ) {
$repair_data = WC_Payments_Subscription_Service::get_discount_item_data_for_subscription( $subscription );
} else {
foreach ( $subscription_discount_ids as $discount_id ) {
if ( ! in_array( $discount_id, $wcpay_discount_data, true ) ) {
$repair_data = WC_Payments_Subscription_Service::get_discount_item_data_for_subscription( $subscription );
break;
}
}
}
}
return $repair_data;
}
}