Skip to content

Commit

Permalink
Merge pull request #89 from p-maguire/6.0.x
Browse files Browse the repository at this point in the history
SP-1066: Resolve conflicts with master for 6.0.x
  • Loading branch information
p-maguire authored Sep 9, 2024
2 parents f9914e5 + eed838b commit 69609bb
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 22 deletions.
78 changes: 60 additions & 18 deletions BitPayLib/class-bitpayipnprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public function __construct(
}

public function execute( WP_REST_Request $request ): void {
$data = $request->get_body();

$data = json_decode( $data, true, 512, JSON_THROW_ON_ERROR );
$event = $data['event'] ?? null;
$data = $data['data'] ?? null;
$invoice_id = $data['id'] ?? null;
$data = $request->get_body();
$data = json_decode( $data, true, 512, JSON_THROW_ON_ERROR );
$event = $data['event'] ?? null;
$data = $data['data'] ?? null;
$data['event'] = $event;
$data['requestDate'] = date( 'Y-m-d H:i:s' );
$invoice_id = $data['id'] ?? null;

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

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

$invoice_id = $bitpay_invoice->getId();
$wordpress_order_status = $this->bitpay_wordpress_helper
->get_bitpay_gateway_option( 'bitpay_checkout_order_process_confirmed_status' );
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $wordpress_order_status ) {
$order->add_order_note(
$this->get_start_order_note( $invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings.'
$this->get_start_order_note( $invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings.'
);
return;
}

$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'Processing';
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? null;
if ( ! $new_status ) {
$new_status = 'Processing';
$wordpress_order_status = 'wc-pending';
$wordpress_order_status = 'pending';
}

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

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

$invoice_id = $bitpay_invoice->getId();
$wordpress_order_status = $this->bitpay_wordpress_helper
->get_bitpay_gateway_option( 'bitpay_checkout_order_process_complete_status' );
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $wordpress_order_status ) {
$order->add_order_note(
$this->get_start_order_note( $invoice_id )
. 'has changed to Complete. The order status has not been updated due to your settings.'
. 'has changed to Complete. The order status has not been updated due to your settings.'
);
return;
}

$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'Processing';
if ( ! $this->should_process_completed_action( $wc_order_status, $wordpress_order_status ) ) {
return;
}

$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? null;
$new_status = apply_filters( 'bitpay_checkout_order_process_complete_status', $new_status, $wordpress_order_status );
if ( ! $new_status ) {
$new_status = 'Processing';
$wordpress_order_status = 'wc-pending';
$wordpress_order_status = 'pending';
}

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

$invoice_id = $bitpay_invoice->getId();
if ( $underpaid_amount ) {
$order->add_order_note( $this->get_start_order_note( $invoice_id ) . $underpaid_amount . ' has been refunded.' );
$order->update_status( 'refunded', __( 'BitPay payment refunded', 'woocommerce' ) );
$this->process_refunded( $bitpay_invoice, $order );
return;
}

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

private function process_refunded( Invoice $bitpay_invoice, WC_Order $order ): void {
if ( ! $this->should_process_refund() ) {
$order->add_order_note(
$this->get_start_order_note( $bitpay_invoice->getId() )
. 'has changed to Refunded. The order status has not been updated due to your settings.'
);
return;
}

$order->add_order_note( $this->get_start_order_note( $bitpay_invoice->getId() ) . 'has been refunded.' );
$order->update_status( 'refunded', __( 'BitPay payment refunded', 'woocommerce' ) );
}

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

Expand All @@ -280,4 +294,32 @@ private function process_processing( Invoice $bitpay_invoice, WC_Order $order ):
$new_status = $this->get_wc_order_statuses()[ $wordpress_order_status ] ?? 'processing';
$order->update_status( $new_status, __( 'BitPay payment processing', 'woocommerce' ) );
}

private function has_final_status( WC_Order $order ): bool {
return \in_array( $order->get_status(), self::FINAL_WC_ORDER_STATUSES, true );
}

/**
* We don't want to change complete order to process.
*
* @param string $wc_order_status WC order status.
* @param string|null $wordpress_order_status_from_settings status to event from BitPay settings.
* @return bool
*/
private function should_process_completed_action( string $wc_order_status, ?string $wordpress_order_status_from_settings ): bool {
if ( 'completed' !== $wc_order_status ) {
return true;
}

if ( 'pending' === $wordpress_order_status_from_settings || 'processing' === $wordpress_order_status_from_settings ) {
return false;
}

return true;
}

