From 23f16988c1d4dd94d6f3e04e2cd52526bc193c4a Mon Sep 17 00:00:00 2001 From: Cesar Garcia Date: Fri, 16 Aug 2024 13:10:31 +0200 Subject: [PATCH] Preparing to Redsys InSite (#23) --- .../PaymentNotificationController.php | 17 +--- src/Payment.php | 14 +++ src/Transaction.php | 9 +- tests/Feature/TransactionTest.php | 85 +++++++++++++++++++ 4 files changed, 107 insertions(+), 18 deletions(-) diff --git a/src/Http/Controllers/PaymentNotificationController.php b/src/Http/Controllers/PaymentNotificationController.php index a976b2e..098be41 100644 --- a/src/Http/Controllers/PaymentNotificationController.php +++ b/src/Http/Controllers/PaymentNotificationController.php @@ -7,7 +7,6 @@ use Descom\Payment\Transaction; use Illuminate\Http\Request; use Illuminate\Routing\Controller; -use Omnipay\Common\Message\ResponseInterface; class PaymentNotificationController extends Controller { @@ -17,7 +16,7 @@ public function __invoke(Request $request, string $payment_key) $response = $payment->responseCompletePurchase($request->all()); - $responseTransformed = $this->unapplyTransformer($payment, $response); + $responseTransformed = $payment->unapplyTransformer($response); $merchantId = $responseTransformed['transaction_id'] ?? $this->getTransactionId($response); $transactionModelId = $responseTransformed['transaction_model_id'] ?? null; @@ -49,18 +48,4 @@ private function getPayment(string $payment_key): Payment { return Payment::find($payment_key); } - - private function unapplyTransformer(Payment $payment, ResponseInterface $response): array - { - $transformer = $payment->transformer ?? null; - - - if ($transformer) { - $transformer = new $transformer(); - - return $transformer->unapply($response); - } - - return []; - } } diff --git a/src/Payment.php b/src/Payment.php index 44c0476..b26c97c 100644 --- a/src/Payment.php +++ b/src/Payment.php @@ -78,4 +78,18 @@ private function objectToArray(array|object $object): array { return is_array($object) ? $object : json_decode(json_encode($object), true); } + + public function unapplyTransformer(ResponseInterface $response): array + { + $transformer = $this->transformer ?? null; + + + if ($transformer) { + $transformer = new $transformer(); + + return $transformer->unapply($response); + } + + return []; + } } diff --git a/src/Transaction.php b/src/Transaction.php index 1d84449..0bc46a6 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -78,6 +78,13 @@ public function notifyPurchase(array $request): ResponseInterface $response = $this->redirectPurchase(array_merge($paymentRequest, $request)); + $this->responsePurchase($response); + + return $response; + } + + public function responsePurchase(ResponseInterface $response): void + { $this->transactionModel->gateway_id = $response->getTransactionReference(); $this->transactionModel->gateway_response = $response->getData(); $this->transactionModel->status = $response->isSuccessful() @@ -91,8 +98,6 @@ public function notifyPurchase(array $request): ResponseInterface : new TransactionDenied($this->transactionModel); event($event); - - return $response; } public function __get(string $param) diff --git a/tests/Feature/TransactionTest.php b/tests/Feature/TransactionTest.php index 511aea8..aa07840 100644 --- a/tests/Feature/TransactionTest.php +++ b/tests/Feature/TransactionTest.php @@ -10,10 +10,14 @@ use Descom\Payment\Tests\TestCase; use Descom\Payment\Transaction; use Descom\Payment\TransactionStatus; +use Descom\Redsys\Merchants\MerchantBuilder; +use Descom\Redsys\Parameters; +use Descom\Redsys\Response; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Event; use Omnipay\OfflineDummy\App\App; use Omnipay\OfflineDummy\Gateway as OfflineDummyGateway; +use Omnipay\Redsys\Message\Rest\BuildFromRedsysResponse; class TransactionTest extends TestCase { @@ -141,4 +145,85 @@ public function testPurchaseCompletedCompleted() fn (TransactionPaid $event) => $event->transactionModel()->status === TransactionStatus::PAID ); } + + public function testPurchaseFillRequestInModelWithResponse() + { + Event::fake(); + + $transaction = Transaction::for($this->payment)->create(12, 1); + + $transaction->purchase([ + 'description' => 'Test purchase', + ]); + + $transaction->responsePurchase(new BuildFromRedsysResponse(new Response( + MerchantBuilder::testing(), + new Parameters([ + 'Ds_MerchantCode' => '999008881', + 'Ds_Terminal' => '1', + 'Ds_Response' => '0000', + 'Ds_Amount' => '145', + 'Ds_Order' => '12346', + 'Ds_AuthorisationCode' => '145', + ]) + ))); + + $this->assertNotEmpty(TransactionModel::find(1)->gateway_request); + } + + public function testPurchaseCompletedFailedWithResponse() + { + Event::fake(); + + $transaction = Transaction::for($this->payment)->create(12, 1); + + $transaction->purchase([ + 'description' => 'Test purchase', + ]); + + $transaction->responsePurchase(new BuildFromRedsysResponse(new Response( + MerchantBuilder::testing(), + new Parameters([ + 'Ds_MerchantCode' => '999008881', + 'Ds_Terminal' => '1', + 'Ds_Response' => '1000', + 'Ds_Amount' => '145', + 'Ds_Order' => '12346', + 'Ds_AuthorisationCode' => '145', + ]) + ))); + + Event::assertDispatched( + TransactionDenied::class, + fn (TransactionDenied $event) => $event->transactionModel()->status === TransactionStatus::DENIED + ); + } + + public function testPurchaseCompletedCompletedWithResponse() + { + Event::fake(); + + $transaction = Transaction::for($this->payment)->create(12, 1); + + $transaction->purchase([ + 'description' => 'Test purchase', + ]); + + $transaction->responsePurchase(new BuildFromRedsysResponse(new Response( + MerchantBuilder::testing(), + new Parameters([ + 'Ds_MerchantCode' => '999008881', + 'Ds_Terminal' => '1', + 'Ds_Response' => '0000', + 'Ds_Amount' => '145', + 'Ds_Order' => '12346', + 'Ds_AuthorisationCode' => '145', + ]) + ))); + + Event::assertDispatched( + TransactionPaid::class, + fn (TransactionPaid $event) => $event->transactionModel()->status === TransactionStatus::PAID + ); + } }