Skip to content

Commit

Permalink
refactor/api agreement + test
Browse files Browse the repository at this point in the history
  • Loading branch information
0zd0 committed Jun 20, 2024
1 parent 56d02f6 commit eaf2f05
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 65 deletions.
Empty file modified bin/phpcs.sh
100644 → 100755
Empty file.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions includes/Gateways/ReepayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
1 change: 0 additions & 1 deletion includes/Gateways/Resurs.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function __construct() {
'products',
'refunds',
);
$this->logos = array( 'resurs' );

parent::__construct();

Expand Down
1 change: 0 additions & 1 deletion includes/Gateways/Swish.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function __construct() {
'products',
'refunds',
);
$this->logos = array( 'swish' );

parent::__construct();

Expand Down
1 change: 0 additions & 1 deletion includes/Gateways/Viabill.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function __construct() {
'products',
'refunds',
);
$this->logos = array( 'viabill' );

parent::__construct();

Expand Down
1 change: 0 additions & 1 deletion includes/Gateways/Vipps.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function __construct() {
'products',
'refunds',
);
$this->logos = array( 'vipps' );

parent::__construct();

Expand Down
1 change: 0 additions & 1 deletion includes/Gateways/VippsRecurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function __construct() {
'subscription_payment_method_change_admin',
'multiple_subscriptions',
);
$this->logos = array( 'vipps' );

parent::__construct();

Expand Down
41 changes: 35 additions & 6 deletions reepay-woocommerce-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
19 changes: 19 additions & 0 deletions tests/helpers/GatewayForTesting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Helper gateway
*
* @package Reepay\Checkout\Tests\Helpers
*/

namespace Reepay\Checkout\Tests\Helpers;

use Reepay\Checkout\Gateways\ReepayGateway;

/**
* Class for testing
*
* @package Reepay\Checkout\Tests\Helpers
*/
class GatewayForTesting extends ReepayGateway {

}
82 changes: 81 additions & 1 deletion tests/helpers/Reepay_UnitTestCase_Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
use Billwerk\Sdk\BillwerkClientFactory;
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 PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -89,6 +97,62 @@ trait Reepay_UnitTestCase_Trait {
*/
protected AccountService $account_service_mock;

/**
* Api agreement service mock
*
* @var AgreementService|MockObject
*/
protected AgreementService $agreement_service_mock;

/**
* Api charge service mock
*
* @var ChargeService|MockObject
*/
protected ChargeService $charge_service_mock;

/**
* Api customer service mock
*
* @var CustomerService|MockObject
*/
protected CustomerService $customer_service_mock;

/**
* Api invoice service mock
*
* @var InvoiceService|MockObject
*/
protected InvoiceService $invoice_service_mock;

/**
* Api payment method service mock
*
* @var PaymentMethodService|MockObject
*/
protected PaymentMethodService $payment_method_service_mock;

/**
* Api refund service mock
*
* @var RefundService|MockObject
*/
protected RefundService $refund_service_mock;

/**
* Api session service mock
*
* @var SessionService|MockObject
*/
protected SessionService $session_service_mock;

/**
* Api transaction service mock
*
* @var TransactionService|MockObject
*/
protected TransactionService $transaction_service_mock;

/**
* Sdk api mock
*
Expand Down Expand Up @@ -124,8 +188,24 @@ public function set_up_data() {
)
);

$this->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 );
Expand Down
Loading

0 comments on commit eaf2f05

Please sign in to comment.