private function should_process_refund(): bool {
$should_process_refund_status = $this->get_wc_order_statuses()['bitpay_checkout_order_process_refund'] ?? '1';
return '1' === $should_process_refund_status;
}
}
2 changes: 1 addition & 1 deletion BitPayLib/class-bitpaypages.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function checkout_thank_you( int $order_id ): void {

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

wp_enqueue_script( 'remote-bitpay-js', $js_script, null, null, false ); // phpcs:ignore
wp_enqueue_script('remote-bitpay-js', $js_script, null, null, false ); // phpcs:ignore
wp_enqueue_script('bitpay_thank_you', plugins_url( '../../js/bitpay_thank_you.js', __FILE__ ), null, BitPayPluginSetup::VERSION, false ); // phpcs:ignore
?>
<script type="text/javascript">
Expand Down
7 changes: 6 additions & 1 deletion BitPayLib/class-bitpaypaymentsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function check_token(): void {
}

$bitpay_checkout_token = $this->get_bitpay_token();
$bitpay_checkout_endpoint = $this->get_bitpay_gateway_setting( 'bitpay_checkout_endpoint' );
$bitpay_checkout_endpoint = $this->get_bitpay_checkout_endpoint();

if ( ! $bitpay_checkout_token ) {
$message = 'There is no token set for your ' . strtoupper( $bitpay_checkout_endpoint ) . ' environment. BitPay will not function if this is not set.';
Expand Down Expand Up @@ -163,4 +163,9 @@ private function get_bitpay_gateway_setting( string $setting_name, $default_valu
private function get_bitpay_gateway_settings(): array {
return get_option( 'woocommerce_bitpay_checkout_gateway_settings', array() );
}

private function get_bitpay_checkout_endpoint(): string {
// 'test' as default when we don't store options yet (before save configuration)
return $this->get_bitpay_gateway_setting( 'bitpay_checkout_endpoint' ) ?? 'test';
}
}
12 changes: 11 additions & 1 deletion BitPayLib/class-wcgatewaybitpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ public function init_form_fields() {
'options' => $wc_statuses_arr,
'default' => 'wc-processing',
),
'bitpay_checkout_order_process_refund' => array(
'title' => __( 'BitPay Process Refund Status', 'woocommerce' ),
'type' => 'select',
'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' ),
'options' => array(
'0' => 'No',
'1' => 'Yes',
),
'default' => '1',
),
'bitpay_checkout_order_expired_status' => array(
'title' => __( 'BitPay Expired Status', 'woocommerce' ),
'type' => 'select',
Expand Down Expand Up @@ -299,7 +309,7 @@ public function process_payment( $order_id ) {
private function get_icon_on_payment_page(): string {
$settings = new BitPayPaymentSettings();

return $settings->get_payment_logo_url() . '" id="bitpay_logo';
return add_query_arg( 'id', 'bitpay_logo', $settings->get_payment_logo_url() );
}

private function get_processing_link(): string {
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Fixed a bug when the IPN/webhook is received with "complete" status
* Add Unit & End2End tests

# 5.5.1
* Fixed issue with payment logo url

# 5.5.0
* Tested compatibility with WordPress 6.5.2

# 5.4.1
* Improved webhook validation to improve timing issues

# 5.4.0
* Added compatibility with Checkout Blocks
* Fixed Checkout Flow (BitPay Modal)
* Tested compatibility with WordPress 6.4.2
* Fixed issue with exception for missing DB data for plugin in admin panel
* Improved logging IPN requests
* Improved webhook handling to prevent an issue where Order IPN's could update a refunded Order's status to a processable Order status

# 5.3.2
* Fix typo "completed" for BitPay available statuses
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Visit the [Releases](https://github.com/bitpay/bitpay-checkout-for-woocommerce/r

**BitPay Support:**

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

Expand Down
12 changes: 12 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,22 @@ You can contact our support team via the following form https://bitpay.com/reque
* Fixed a bug when the IPN/webhook is received with "complete" status
* * Add Unit & End2End tests

= 5.5.1 =
* Fixed issue with payment logo url

= 5.5.0 =
* Tested compatibility with WordPress 6.5.2

= 5.4.1 =
* Improved webhook validation to improve timing issues

= 5.4.0 =
* Added compatibility with Checkout Blocks
* Fixed Checkout Flow (BitPay Modal)
* Tested compatibility with WordPress 6.4.2
* Fixed issue with exception for missing DB data for plugin in admin panel
* Improved logging IPN requests
* Improved webhook handling to prevent an issue where Order IPN's could update a refunded Order's status to a processable Order status

= 5.3.2 =
* Fix typo "completed" for BitPay available statuses
Expand Down

0 comments on commit 69609bb

Please sign in to comment.