Skip to content

Commit 69609bb

Browse files
authored
Merge pull request #89 from p-maguire/6.0.x
SP-1066: Resolve conflicts with master for 6.0.x
2 parents f9914e5 + eed838b commit 69609bb

7 files changed

+103
-22
lines changed

BitPayLib/class-bitpayipnprocess.php

+60-18
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public function __construct(
3838
}
3939

4040
public function execute( WP_REST_Request $request ): void {
41-
$data = $request->get_body();
42-
43-
$data = json_decode( $data, true, 512, JSON_THROW_ON_ERROR );
44-
$event = $data['event'] ?? null;
45-
$data = $data['data'] ?? null;
46-
$invoice_id = $data['id'] ?? null;
41+
$data = $request->get_body();
42+
$data = json_decode( $data, true, 512, JSON_THROW_ON_ERROR );
43+
$event = $data['event'] ?? null;
44+
$data = $data['data'] ?? null;
45+
$data['event'] = $event;
46+
$data['requestDate'] = date( 'Y-m-d H:i:s' );
47+
$invoice_id = $data['id'] ?? null;
4748

4849
$this->logger->execute( $data, 'INCOMING IPN', true );
4950
if ( ! $event || ! $data || ! $invoice_id ) {
@@ -142,28 +143,28 @@ private function get_bitpay_dashboard_link( string $invoice_id ): string {
142143
}
143144

144145
private function process_confirmed( Invoice $bitpay_invoice, WC_Order $order ): void {
145-
$this->validate_bitpay_status_in_available_statuses( $bitpay_invoice, array( 'confirmed' ) );
146+
$this->validate_bitpay_status_in_available_statuses( $bitpay_invoice, array( 'confirmed', 'complete' ) );
146147

147148
$invoice_id = $bitpay_invoice->getId();
148149
$wordpress_order_status = $this->bitpay_wordpress_helper
149150
->get_bitpay_gateway_option( 'bitpay_checkout_order_process_confirmed_status' );
150151
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $wordpress_order_status ) {
151152
$order->add_order_note(
152-
$this->get_start_order_note( $invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings.'
153+
$this->get_start_order_note( $invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings.'
153154
);
154155
return;
155156
}
156157

157-
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'Processing';
158+
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? null;
158159
if ( ! $new_status ) {
159160
$new_status = 'Processing';
160-
$wordpress_order_status = 'wc-pending';
161+
$wordpress_order_status = 'pending';
161162
}
162163

163164
$order->add_order_note(
164165
$this->get_start_order_note( $invoice_id ) . 'has changed to ' . $new_status . '.'
165166
);
166-
if ( 'wc-completed' === $wordpress_order_status ) {
167+
if ( 'wc-completed' === $wordpress_order_status ) { // statuses with 'wc' prefix.
167168
$order->payment_complete();
168169
$order->add_order_note( 'Payment Completed' );
169170
} else {
@@ -174,26 +175,32 @@ private function process_confirmed( Invoice $bitpay_invoice, WC_Order $order ):
174175

175176
private function process_completed( Invoice $bitpay_invoice, WC_Order $order ): void {
176177
$this->validate_bitpay_status_in_available_statuses( $bitpay_invoice, array( 'complete' ) );
178+
$wc_order_status = $order->get_status();
177179

178180
$invoice_id = $bitpay_invoice->getId();
179181
$wordpress_order_status = $this->bitpay_wordpress_helper
180182
->get_bitpay_gateway_option( 'bitpay_checkout_order_process_complete_status' );
181183
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $wordpress_order_status ) {
182184
$order->add_order_note(
183185
$this->get_start_order_note( $invoice_id )
184-
. 'has changed to Complete. The order status has not been updated due to your settings.'
186+
. 'has changed to Complete. The order status has not been updated due to your settings.'
185187
);
186188
return;
187189
}
188190

189-
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'Processing';
191+
if ( ! $this->should_process_completed_action( $wc_order_status, $wordpress_order_status ) ) {
192+
return;
193+
}
194+
195+
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? null;
196+
$new_status = apply_filters( 'bitpay_checkout_order_process_complete_status', $new_status, $wordpress_order_status );
190197
if ( ! $new_status ) {
191198
$new_status = 'Processing';
192-
$wordpress_order_status = 'wc-pending';
199+
$wordpress_order_status = 'pending';
193200
}
194201

195202
$order->add_order_note( $this->get_start_order_note( $invoice_id ) . 'has changed to ' . $new_status . '.' );
196-
if ( 'wc-completed' === $wordpress_order_status ) {
203+
if ( 'wc-completed' === $wordpress_order_status ) { // statuses with 'wc' prefix.
197204
$order->payment_complete();
198205
$order->add_order_note( 'Payment Completed' );
199206
} else {
@@ -244,8 +251,7 @@ private function process_abandoned( Invoice $bitpay_invoice, WC_Order $order ):
244251

245252
$invoice_id = $bitpay_invoice->getId();
246253
if ( $underpaid_amount ) {
247-
$order->add_order_note( $this->get_start_order_note( $invoice_id ) . $underpaid_amount . ' has been refunded.' );
248-
$order->update_status( 'refunded', __( 'BitPay payment refunded', 'woocommerce' ) );
254+
$this->process_refunded( $bitpay_invoice, $order );
249255
return;
250256
}
251257

@@ -258,12 +264,20 @@ private function process_abandoned( Invoice $bitpay_invoice, WC_Order $order ):
258264
}
259265

260266
private function process_refunded( Invoice $bitpay_invoice, WC_Order $order ): void {
267+
if ( ! $this->should_process_refund() ) {
268+
$order->add_order_note(
269+
$this->get_start_order_note( $bitpay_invoice->getId() )
270+
. 'has changed to Refunded. The order status has not been updated due to your settings.'
271+
);
272+
return;
273+
}
274+
261275
$order->add_order_note( $this->get_start_order_note( $bitpay_invoice->getId() ) . 'has been refunded.' );
262276
$order->update_status( 'refunded', __( 'BitPay payment refunded', 'woocommerce' ) );
263277
}
264278

265279
private function process_processing( Invoice $bitpay_invoice, WC_Order $order ): void {
266-
$this->validate_bitpay_status_in_available_statuses( $bitpay_invoice, array( 'paid' ) );
280+
$this->validate_bitpay_status_in_available_statuses( $bitpay_invoice, array( 'paid', 'confirmed', 'complete' ) );
267281
$invoice_id = $bitpay_invoice->getId();
268282
$order->add_order_note( $this->get_start_order_note( $invoice_id ) . 'is paid and awaiting confirmation.' );
269283

@@ -280,4 +294,32 @@ private function process_processing( Invoice $bitpay_invoice, WC_Order $order ):
280294
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'processing';
281295
$order->update_status( $new_status, __( 'BitPay payment processing', 'woocommerce' ) );
282296
}
297+
298+
private function has_final_status( WC_Order $order ): bool {
299+
return \in_array( $order->get_status(), self::FINAL_WC_ORDER_STATUSES, true );
300+
}
301+
302+
/**
303+
* We don't want to change complete order to process.
304+
*
305+
* @param string $wc_order_status WC order status.
306+
* @param string|null $wordpress_order_status_from_settings status to event from BitPay settings.
307+
* @return bool
308+
*/
309+
private function should_process_completed_action( string $wc_order_status, ?string $wordpress_order_status_from_settings ): bool {
310+
if ( 'completed' !== $wc_order_status ) {
311+
return true;
312+
}
313+
314+
if ( 'pending' === $wordpress_order_status_from_settings || 'processing' === $wordpress_order_status_from_settings ) {
315+
return false;
316+
}
317+
318+
return true;
319+
}
320+
321+
private function should_process_refund(): bool {
322+
$should_process_refund_status = $this->get_wc_order_statuses()['bitpay_checkout_order_process_refund'] ?? '1';
323+
return '1' === $should_process_refund_status;
324+
}
283325
}

BitPayLib/class-bitpaypages.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function checkout_thank_you( int $order_id ): void {
3838

3939
$invoice_id = $_COOKIE[BitPayPluginSetup::COOKIE_INVOICE_ID_NAME] ?? null; // phpcs:ignore
4040

41-
wp_enqueue_script( 'remote-bitpay-js', $js_script, null, null, false ); // phpcs:ignore
41+
wp_enqueue_script('remote-bitpay-js', $js_script, null, null, false ); // phpcs:ignore
4242
wp_enqueue_script('bitpay_thank_you', plugins_url( '../../js/bitpay_thank_you.js', __FILE__ ), null, BitPayPluginSetup::VERSION, false ); // phpcs:ignore
4343
?>
4444
<script type="text/javascript">

BitPayLib/class-bitpaypaymentsettings.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function check_token(): void {
8888
}
8989

9090
$bitpay_checkout_token = $this->get_bitpay_token();
91-
$bitpay_checkout_endpoint = $this->get_bitpay_gateway_setting( 'bitpay_checkout_endpoint' );
91+
$bitpay_checkout_endpoint = $this->get_bitpay_checkout_endpoint();
9292

9393
if ( ! $bitpay_checkout_token ) {
9494
$message = 'There is no token set for your ' . strtoupper( $bitpay_checkout_endpoint ) . ' environment. BitPay will not function if this is not set.';
@@ -163,4 +163,9 @@ private function get_bitpay_gateway_setting( string $setting_name, $default_valu
163163
private function get_bitpay_gateway_settings(): array {
164164
return get_option( 'woocommerce_bitpay_checkout_gateway_settings', array() );
165165
}
166+
167+
private function get_bitpay_checkout_endpoint(): string {
168+
// 'test' as default when we don't store options yet (before save configuration)
169+
return $this->get_bitpay_gateway_setting( 'bitpay_checkout_endpoint' ) ?? 'test';
170+
}
166171
}

BitPayLib/class-wcgatewaybitpay.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ public function init_form_fields() {
259259
'options' => $wc_statuses_arr,
260260
'default' => 'wc-processing',
261261
),
262+
'bitpay_checkout_order_process_refund' => array(
263+
'title' => __( 'BitPay Process Refund Status', 'woocommerce' ),
264+
'type' => 'select',
265+
'description' => __( 'If set to <b>Yes</b>, automatically set the order to "refunded" when the invoice has a "refund_success" status, as notified by the BitPay IPN.', 'woocommerce' ),
266+
'options' => array(
267+
'0' => 'No',
268+
'1' => 'Yes',
269+
),
270+
'default' => '1',
271+
),
262272
'bitpay_checkout_order_expired_status' => array(
263273
'title' => __( 'BitPay Expired Status', 'woocommerce' ),
264274
'type' => 'select',
@@ -299,7 +309,7 @@ public function process_payment( $order_id ) {
299309
private function get_icon_on_payment_page(): string {
300310
$settings = new BitPayPaymentSettings();
301311

302-
return $settings->get_payment_logo_url() . '" id="bitpay_logo';
312+
return add_query_arg( 'id', 'bitpay_logo', $settings->get_payment_logo_url() );
303313
}
304314

305315
private function get_processing_link(): string {

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
* Fixed a bug when the IPN/webhook is received with "complete" status
77
* Add Unit & End2End tests
88

9+
# 5.5.1
10+
* Fixed issue with payment logo url
11+
12+
# 5.5.0
13+
* Tested compatibility with WordPress 6.5.2
14+
15+
# 5.4.1
16+
* Improved webhook validation to improve timing issues
17+
918
# 5.4.0
1019
* Added compatibility with Checkout Blocks
1120
* Fixed Checkout Flow (BitPay Modal)
1221
* Tested compatibility with WordPress 6.4.2
22+
* Fixed issue with exception for missing DB data for plugin in admin panel
23+
* Improved logging IPN requests
24+
* Improved webhook handling to prevent an issue where Order IPN's could update a refunded Order's status to a processable Order status
1325

1426
# 5.3.2
1527
* Fix typo "completed" for BitPay available statuses

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Visit the [Releases](https://github.com/bitpay/bitpay-checkout-for-woocommerce/r
2626

2727
**BitPay Support:**
2828

29-
* Last Cart Version Tested: Wordpress 6.4.2
29+
* Last Cart Version Tested: Wordpress 6.5.2
3030
* [GitHub Issues](https://github.com/bitpay/bitpay-checkout-for-woocommerce/issues)
3131
* [Support](https://support.bitpay.com/hc/en-us)
3232

readme.txt

+12
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,22 @@ You can contact our support team via the following form https://bitpay.com/reque
117117
* Fixed a bug when the IPN/webhook is received with "complete" status
118118
* * Add Unit & End2End tests
119119

120+
= 5.5.1 =
121+
* Fixed issue with payment logo url
122+
123+
= 5.5.0 =
124+
* Tested compatibility with WordPress 6.5.2
125+
126+
= 5.4.1 =
127+
* Improved webhook validation to improve timing issues
128+
120129
= 5.4.0 =
121130
* Added compatibility with Checkout Blocks
122131
* Fixed Checkout Flow (BitPay Modal)
123132
* Tested compatibility with WordPress 6.4.2
133+
* Fixed issue with exception for missing DB data for plugin in admin panel
134+
* Improved logging IPN requests
135+
* Improved webhook handling to prevent an issue where Order IPN's could update a refunded Order's status to a processable Order status
124136

125137
= 5.3.2 =
126138
* Fix typo "completed" for BitPay available statuses

0 commit comments

Comments
 (0)