1
1
<?php
2
2
3
- use EventEspresso \PaymentMethods \ PayPalCommerce \ domain \ Domain ;
4
- use EventEspresso \PaymentMethods \ PayPalCommerce \ tools \ extra_meta \ PayPalExtraMetaManager ;
3
+ use EventEspresso \core \ services \ loaders \ LoaderFactory ;
4
+ use EventEspresso \core \ services \ request \ RequestInterface ;
5
5
use EventEspresso \PaymentMethods \PayPalCommerce \tools \logging \PayPalLogger ;
6
6
7
7
/**
@@ -53,56 +53,99 @@ class EEG_PayPalCheckout extends EE_Onsite_Gateway
53
53
* @param EE_Payment|null $payment
54
54
* @param array|null $billing_info
55
55
* @return EE_Payment
56
+ * @throws EE_Error|ReflectionException
56
57
*/
57
58
public function do_direct_payment ($ payment , $ billing_info = null )
58
59
{
59
- // Normally we shouldn't be getting here because the payment should have been processed
60
- // along with the PP Order Charge.
61
- return $ payment ;
60
+ $ request = LoaderFactory::getLoader ()->getShared (RequestInterface::class);
61
+ $ post_parameters = $ request ->postParams ();
62
+ // Check the payment.
63
+ $ payment = $ this ->validatePayment ($ payment , $ request );
64
+ if ($ payment ->details () === 'error ' && $ payment ->status () === EEM_Payment::status_id_failed) {
65
+ return $ payment ;
66
+ }
67
+ $ transaction = $ payment ->transaction ();
68
+ $ payment_method = $ transaction ->payment_method ();
69
+ // Get the order details.
70
+ $ order_id = $ request ->getRequestParam ('pp_order_id ' );
71
+ if (! $ order_id ) {
72
+ return EEG_PayPalCheckout::updatePaymentStatus (
73
+ $ payment ,
74
+ EEM_Payment::status_id_failed,
75
+ $ post_parameters ,
76
+ esc_html__ ('Can \'t charge the Order. The Order ID is missing. ' , 'event_espresso ' )
77
+ );
78
+ }
79
+ // Capture the order.
80
+ $ capture_status = EED_PayPalCommerce::captureOrder ($ transaction , $ payment_method , $ order_id );
81
+ // Check the order status.
82
+ $ order_status = $ this ->isOrderCompleted ($ order_id , $ transaction , $ payment_method );
83
+ if (! $ order_status ['completed ' ]) {
84
+ return EEG_PayPalCheckout::updatePaymentStatus (
85
+ $ payment ,
86
+ EEM_Payment::status_id_failed,
87
+ $ order_status ,
88
+ $ order_status ['message ' ] ?? ''
89
+ );
90
+ }
91
+ // Looks like all is good. Mark payment as a success.
92
+ $ this ->saveBillingDetails ($ payment , $ transaction , $ order_status ['details ' ], $ billing_info );
93
+ return EEG_PayPalCheckout::updatePaymentStatus ($ payment , EEM_Payment::status_id_approved, $ capture_status );
62
94
}
63
95
64
96
65
97
/**
66
98
* Validate the Order.
67
99
*
68
- * @param $order
69
- * @param null $provided_order_id
100
+ * @param string $order_id
101
+ * @param EE_Transaction $transaction
102
+ * @param EE_Payment_Method $payment_method
70
103
* @return array ['completed' => {boolean}, 'message' => {string}]
104
+ * @throws EE_Error
105
+ * @throws ReflectionException
71
106
*/
72
- public static function isOrderCompleted ($ order , $ provided_order_id = null ): array
107
+ public static function isOrderCompleted (
108
+ string $ order_id ,
109
+ EE_Transaction $ transaction ,
110
+ EE_Payment_Method $ payment_method
111
+ ): array
73
112
{
113
+ $ order_details = EED_PayPalCommerce::getOrderDetails ($ order_id , $ transaction , $ payment_method );
74
114
$ conclusion = [
75
115
'completed ' => false ,
76
- 'message ' => esc_html__ ( ' Could not validate this Order. ' , ' event_espresso ' ) ,
116
+ 'details ' => $ order_details ,
77
117
];
78
- if (! empty ($ order ) && ! empty ($ provided_order_id ) && $ order ['id ' ] !== $ provided_order_id ) {
79
- $ conclusion ['message ' ] = esc_html__ ('Order ID mismatch. ' , 'event_espresso ' );
80
- }
81
- if (! $ order || ! is_array ($ order )) {
82
- $ conclusion ['message ' ] = esc_html__ ('The Order data is incorrectly formatted. ' , 'event_espresso ' );
83
- } elseif (empty ($ order ['status ' ])) {
118
+ if (! $ order_details ) {
119
+ $ conclusion ['message ' ] = esc_html__ (
120
+ 'Could not validate this payment. The Order details were empty. ' ,
121
+ 'event_espresso '
122
+ );
123
+ } elseif (! empty ($ order_details ['error ' ])) {
124
+ $ conclusion ['message ' ] = $ order_details ['message ' ] ?? $ order_details ['error ' ];
125
+ } elseif (empty ($ order_details ['status ' ])) {
84
126
$ conclusion ['message ' ] = esc_html__ (
85
127
'There was an error with this payment. The status of the Order could not be determined. ' ,
86
128
'event_espresso '
87
129
);
88
- } elseif ($ order ['status ' ] !== 'COMPLETED ' ) {
130
+ } elseif ($ order_details ['status ' ] !== 'COMPLETED ' ) {
89
131
$ conclusion ['message ' ] = esc_html__ (
90
132
'There was an error with this payment. Order was not approved. ' ,
91
133
'event_espresso '
92
134
);
93
- } elseif (empty ($ order ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['status ' ])) {
135
+ } elseif (empty ($ order_details ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['status ' ])) {
94
136
$ conclusion ['message ' ] = esc_html__ (
95
137
'There was an error with this payment. The status of the Payment could not be determined. ' ,
96
138
'event_espresso '
97
139
);
98
- } elseif ($ order ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['status ' ] !== 'COMPLETED ' ) {
140
+ } elseif ($ order_details ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['status ' ] !== 'COMPLETED ' ) {
99
141
$ conclusion ['message ' ] = esc_html__ (
100
142
'This payment was declined or failed validation. Please check the billing information you provided. ' ,
101
143
'event_espresso '
102
144
);
103
145
} else {
104
146
// If we didn't fail on the above, the Order should be considered valid.
105
147
$ conclusion ['completed ' ] = true ;
148
+ $ conclusion ['message ' ] = esc_html__ ('Order Valid. ' , 'event_espresso ' );
106
149
}
107
150
return $ conclusion ;
108
151
}
@@ -161,7 +204,7 @@ public static function updatePaymentStatus(
161
204
$ payment ->set_amount ((float ) $ amount );
162
205
}
163
206
$ payment ->set_txn_id_chq_nmbr (
164
- $ order ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['id ' ] ?? $ response_data ['id ' ]
207
+ $ response_data ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['id ' ] ?? $ response_data ['id ' ]
165
208
);
166
209
} else {
167
210
$ default_message = sprintf (
@@ -183,6 +226,47 @@ public static function updatePaymentStatus(
183
226
}
184
227
185
228
229
+ /**
230
+ * Validate the payment.
231
+ *
232
+ * @param EE_Payment|null $payment
233
+ * @param RequestInterface $request
234
+ * @return EE_Payment
235
+ * @throws EE_Error
236
+ * @throws ReflectionException
237
+ */
238
+ public function validatePayment (?EE_Payment $ payment , RequestInterface $ request ): EE_Payment
239
+ {
240
+ $ failed_status = $ this ->_pay_model ->failed_status ();
241
+ // Check the payment.
242
+ if (! $ payment instanceof EE_Payment) {
243
+ $ payment = EE_Payment::new_instance ();
244
+ $ error_message = esc_html__ ('Error. No associated payment was found. ' , 'event_espresso ' );
245
+ return EEG_PayPalCheckout::updatePaymentStatus (
246
+ $ payment ,
247
+ $ failed_status ,
248
+ $ request ->postParams (),
249
+ $ error_message
250
+ );
251
+ }
252
+ // Check the transaction.
253
+ $ transaction = $ payment ->transaction ();
254
+ if (! $ transaction instanceof EE_Transaction) {
255
+ $ error_message = esc_html__ (
256
+ 'Could not process this payment because it has no associated transaction. ' ,
257
+ 'event_espresso '
258
+ );
259
+ return EEG_PayPalCheckout::updatePaymentStatus (
260
+ $ payment ,
261
+ $ failed_status ,
262
+ $ request ->postParams (),
263
+ $ error_message
264
+ );
265
+ }
266
+ return $ payment ;
267
+ }
268
+
269
+
186
270
/**
187
271
* Save some transaction details, like billing information.
188
272
*
@@ -203,7 +287,7 @@ public static function saveBillingDetails(
203
287
$ primary_reg = $ transaction ->primary_registration ();
204
288
$ attendee = $ primary_reg instanceof EE_Registration ? $ primary_reg ->attendee () : null ;
205
289
$ payment_method = $ transaction ->payment_method ();
206
- $ postmeta_name = $ payment_method ->type_obj () instanceof EE_PMT_Base
290
+ $ post_meta_name = $ payment_method ->type_obj () instanceof EE_PMT_Base
207
291
? 'billing_info_ ' . $ payment_method ->type_obj ()->system_name ()
208
292
: '' ;
209
293
if (empty ($ order ['payment_source ' ]) || ! $ attendee instanceof EE_Attendee) {
@@ -218,8 +302,8 @@ public static function saveBillingDetails(
218
302
if (! empty ($ billing_info ['address ' ])) {
219
303
$ attendee ->set_address ($ billing_info ['address ' ]);
220
304
}
221
- if (! empty ($ billing_info ['address_2 ' ])) {
222
- $ attendee ->set_address2 ($ billing_info ['address_2 ' ]);
305
+ if (! empty ($ billing_info ['address2 ' ])) {
306
+ $ attendee ->set_address2 ($ billing_info ['address2 ' ]);
223
307
}
224
308
if (! empty ($ billing_info ['city ' ])) {
225
309
$ attendee ->set_city ($ billing_info ['city ' ]);
@@ -233,7 +317,8 @@ public static function saveBillingDetails(
233
317
if (! empty ($ billing_info ['zip ' ])) {
234
318
$ attendee ->set_zip ($ billing_info ['zip ' ]);
235
319
}
236
- // Or card information from ACDC ?
320
+ // Or card information from advanced card fields ?
321
+ $ billing_info ['credit_card ' ] = '' ;
237
322
if (! empty ($ order ['payment_source ' ]['card ' ])) {
238
323
$ payer_card = $ order ['payment_source ' ]['card ' ];
239
324
if (! empty ($ payer_card ['name ' ])) {
@@ -244,7 +329,8 @@ public static function saveBillingDetails(
244
329
$ billing_info ['last_name ' ] = $ full_name [1 ] ?? $ attendee ->lname ();
245
330
}
246
331
}
247
- update_post_meta ($ attendee ->ID (), $ postmeta_name , $ billing_info );
332
+ // Update attendee billing info in the transaction details.
333
+ update_post_meta ($ attendee ->ID (), $ post_meta_name , $ billing_info );
248
334
$ attendee ->save ();
249
335
}
250
336
}
0 commit comments