Skip to content

Commit

Permalink
Merge pull request #78 from mwarzybok-sumoheavy/SP-811
Browse files Browse the repository at this point in the history
SP-811 Add testing to WooCommerce
  • Loading branch information
p-maguire committed Mar 8, 2024
2 parents 1004d54 + b0c5ed8 commit 8377c5b
Show file tree
Hide file tree
Showing 40 changed files with 6,517 additions and 347 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php-version: [ 8.0, 8.1, 8.2 ]
php-version: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/checkout@v3
Expand All @@ -18,4 +18,4 @@ jobs:
run: chmod 755 build/vendor/bin/phpcs
- name: Run WordPress code standard
run: |
./build/vendor/bin/phpcs -v --ignore=vendor,build,wpcs,scoper.inc.php --standard=./phpcs.xml ./
./build/vendor/bin/phpcs -v --ignore=vendor,build,tests,wpcs,scoper.inc.php --standard=./phpcs.xml ./
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
BitPayLib/.DS_Store
codeception.yml
logs/*.*
/.idea/
/vendor
Expand Down
16 changes: 16 additions & 0 deletions BitPayLib/Exception/class-bitpayinvalidorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace BitPayLib\Exception;

/**
* Plugin Name: BitPay Checkout for WooCommerce
* Plugin URI: https://www.bitpay.com
* Description: BitPay Checkout Plugin
* Version: 6.0.0
* Author: BitPay
* Author URI: mailto:integrations@bitpay.com?subject=BitPay Checkout for WooCommerce
*/
class BitPayInvalidOrder extends \RuntimeException {
}
2 changes: 1 addition & 1 deletion BitPayLib/class-bitpaycancelorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function execute( \WP_REST_Request $request ): void {
}

