Skip to content

Commit

Permalink
Preparing to Redsys InSite (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesargb authored Aug 16, 2024
1 parent 2a63dfa commit 23f1698
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
17 changes: 1 addition & 16 deletions src/Http/Controllers/PaymentNotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -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 [];
}
}
14 changes: 14 additions & 0 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}
9 changes: 7 additions & 2 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -91,8 +98,6 @@ public function notifyPurchase(array $request): ResponseInterface
: new TransactionDenied($this->transactionModel);

event($event);

return $response;
}

public function __get(string $param)
Expand Down
85 changes: 85 additions & 0 deletions tests/Feature/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit 23f1698

Please sign in to comment.