diff --git a/bin/phpcs.sh b/bin/phpcs.sh old mode 100644 new mode 100755 diff --git a/composer.lock b/composer.lock index ca6b77ac..0faf93cd 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "billwerk/php-sdk", - "version": "v1.0.9", + "version": "v1.0.15", "source": { "type": "git", "url": "https://github.com/0zd0/billwerk-sdk-php.git", - "reference": "beb3c6255a9790695011c8f266d055d053bbeb5b" + "reference": "bd4bab10ae6ce717fdfb567314463dcf36405355" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/0zd0/billwerk-sdk-php/zipball/beb3c6255a9790695011c8f266d055d053bbeb5b", - "reference": "beb3c6255a9790695011c8f266d055d053bbeb5b", + "url": "https://api.github.com/repos/0zd0/billwerk-sdk-php/zipball/bd4bab10ae6ce717fdfb567314463dcf36405355", + "reference": "bd4bab10ae6ce717fdfb567314463dcf36405355", "shasum": "" }, "require": { @@ -49,9 +49,9 @@ "description": "Api SDK for Billwerk+", "support": { "issues": "https://github.com/0zd0/billwerk-sdk-php/issues", - "source": "https://github.com/0zd0/billwerk-sdk-php/tree/v1.0.9" + "source": "https://github.com/0zd0/billwerk-sdk-php/tree/v1.0.15" }, - "time": "2024-06-18T18:14:56+00:00" + "time": "2024-06-20T20:43:02+00:00" }, { "name": "guzzlehttp/guzzle", diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index a8d2ed36..cb6edcc7 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -8,8 +8,12 @@ namespace Reepay\Checkout\Gateways; use Billwerk\Sdk\Exception\BillwerkApiException; +use Billwerk\Sdk\Exception\BillwerkClientException; +use Billwerk\Sdk\Exception\BillwerkNetworkException; +use Billwerk\Sdk\Exception\BillwerkRequestException; use Billwerk\Sdk\Model\Account\AccountModel; use Billwerk\Sdk\Model\Account\WebhookSettingsUpdateModel; +use Billwerk\Sdk\Model\Agreement\AgreementGetAllModel; use Exception; use Reepay\Checkout\Api; use Reepay\Checkout\Integrations\PWGiftCardsIntegration; @@ -349,19 +353,24 @@ public function reepay_card_store() { * Check if payment method activated in reepay * * @return bool + * @throws Exception Exception. */ public function check_is_active(): bool { - $gateways_reepay = get_transient( 'gateways_reepay' ); - if ( empty( $gateways_reepay ) ) { - $gateways_reepay = reepay()->api( $this )->request( 'GET', 'https://api.reepay.com/v1/agreement?only_active=true' ); - set_transient( 'gateways_reepay', $gateways_reepay, 5 ); + $current_name = str_replace( 'reepay_', '', $this->id ); + $key_transient = 'billwerk_active_agreements'; + $agreements = get_transient( $key_transient ); + if ( empty( $agreements ) ) { + $agreements = reepay()->sdk()->agreement()->all( ( new AgreementGetAllModel() )->setOnlyActive( true ) ); + set_transient( $key_transient, $agreements, 5 ); } - $current_name = str_replace( 'reepay_', '', $this->id ); + if ( ! is_array( $agreements ) ) { + return false; + } - if ( ! empty( $gateways_reepay ) && ! is_wp_error( $gateways_reepay ) ) { - foreach ( $gateways_reepay as $app ) { - if ( stripos( $app['type'], $current_name ) !== false ) { + foreach ( $agreements as $agreement ) { + if ( $agreement->getType() ) { + if ( stripos( $agreement->getType(), $current_name ) !== false ) { return true; } } diff --git a/includes/Gateways/Resurs.php b/includes/Gateways/Resurs.php index d8179908..159ad493 100644 --- a/includes/Gateways/Resurs.php +++ b/includes/Gateways/Resurs.php @@ -44,7 +44,6 @@ public function __construct() { 'products', 'refunds', ); - $this->logos = array( 'resurs' ); parent::__construct(); diff --git a/includes/Gateways/Swish.php b/includes/Gateways/Swish.php index c10ba696..e379b04a 100644 --- a/includes/Gateways/Swish.php +++ b/includes/Gateways/Swish.php @@ -44,7 +44,6 @@ public function __construct() { 'products', 'refunds', ); - $this->logos = array( 'swish' ); parent::__construct(); diff --git a/includes/Gateways/Viabill.php b/includes/Gateways/Viabill.php index c3849f79..b721e71c 100644 --- a/includes/Gateways/Viabill.php +++ b/includes/Gateways/Viabill.php @@ -44,7 +44,6 @@ public function __construct() { 'products', 'refunds', ); - $this->logos = array( 'viabill' ); parent::__construct(); diff --git a/includes/Gateways/Vipps.php b/includes/Gateways/Vipps.php index 99e4fd7e..168ad633 100644 --- a/includes/Gateways/Vipps.php +++ b/includes/Gateways/Vipps.php @@ -46,7 +46,6 @@ public function __construct() { 'products', 'refunds', ); - $this->logos = array( 'vipps' ); parent::__construct(); diff --git a/includes/Gateways/VippsRecurring.php b/includes/Gateways/VippsRecurring.php index 7ad737aa..84674cb0 100644 --- a/includes/Gateways/VippsRecurring.php +++ b/includes/Gateways/VippsRecurring.php @@ -60,7 +60,6 @@ public function __construct() { 'subscription_payment_method_change_admin', 'multiple_subscriptions', ); - $this->logos = array( 'vipps' ); parent::__construct(); diff --git a/reepay-woocommerce-payment.php b/reepay-woocommerce-payment.php index 2d2257c9..d7c779fd 100755 --- a/reepay-woocommerce-payment.php +++ b/reepay-woocommerce-payment.php @@ -17,6 +17,14 @@ use Billwerk\Sdk\BillwerkRequest; use Billwerk\Sdk\Sdk; use Billwerk\Sdk\Service\AccountService; +use Billwerk\Sdk\Service\AgreementService; +use Billwerk\Sdk\Service\ChargeService; +use Billwerk\Sdk\Service\CustomerService; +use Billwerk\Sdk\Service\InvoiceService; +use Billwerk\Sdk\Service\PaymentMethodService; +use Billwerk\Sdk\Service\RefundService; +use Billwerk\Sdk\Service\SessionService; +use Billwerk\Sdk\Service\TransactionService; use GuzzleHttp\Client; use GuzzleHttp\Psr7\HttpFactory; use Reepay\Checkout\Api; @@ -277,16 +285,37 @@ public function sdk( bool $force_live_key = false ): Sdk { new SdkLogger() ); - if ( $this->di()->is_set( AccountService::class ) ) { - $account_service = $this->di()->get( AccountService::class ); - if ( $account_service instanceof AccountService ) { - $sdk->setAccountService( $account_service ); - } - } + $this->set_service_if_available( AccountService::class, $sdk, 'setAccountService' ); + $this->set_service_if_available( AgreementService::class, $sdk, 'setAgreementService' ); + $this->set_service_if_available( ChargeService::class, $sdk, 'setChargeService' ); + $this->set_service_if_available( CustomerService::class, $sdk, 'setCustomerService' ); + $this->set_service_if_available( InvoiceService::class, $sdk, 'setInvoiceService' ); + $this->set_service_if_available( PaymentMethodService::class, $sdk, 'setPaymentMethodService' ); + $this->set_service_if_available( RefundService::class, $sdk, 'setRefundService' ); + $this->set_service_if_available( SessionService::class, $sdk, 'setSessionService' ); + $this->set_service_if_available( TransactionService::class, $sdk, 'setTransactionService' ); return $sdk; } + /** + * Sets the service in the SDK if the service is available in the DI container. + * + * @param string $service_class The class name of the service. + * @param Sdk $sdk The SDK instance. + * @param string $set_method The method to set the service in the SDK. + * + * @return void + */ + private function set_service_if_available( string $service_class, Sdk $sdk, string $set_method ) { + if ( $this->di()->is_set( $service_class ) ) { + $service = $this->di()->get( $service_class ); + if ( $service instanceof $service_class ) { + $sdk->$set_method( $service ); + } + } + } + /** * Get logger * diff --git a/tests/helpers/GatewayForTesting.php b/tests/helpers/GatewayForTesting.php new file mode 100644 index 00000000..31a70599 --- /dev/null +++ b/tests/helpers/GatewayForTesting.php @@ -0,0 +1,19 @@ +account_service_mock = $this->createMock( AccountService::class ); + $this->account_service_mock = $this->createMock( AccountService::class ); + $this->agreement_service_mock = $this->createMock( AgreementService::class ); + $this->charge_service_mock = $this->createMock( ChargeService::class ); + $this->customer_service_mock = $this->createMock( CustomerService::class ); + $this->invoice_service_mock = $this->createMock( InvoiceService::class ); + $this->payment_method_service_mock = $this->createMock( PaymentMethodService::class ); + $this->refund_service_mock = $this->createMock( RefundService::class ); + $this->session_service_mock = $this->createMock( SessionService::class ); + $this->transaction_service_mock = $this->createMock( TransactionService::class ); reepay()->di()->set( AccountService::class, $this->account_service_mock ); + reepay()->di()->set( AgreementService::class, $this->agreement_service_mock ); + reepay()->di()->set( ChargeService::class, $this->charge_service_mock ); + reepay()->di()->set( CustomerService::class, $this->customer_service_mock ); + reepay()->di()->set( InvoiceService::class, $this->invoice_service_mock ); + reepay()->di()->set( PaymentMethodService::class, $this->payment_method_service_mock ); + reepay()->di()->set( RefundService::class, $this->refund_service_mock ); + reepay()->di()->set( SessionService::class, $this->session_service_mock ); + reepay()->di()->set( TransactionService::class, $this->transaction_service_mock ); $this->api_mock = $this->getMockBuilder( Api::class )->getMock(); reepay()->di()->set( Api::class, $this->api_mock ); diff --git a/tests/unit/Gateways/ReepayGatewayTest.php b/tests/unit/Gateways/ReepayGatewayTest.php index 1a5b9de1..fc47b891 100644 --- a/tests/unit/Gateways/ReepayGatewayTest.php +++ b/tests/unit/Gateways/ReepayGatewayTest.php @@ -7,40 +7,44 @@ namespace Reepay\Checkout\Tests\Unit\Gateways; +use Billwerk\Sdk\Enum\AgreementTypeEnum; use Billwerk\Sdk\Exception\BillwerkApiException; use Billwerk\Sdk\Model\Account\AccountModel; use Billwerk\Sdk\Model\Account\WebhookSettingsModel; +use Billwerk\Sdk\Model\Agreement\AgreementModel; use Exception; use Reepay\Checkout\Gateways\ReepayGateway; +use Reepay\Checkout\Tests\Helpers\GatewayForTesting; use Reepay\Checkout\Tests\Helpers\OrderItemsGenerator; use Reepay\Checkout\Tests\Helpers\PLUGINS_STATE; use Reepay\Checkout\Tests\Helpers\Reepay_UnitTestCase; use WP_Error; -/** - * Test class child - * - * @package Reepay\Checkout\Tests\Unit\Gateways - */ -class ReepayGatewayTestChild extends ReepayGateway { - -} - /** * Test class * * @package Reepay\Checkout\Tests\Unit\Gateways */ class ReepayGatewayTest extends Reepay_UnitTestCase { + /** + * Gateway for testing + * + * @var GatewayForTesting $gateway + */ + public static GatewayForTesting $gateway; - public static ReepayGatewayTestChild $gateway; - + /** + * Set up before class + */ public static function set_up_before_class() { parent::set_up_before_class(); - self::$gateway = new ReepayGatewayTestChild(); + self::$gateway = new GatewayForTesting(); } + /** + * Tear down after class + */ public static function tear_down_after_class() { parent::tear_down_after_class(); @@ -49,10 +53,18 @@ public static function tear_down_after_class() { } /** - * Test is_webhook_configured + * Agreement types + * + * @return array + */ + public function agreement_type_provider(): array { + return array_map( fn( $gateway ) => array( $gateway ), AgreementTypeEnum::getAll() ); + } + + /** + * Test function is_webhook_configured * * @see ReepayGateway::is_webhook_configured() - * @todo add check exceptions * @return void */ public function test_is_webhook_configured() { @@ -62,39 +74,26 @@ public function test_is_webhook_configured() { $this->account_service_mock ->method( 'getWebHookSettings' ) - ->willReturnOnConsecutiveCalls( $settings ); + ->willReturnOnConsecutiveCalls( $settings, $this->throwException( new BillwerkApiException() ) ); + $this::assertTrue( self::$gateway->is_webhook_configured() ); + $this::assertFalse( self::$gateway->is_webhook_configured() ); } /** + * Test function check_is_active + * * @param string $gateway gateway id. * - * @testWith - * ["anyday"] - * ["applepay"] - * ["googlepay"] - * ["klarna_pay_later"] - * ["klarna_pay_now"] - * ["klarna_slice_it"] - * ["mobilepay"] - * ["mobilepay_subscriptions"] - * ["paypal"] - * ["checkout"] - * ["resurs"] - * ["swish"] - * ["viabill"] - * ["vipps"] + * @dataProvider agreement_type_provider + * @throws Exception Exception. + * @see ReepayGateway::check_is_active() */ public function test_check_is_active( string $gateway ) { self::$gateway->id = 'reepay_' . $gateway; - - $this->api_mock->method( 'request' )->willReturn( - array( - array( - 'type' => $gateway, - ), - ) - ); + $this->agreement_service_mock + ->method( 'all' ) + ->willReturn( array( ( new AgreementModel() )->setType( $gateway ) ) ); $this->assertTrue( self::$gateway->check_is_active() ); } @@ -595,7 +594,7 @@ public function test_get_order_items_pw_gift_card( bool $include_tax, bool $only * [false, false] * [false, true] * [true, false] - * [true, true] + * [true, true] */ public function test_get_order_items_giftup( bool $include_tax, bool $only_not_settled ) { $this->markTestIncomplete();