From 60ec32f6a787252ff4c080332c7ba90d6dd313e4 Mon Sep 17 00:00:00 2001 From: Robert Kujawa Date: Thu, 27 Apr 2023 07:23:01 -0600 Subject: [PATCH 1/4] Add `setUp()` to response --- src/PaymentResponse.php | 12 ++++++++++++ src/stubs/payment-response.stub | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/PaymentResponse.php b/src/PaymentResponse.php index d9e427a..42b32d9 100644 --- a/src/PaymentResponse.php +++ b/src/PaymentResponse.php @@ -93,6 +93,18 @@ public function __construct($rawResponse, $additionalInformation = null) { $this->rawResponse = $rawResponse; $this->additionalInformation = $additionalInformation; + + $this->setUp(); + } + + /** + * Set up the response. + * + * @return void + */ + protected function setUp() + { + // } /** diff --git a/src/stubs/payment-response.stub b/src/stubs/payment-response.stub index c2a2aad..1fdd402 100644 --- a/src/stubs/payment-response.stub +++ b/src/stubs/payment-response.stub @@ -6,6 +6,16 @@ use Payavel\Checkout\PaymentResponse; class {{ name }}PaymentResponse extends PaymentResponse { + /** + * Set up the response. + * + * @return void + */ + protected function setUp() + { + // + } + /** * Determines the status code based on the request's raw response. * From b8b553d7b7b39e8a6a567de2aa9efe40a79fb758 Mon Sep 17 00:00:00 2001 From: Robert Kujawa Date: Fri, 28 Apr 2023 08:30:49 -0600 Subject: [PATCH 2/4] Fix alternative response :sweat_smile: --- tests/GatewayTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/GatewayTestCase.php b/tests/GatewayTestCase.php index 34bb6a0..3021fc2 100644 --- a/tests/GatewayTestCase.php +++ b/tests/GatewayTestCase.php @@ -244,7 +244,7 @@ class AlternativePaymentRequest extends PaymentRequest { public function authorize($data, Billable $billable = null) { - return new TestPaymentResponse([]); + return new AlternativePaymentResponse([]); } } From 994e6acc138052253a13fa4073213804fb6c2784 Mon Sep 17 00:00:00 2001 From: Robert Kujawa Date: Fri, 28 Apr 2023 08:32:05 -0600 Subject: [PATCH 3/4] Add `reset()` function to PaymentService --- src/Facades/Payment.php | 1 + src/PaymentService.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Facades/Payment.php b/src/Facades/Payment.php index 7ca129f..413d839 100644 --- a/src/Facades/Payment.php +++ b/src/Facades/Payment.php @@ -13,6 +13,7 @@ * @method static \Payavel\Checkout\PaymentGateway merchant($merchant) * @method static \Payavel\Checkout\Contracts\Merchantable getMerchant() * @method static void setMerchant($merchant, $strict = true) + * @method static void reset() * @method static string|int|\Payavel\Checkout\Contracts\Merchantable getDefaultMerchant() * @method static \Payavel\Checkout\PaymentResponse getWallet(\Payavel\Checkout\Models\Wallet $wallet) * @method static \Payavel\Checkout\PaymentResponse getPaymentMethod(\Payavel\Checkout\Models\PaymentMethod $paymentMethod) diff --git a/src/PaymentService.php b/src/PaymentService.php index 87927e9..a379b81 100644 --- a/src/PaymentService.php +++ b/src/PaymentService.php @@ -205,4 +205,16 @@ protected function setGateway() $this->gateway = new $gateway($provider, $merchant); } + + /** + * Reset the payment service to it's defaults. + * + * @return void + */ + public function reset() + { + $this->provider = null; + $this->merchant = null; + $this->gateway = null; + } } From ac2085fe39aa43a75cb99e08623902ec0abb4c09 Mon Sep 17 00:00:00 2001 From: Robert Kujawa Date: Fri, 28 Apr 2023 08:32:19 -0600 Subject: [PATCH 4/4] Add test for `reset()` --- tests/Unit/TestPaymentGateway.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/Unit/TestPaymentGateway.php b/tests/Unit/TestPaymentGateway.php index 18852e3..cc20f1a 100644 --- a/tests/Unit/TestPaymentGateway.php +++ b/tests/Unit/TestPaymentGateway.php @@ -8,7 +8,9 @@ use Payavel\Checkout\Models\PaymentTransaction; use Payavel\Checkout\Models\Wallet; use Payavel\Checkout\PaymentResponse; +use Payavel\Checkout\Tests\AlternativePaymentResponse; use Payavel\Checkout\Tests\GatewayTestCase; +use Payavel\Checkout\Tests\TestPaymentResponse; use Payavel\Checkout\Tests\User; class TestPaymentGateway extends GatewayTestCase @@ -24,7 +26,7 @@ public function set_provider_and_merchant_fluently() /** @test */ public function setting_invalid_driver_throws_exception() { - config(['payment.defaults.driver' => 'fake']); + config(['payment.defaults.driver' => 'invalid']); $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid checkout driver provided.'); @@ -38,7 +40,7 @@ public function setting_invalid_provider_throws_exception() $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid checkout provider.'); - Payment::setProvider('fake'); + Payment::setProvider('invalid'); } /** @test */ @@ -47,7 +49,7 @@ public function setting_invalid_merchant_throws_exception() $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid checkout merchant.'); - Payment::setMerchant('faker'); + Payment::setMerchant('invalid'); } /** @test */ @@ -60,6 +62,18 @@ public function setting_incompatible_merchant_provider_throws_exception() Payment::authorize([]); } + /** @test */ + public function resetting_payment_service_to_default_configuration() + { + Payment::provider('alternative')->merchant('alternate'); + + $this->assertEquals(AlternativePaymentResponse::class, get_class(Payment::authorize([]))); + + Payment::reset(); + + $this->assertEquals(TestPaymentResponse::class, get_class(Payment::authorize([]))); + } + /** @test */ public function payment_service_throws_exception_when_test_mode_gateway_does_not_exist() {