Skip to content

Commit

Permalink
fix: format amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Aug 16, 2023
1 parent 1477f2b commit b86ac6a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use GuzzleHttp\ClientInterface;
use Carbon\Carbon;
use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\ParallexBank\Responses\TransactionResponse;
use BrokeYourBike\ParallexBank\Responses\TransferResponse;
use BrokeYourBike\ParallexBank\Responses\LoginResponse;
use BrokeYourBike\ParallexBank\Interfaces\TransactionInterface;
use BrokeYourBike\ParallexBank\Interfaces\ConfigInterface;
Expand Down Expand Up @@ -100,7 +100,7 @@ public function fetchAuthTokenRaw(): LoginResponse
return new LoginResponse($response);
}

public function postTransaction(TransactionInterface $transaction): TransactionResponse
public function transfer(TransactionInterface $transaction): TransferResponse
{
$options = [
\GuzzleHttp\RequestOptions::HEADERS => [
Expand All @@ -113,13 +113,13 @@ public function postTransaction(TransactionInterface $transaction): TransactionR
'credits' => [[
'accountToDebit' => $transaction->getAccountNumber(),
'accountName' => $transaction->getRecipientName(),
'amount' => $transaction->getAmount(),
'amount' => (string) $transaction->getAmount(),
'naration' => $transaction->getReference(),
]],
'debits' => [[
'accountToDebit' => $this->config->getDebitAccountNumber(),
'accountName' => $this->config->getDebitAccountName(),
'amount' => $transaction->getAmount(),
'amount' => (string) $transaction->getAmount(),
'naration' => $transaction->getReference(),
]],
],
Expand All @@ -131,6 +131,6 @@ public function postTransaction(TransactionInterface $transaction): TransactionR

$uri = (string) $this->resolveUriFor($this->config->getUrl(), 'api/ThirdPartyTransfer/BulkTransfer');
$response = $this->httpClient->request(HttpMethodEnum::POST->value, $uri, $options);
return new TransactionResponse($response);
return new TransferResponse($response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @author Ivan Stasiuk <[email protected]>
*/
class TransactionResponse extends JsonResponse
class TransferResponse extends JsonResponse
{
public string $responseCode;
public string $responseDescription;
Expand Down
95 changes: 95 additions & 0 deletions tests/PostTransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ParallexBank\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use Carbon\CarbonImmutable;
use BrokeYourBike\ParallexBank\Responses\TransferResponse;
use BrokeYourBike\ParallexBank\Interfaces\TransactionInterface;
use BrokeYourBike\ParallexBank\Interfaces\ConfigInterface;
use BrokeYourBike\ParallexBank\Client;

/**
* @author Ivan Stasiuk <[email protected]>
*/
class PostTransactionTest extends TestCase
{
private string $token = 'super-secure-token';

/** @test */
public function it_can_prepare_request(): void
{
$transaction = $this->getMockBuilder(TransactionInterface::class)->getMock();
$transaction->method('getReference')->willReturn('ref-123');
$transaction->method('getAccountNumber')->willReturn('12345');
$transaction->method('getRecipientName')->willReturn('John Doe');
$transaction->method('getAmount')->willReturn(50.00);
$transaction->method('getTransactionDate')->willReturn(CarbonImmutable::parse('23 Oct 2021 13:43:37'));

/** @var TransactionInterface $transaction */
$this->assertInstanceOf(TransactionInterface::class, $transaction);

$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getDebitAccountNumber')->willReturn('debit-12345');
$mockedConfig->method('getDebitAccountName')->willReturn('debit-Jane');

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"responseCode":"00",
"responseDescription":"Transaction Successful"
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/api/ThirdPartyTransfer/BulkTransfer',
[
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->token}",
],
\GuzzleHttp\RequestOptions::JSON => [
'transactionDate' => '2021-10-23',
'transactionID' => 'ref-123',
'credits' => [[
'accountToDebit' => '12345',
'accountName' => 'John Doe',
'amount' => "50.00",
'naration' => 'ref-123',
]],
'debits' => [[
'accountToDebit' => 'debit-12345',
'accountName' => 'debit-Jane',
'amount' => "50.00",
'naration' => 'ref-123',
]],
],
],
])->once()->andReturn($mockedResponse);

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
$mockedCache->method('has')->willReturn(true);
$mockedCache->method('get')->willReturn($this->token);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);

$requestResult = $api->transfer($transaction);
$this->assertInstanceOf(TransferResponse::class, $requestResult);
}
}

0 comments on commit b86ac6a

Please sign in to comment.