private function clear_cookie_for_invoice_id(): void {
setcookie( 'bitpay-invoice-id', '', time() - 3600 );
setcookie( BitPayPluginSetup::COOKIE_INVOICE_ID_NAME, '', time() - 3600 );
}
}
15 changes: 10 additions & 5 deletions BitPayLib/class-bitpaycheckouttransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class BitPayCheckoutTransactions {

private const TABLE_NAME = '_bitpay_checkout_transactions';

private BitPayWordpressHelper $bitpay_wordpress;

public function __construct( BitPayWordpressHelper $bitpay_wordpress ) {
$this->bitpay_wordpress = $bitpay_wordpress;
}

public function create_table(): void {
$table_name = '_bitpay_checkout_transactions';

Expand All @@ -36,11 +42,6 @@ public function create_table(): void {
dbDelta( $sql );
}

private function get_wpdb(): wpdb {
global $wpdb;
return $wpdb;
}

public function create_transaction(
string $order_id,
string $transaction_id,
Expand Down Expand Up @@ -117,4 +118,8 @@ public function update_transaction_status( Invoice $invoice ): void {
)
);
}

private function get_wpdb(): wpdb {
return $this->bitpay_wordpress->get_wpdb();
}
}
79 changes: 17 additions & 62 deletions BitPayLib/class-bitpayinvoicecreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,48 @@
*/
class BitPayInvoiceCreate {

private const COOKIE_INVOICE_ID_NAME = 'bitpay-invoice-id';

private BitPayClientFactory $client_factory;
private BitPayLogger $bitpay_logger;
private BitPayCheckoutTransactions $bitpay_checkout_transactions;
private BitPayPaymentSettings $bitpay_payment_settings;
private BitPayWordpressHelper $wordpress_helper;
private BitPayInvoiceFactory $bitpay_invoice_factory;

public function __construct(
BitPayClientFactory $client_factory,
BitPayInvoiceFactory $bitpay_invoice_factory,
BitPayCheckoutTransactions $bitpay_checkout_transactions,
BitPayPaymentSettings $bitpay_payment_settings,
BitPayWordpressHelper $wordpress_helper,
BitPayLogger $bitpay_logger
) {
$this->client_factory = $client_factory;
$this->bitpay_invoice_factory = $bitpay_invoice_factory;
$this->bitpay_checkout_transactions = $bitpay_checkout_transactions;
$this->bitpay_payment_settings = $bitpay_payment_settings;
$this->wordpress_helper = $wordpress_helper;
$this->bitpay_logger = $bitpay_logger;
}

public function execute(): void {
global $wp;
$bitpay_checkout_options = get_option( 'woocommerce_bitpay_checkout_gateway_settings' );

if ( ! is_checkout() || empty( $wp->query_vars['order-received'] ) ) {
$order_id = $this->wordpress_helper->get_query_var( 'order-received' );

if ( ! $order_id || ! is_checkout() ) {
return;
}

$order_id = $wp->query_vars['order-received'];

try {
$order = new \WC_Order( $order_id );
$order = $this->wordpress_helper->get_order( $order_id );

if ( isset( $_GET['redirect'] ) && $_GET['redirect'] === 'false' ) { // phpcs:ignore
$redirect = $this->wordpress_helper->get_url_parameter( 'redirect' );
if ( $redirect === 'false' ) { // phpcs:ignore
$this->clear_invoice_id_cookie();
return;
}

if ( $order->get_payment_method() !== 'bitpay_checkout_gateway' ) {
return;
}

$bitpay_invoice = new Invoice();
$bitpay_invoice->setPrice( (float) $order->get_total() );
$bitpay_invoice->setCurrency( $order->get_currency() );
$bitpay_invoice->setOrderId( $order->get_order_number() );
$bitpay_invoice->setAcceptanceWindow( 1200000 );
$bitpay_invoice->setNotificationURL( get_home_url() . '/wp-json/bitpay/ipn/status' );
$bitpay_invoice->setExtendedNotifications( true );
$this->add_buyer_to_invoice( $bitpay_invoice );
$this->add_redirect_url( $order, $bitpay_invoice );

$bitpay_invoice = $this->bitpay_invoice_factory->create_by_wc_order( $order );
$bitpay_invoice = $this->client_factory->create()->createInvoice( $bitpay_invoice, Facade::POS, false );

$this->bitpay_logger->execute( $bitpay_invoice->toArray(), 'NEW BITPAY INVOICE', true );
Expand All @@ -91,7 +82,7 @@ public function execute(): void {
} catch ( BitPayException $e ) {
$this->bitpay_logger->execute( $e->getMessage(), 'NEW BITPAY INVOICE', false, true );
$error_url = get_home_url() . '/' . $bitpay_checkout_options['bitpay_checkout_error'];
$order = new \WC_Order( $order_id );
$order = $this->wordpress_helper->get_order( $order_id );
$items = $order->get_items();
$order->update_status( 'wc-cancelled', __( $e->getMessage() . '.', 'woocommerce' ) ); // phpcs:ignore

Expand Down Expand Up @@ -123,54 +114,18 @@ private function bitpay_checkout_insert_order_note( $order_id = null, $transacti
return;
}

$order = new \WC_Order( $order_id );
$order = $this->wordpress_helper->get_order( $order_id );
$order->set_transaction_id( $transaction_id );
$order->save();
}

private function clear_invoice_id_cookie(): void {
setcookie( self::COOKIE_INVOICE_ID_NAME, '', time() - 3600 );
}

private function add_buyer_to_invoice( Invoice $bitpay_invoice ): void {
if ( ! $this->bitpay_payment_settings->should_capture_email() ) {
return;
}

/** @var \WP_User $current_user */ // phpcs:ignore
$current_user = wp_get_current_user();

if ( $current_user->user_email ) {
$buyer = new Buyer();
$buyer->setName( $current_user->display_name );
$buyer->setEmail( $current_user->user_email );
$bitpay_invoice->setBuyer( $buyer );
}
}

private function add_redirect_url( \WC_Order $order, Invoice $bitpay_invoice ): void {
$bitpay_invoice->setRedirectURL( $this->get_redirect_url( $order ) );
setcookie( BitPayPluginSetup::COOKIE_INVOICE_ID_NAME, '', time() - 3600 );
}

private function set_cookie_for_redirects_and_updating_order_status( ?string $invoice_id ): void {
$cookie_name = self::COOKIE_INVOICE_ID_NAME;
$cookie_name = BitPayPluginSetup::COOKIE_INVOICE_ID_NAME;
$cookie_value = $invoice_id;
setcookie( $cookie_name, $cookie_value, time() + ( 86400 * 30 ), '/' );
}

private function get_redirect_url( \WC_Order $order ): string {
$custom_redirect_page = $this->bitpay_payment_settings->get_custom_redirect_page();
if ( $custom_redirect_page ) {
return $custom_redirect_page . '?custompage=true';
}

$url_suffix = '?key=' . $order->get_order_key() . '&redirect=false';
$checkout_slug = $this->bitpay_payment_settings->get_checkout_slug();
if ( $checkout_slug ) {
return get_home_url() . DIRECTORY_SEPARATOR . $checkout_slug . '/order-received/'
. $order->get_id() . DIRECTORY_SEPARATOR . $url_suffix;
}

return wc_get_endpoint_url( 'order-received', $order->get_id(), wc_get_checkout_url() ) . $url_suffix;
}
}
84 changes: 84 additions & 0 deletions BitPayLib/class-bitpayinvoicefactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace BitPayLib;

use BitPaySDK\Model\Invoice\Buyer;
use BitPaySDK\Model\Invoice\Invoice;

/**
* Plugin Name: BitPay Checkout for WooCommerce
* Plugin URI: https://www.bitpay.com
* Description: BitPay Checkout Plugin
* Version: 6.0.0
* Author: BitPay
* Author URI: mailto:integrations@bitpay.com?subject=BitPay Checkout for WooCommerce
*/
class BitPayInvoiceFactory {

private BitPayPaymentSettings $bitpay_payment_settings;
private BitPayWordpressHelper $wordpress_helper;

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

public function create_by_wc_order( \WC_Order $wc_order ): Invoice {
$bitpay_invoice = new Invoice();
$bitpay_invoice->setPrice( (float) $wc_order->get_total() );
$bitpay_invoice->setCurrency( $wc_order->get_currency() );
$bitpay_invoice->setOrderId( $wc_order->get_order_number() );
$bitpay_invoice->setAcceptanceWindow( 1200000 );
$bitpay_invoice->setNotificationURL( $this->wordpress_helper->get_home_url() . '/wp-json/bitpay/ipn/status' );
$bitpay_invoice->setExtendedNotifications( true );
$this->add_buyer_to_invoice( $bitpay_invoice );
$this->add_redirect_url( $wc_order, $bitpay_invoice );

return $bitpay_invoice;
}

private function add_buyer_to_invoice( Invoice $bitpay_invoice ): void {
if ( ! $this->bitpay_payment_settings->should_capture_email() ) {
return;
}

/** @var \WP_User $current_user */ // phpcs:ignore
$current_user = $this->wordpress_helper->wp_get_current_user();

if ( $current_user->user_email ) {
$buyer = new Buyer();
$buyer->setName( $current_user->display_name );
$buyer->setEmail( $current_user->user_email );
$bitpay_invoice->setBuyer( $buyer );
}
}

private function add_redirect_url( \WC_Order $order, Invoice $bitpay_invoice ): void {
$bitpay_invoice->setRedirectURL( $this->get_redirect_url( $order ) );
}

private function get_redirect_url( \WC_Order $order ): string {
$custom_redirect_page = $this->bitpay_payment_settings->get_custom_redirect_page();
if ( $custom_redirect_page ) {
return $custom_redirect_page . '?custompage=true';
}

$url_suffix = '?key=' . $order->get_order_key() . '&redirect=false';
$checkout_slug = $this->bitpay_payment_settings->get_checkout_slug();
if ( $checkout_slug ) {
return get_home_url() . DIRECTORY_SEPARATOR . $checkout_slug . '/order-received/'
. $order->get_id() . DIRECTORY_SEPARATOR . $url_suffix;
}

return $this->wordpress_helper->get_endpoint_url(
'order-received',
(string) $order->get_id(),
$this->wordpress_helper->get_checkout_url()
) . $url_suffix;
}
}

0 comments on commit 8377c5b

Please sign in to comment.