Skip to content

Commit

Permalink
Merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kwiatkowski committed Nov 27, 2024
2 parents 740e6eb + a7698f6 commit 8cdd257
Show file tree
Hide file tree
Showing 19 changed files with 3,003 additions and 2,368 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
php-version: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install Composer Dependencies
run: composer install --dev
- name: Change permissions for phpcs
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ codeception.yml
logs/*.*
/.idea/
/vendor
/build
/build
.phpunit.result.cache
tests/.env
tests/Unit/wp-config.php
3 changes: 2 additions & 1 deletion BitPayLib/class-bitpayclientfactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function create(): Client {

return new PosClient(
$token,
$this->get_environment()
$this->get_environment(),
'BitPay_WooCommerce_v6.0.0'
);
}

Expand Down
26 changes: 26 additions & 0 deletions BitPayLib/class-bitpaycreateorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace BitPayLib;

class BitPayCreateOrder {

public const BITPAY_TOKEN_ORDER_METADATA_KEY = '_bitpay_token';

private BitPayPaymentSettings $bitpay_payment_settings;

public function __construct(
BitPayPaymentSettings $bitpay_payment_settings,
) {
$this->bitpay_payment_settings = $bitpay_payment_settings;
}

public function execute( int $order_id ): void {
$token = $this->bitpay_payment_settings->get_bitpay_token();
$order = new \WC_Order( $order_id );

$order->update_meta_data( self::BITPAY_TOKEN_ORDER_METADATA_KEY, $token );
$order->save_meta_data();
}
}
2 changes: 1 addition & 1 deletion BitPayLib/class-bitpayinvoicecreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function execute(): void {

$this->bitpay_checkout_insert_order_note( $order_id, $invoice_id );

wp_redirect( $bitpay_invoice->getURL() ); // phpcs:ignore
wp_redirect( $bitpay_invoice->getUrl() ); // phpcs:ignore
exit();
} catch ( BitPayException $e ) {
$this->bitpay_logger->execute( $e->getMessage(), 'NEW BITPAY INVOICE', false, true );
Expand Down
32 changes: 26 additions & 6 deletions BitPayLib/class-bitpayipnprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ class BitPayIpnProcess {
private BitPayLogger $logger;
private BitPayClientFactory $factory;
private BitPayWordpressHelper $bitpay_wordpress_helper;
private BitPayWebhookVerifier $bitpay_webhook_verifier;
private BitPayPaymentSettings $bitpay_payment_settings;

public function __construct(
BitPayCheckoutTransactions $bitpay_checkout_transactions,
BitPayClientFactory $factory,
BitPayWordpressHelper $bitpay_wordpress_helper,
BitPayLogger $logger
BitPayLogger $logger,
BitPayWebhookVerifier $bitpay_webhook_verifier,
BitPayPaymentSettings $bitpay_payment_settings,
) {
$this->bitpay_checkout_transactions = $bitpay_checkout_transactions;
$this->logger = $logger;
$this->factory = $factory;
$this->bitpay_wordpress_helper = $bitpay_wordpress_helper;
$this->bitpay_webhook_verifier = $bitpay_webhook_verifier;
$this->bitpay_payment_settings = $bitpay_payment_settings;
}

public function execute( WP_REST_Request $request ): void {
Expand All @@ -45,9 +51,11 @@ public function execute( WP_REST_Request $request ): void {
$data['event'] = $event;
$data['requestDate'] = date( 'Y-m-d H:i:s' );
$invoice_id = $data['id'] ?? null;
$x_signature = $request->get_header( 'x-signature' );

$this->logger->execute( $data, 'INCOMING IPN', true );
if ( ! $event || ! $data || ! $invoice_id ) {

if ( ! $event || ! $data || ! $invoice_id || ! $x_signature ) {
$this->logger->execute( 'Wrong IPN request', 'INCOMING IPN ERROR', false, true );
return;
}
Expand All @@ -57,6 +65,8 @@ public function execute( WP_REST_Request $request ): void {
do_action( 'bitpay_checkout_woocoomerce_after_get_invoice', $bitpay_invoice );
$order = $this->bitpay_wordpress_helper->get_order( $bitpay_invoice->getOrderId() );
$this->validate_order( $order, $invoice_id );
$this->validate_webhook( $x_signature, $request->get_body(), $order );

$this->process( $bitpay_invoice, $order, $event['name'] );
} catch ( BitPayInvalidOrder $e ) { // phpcs:ignore
// do nothing.
Expand Down Expand Up @@ -295,10 +305,6 @@ private function process_processing( Invoice $bitpay_invoice, WC_Order $order ):
$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.
*
Expand All @@ -322,4 +328,18 @@ 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;
}

private function validate_webhook( string $x_signature, string $webhook_body, WC_Order $order ): void {
$order_bitpay_token = $order->get_meta( BitPayCreateOrder::BITPAY_TOKEN_ORDER_METADATA_KEY );

if ( $order_bitpay_token &&
! $this->bitpay_webhook_verifier->verify(
$this->bitpay_payment_settings->get_bitpay_token(),
$x_signature,
$webhook_body
)
) {
throw new \Exception( 'IPN Request failed HMAC validation' );
}
}
}
8 changes: 6 additions & 2 deletions BitPayLib/class-bitpaylogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class BitPayLogger {

public function execute( $msg, string $type, bool $is_array = false, $error = false ): void {
$bitpay_checkout_options = get_option( 'woocommerce_bitpay_checkout_gateway_settings' );
$log_directory = plugin_dir_path( __FILE__ ) . '..' . DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'logs/';
$log_directory = $this->get_log_directory();
if ( ! file_exists( $log_directory ) && ! mkdir( $log_directory ) && ! is_dir( $log_directory ) ) {
throw new \RuntimeException( sprintf( 'Directory "%s" was not created', esc_html( $log_directory ) ) );
}
Expand Down Expand Up @@ -47,4 +46,9 @@ public function execute( $msg, string $type, bool $is_array = false, $error = fa
}
// @codingStandardsIgnoreEnd
}

public function get_log_directory(): string {
return plugin_dir_path( __FILE__ ) . '..' . DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'logs/';
}
}
29 changes: 28 additions & 1 deletion BitPayLib/class-bitpaypluginsetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ class BitPayPluginSetup {
private BitPayPaymentSettings $bitpay_payment_settings;
private BitPayInvoiceCreate $bitpay_invoice_create;
private BitPayCheckoutTransactions $bitpay_checkout_transactions;
private BitPayCreateOrder $bitpay_create_order;
private BitPaySupportPackage $bitpay_support_package;

public function __construct() {
$this->bitpay_payment_settings = new BitPayPaymentSettings();
$factory = new BitPayClientFactory( $this->bitpay_payment_settings );
$cart = new BitPayCart();
$logger = new BitPayLogger();
$wordpress_helper = new BitPayWordpressHelper();
$webhook_verifier = new BitPayWebhookVerifier();
$this->bitpay_checkout_transactions = new BitPayCheckoutTransactions( $wordpress_helper );
$this->bitpay_ipn_process = new BitPayIpnProcess( $this->bitpay_checkout_transactions, $factory, $wordpress_helper, $logger );
$this->bitpay_ipn_process = new BitPayIpnProcess( $this->bitpay_checkout_transactions, $factory, $wordpress_helper, $logger, $webhook_verifier, $this->bitpay_payment_settings );
$this->bitpay_cancel_order = new BitPayCancelOrder( $cart, $this->bitpay_checkout_transactions, $logger );
$this->bitpay_invoice_create = new BitPayInvoiceCreate(
$factory,
Expand All @@ -42,6 +45,13 @@ public function __construct() {
$wordpress_helper,
$logger
);
$this->bitpay_create_order = new BitPayCreateOrder(
$this->bitpay_payment_settings
);
$this->bitpay_support_package = new BitPaySupportPackage(
$wordpress_helper,
$logger
);
}

public function execute(): void {
Expand All @@ -58,6 +68,8 @@ public function execute(): void {
add_filter( 'woocommerce_payment_gateways', array( $this, 'wc_bitpay_checkout_add_to_gateways' ) );
add_filter( 'woocommerce_order_button_html', array( $this, 'bitpay_checkout_replace_order_button_html' ), 10, 2 );
add_action( 'woocommerce_blocks_loaded', array( $this, 'register_payment_block' ) );
add_action( 'woocommerce_new_order', array( $this, 'bitpay_create_order' ) );
add_action( 'woocommerce_update_order', array( $this, 'bitpay_create_order' ) );

// http://<host>/wp-json/bitpay/ipn/status url.
// http://<host>/wp-json/bitpay/cartfix/restore url.
Expand All @@ -82,6 +94,17 @@ function () {
'permission_callback' => '__return_true',
)
);
register_rest_route(
'bitpay/site',
'/health-status',
array(
'methods' => 'GET',
'callback' => array( $this->bitpay_support_package, 'get_zip' ),
'permission_callback' => function () {
return current_user_can( 'manage_woocommerce' );
},
)
);
}
);
}
Expand Down Expand Up @@ -221,4 +244,8 @@ function () {
5
);
}

public function bitpay_create_order( int $order_id ): void {
$this->bitpay_create_order->execute( $order_id );
}
}
Loading

0 comments on commit 8cdd257

Please sign in to comment.