class-wc-payments-action-scheduler-service.php
4.8 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
<?php
/**
* WC_Payments_Action_Scheduler_Service class
*
* @package WooCommerce\Payments
*/
defined( 'ABSPATH' ) || exit;
/**
* Class which handles setting up all ActionScheduler hooks.
*/
class WC_Payments_Action_Scheduler_Service {
const GROUP_ID = 'woocommerce_payments';
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;
/**
* Constructor for WC_Payments_Action_Scheduler_Service.
*
* @param WC_Payments_API_Client $payments_api_client - WooCommerce Payments API client.
*/
public function __construct(
WC_Payments_API_Client $payments_api_client
) {
$this->payments_api_client = $payments_api_client;
$this->add_action_scheduler_hooks();
}
/**
* Attach hooks for all ActionScheduler actions.
*
* @return void
*/
public function add_action_scheduler_hooks() {
add_action( 'wcpay_track_new_order', [ $this, 'track_new_order_action' ] );
add_action( 'wcpay_track_update_order', [ $this, 'track_update_order_action' ] );
}
/**
* This function is a hook that will be called by ActionScheduler when an order is created.
* It will make a request to the Payments API to track this event.
*
* @param array $order_id The ID of the order that has been created.
*
* @return bool
*/
public function track_new_order_action( $order_id ) {
return $this->track_order( $order_id, false );
}
/**
* This function is a hook that will be called by ActionScheduler when an order is updated.
* It will make a request to the Payments API to track this event.
*
* @param int $order_id The ID of the order which has been updated.
*
* @return bool
*/
public function track_update_order_action( $order_id ) {
return $this->track_order( $order_id, true );
}
/**
* Track an order by making a request to the Payments API.
*
* @param mixed $order_id The ID of the order which has been updated/created.
* @param bool $is_update Is this an update event. If false, it is assumed this is a creation event.
*
* @return bool
*/
private function track_order( $order_id, $is_update = false ) {
// Get the order details.
$order = wc_get_order( $order_id );
if ( ! $order ) {
return false;
}
// If we do not have a valid payment method for this order, don't send the request.
$payment_method = $order->get_meta( ( '_payment_method_id' ) );
if ( empty( $payment_method ) ) {
return false;
}
$order_mode = $order->get_meta( '_wcpay_mode' );
if ( $order_mode ) {
$current_mode = $this->payments_api_client->is_in_test_mode() ? 'test' : 'prod';
if ( $current_mode !== $order_mode ) {
// If mode doesn't match make sure to stop order tracking to prevent order tracking issues.
// False will be returned so maybe future crons will have correct mode.
return false;
}
}
// Send the order data to the Payments API to track it.
$response = $this->payments_api_client->track_order(
array_merge(
$order->get_data(),
[
'_payment_method_id' => $payment_method,
'_stripe_customer_id' => $order->get_meta( '_stripe_customer_id' ),
'_wcpay_mode' => $order_mode,
]
),
$is_update
);
if ( 'success' === $response['result'] && ! $is_update ) {
// Update the metadata to reflect that the order creation event has been fired.
$order->add_meta_data( '_new_order_tracking_complete', 'yes' );
$order->save_meta_data();
}
return ( 'success' === ( $response['result'] ?? null ) );
}
/**
* Schedule an action scheduler job. Also unschedules (replaces) any previous instances of the same job.
* This prevents duplicate jobs, for example when multiple events fire as part of the order update process.
* The `as_unschedule_action` function will only replace a job which has the same $hook, $args AND $group.
*
* @param int $timestamp - When the job will run.
* @param string $hook - The hook to trigger.
* @param array $args - An array containing the arguments to be passed to the hook.
* @param string $group - The AS group the action will be created under.
*
* @return void
*/
public function schedule_job( int $timestamp, string $hook, array $args = [], string $group = self::GROUP_ID ) {
// Unschedule any previously scheduled instances of this particular job.
as_unschedule_action( $hook, $args, $group );
// Schedule the job.
as_schedule_single_action( $timestamp, $hook, $args, $group );
}
/**
* Checks to see if there is a Pending action with the same hook already.
*
* @param string $hook Hook name.
*
* @return bool
*/
public function pending_action_exists( string $hook ): bool {
$actions = as_get_scheduled_actions(
[
'hook' => $hook,
'status' => ActionScheduler_Store::STATUS_PENDING,
'group' => self::GROUP_ID,
]
);
return count( $actions ) > 0;
}
}