Skip to content

Commit b4256b4

Browse files
committed
update: BankAccount Service and test
1 parent 3dc88e5 commit b4256b4

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

src/EventHandlers/AccountEventHandler.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public function onTimeout($transactionReference, $data): void
9191
* */
9292
public function onAuthorization(\stdClass $response, ?array $resource = null): array
9393
{
94-
$mode = $response->meta->authorization->mode;
94+
95+
$mode = $response->data->meta->authorization->mode;
9596

9697
if (property_exists($response, 'data')) {
9798
$transactionId = $response->data->id;
@@ -109,7 +110,7 @@ public function onAuthorization(\stdClass $response, ?array $resource = null): a
109110
break;
110111
case 'redirect':
111112
$data['dev_instruction'] = 'Redirect the user to the auth link for validation';
112-
$data['url'] = $response->meta->authorization->redirect;
113+
$data['url'] = $response->data->meta->authorization->redirect;
113114
break;
114115
case 'avs':
115116
throw new \Exception('AVS is currently not available via the SDK. please call the endpoint directly.');
@@ -120,7 +121,7 @@ public function onAuthorization(\stdClass $response, ?array $resource = null): a
120121
if (property_exists($response->data, 'processor_response')) {
121122
$data['instruction'] = $response->data->processor_response;
122123
} else {
123-
$data['instruction'] = $response->meta->authorization->validate_instructions;
124+
$data['instruction'] = $response->data->meta->authorization->validate_instructions;
124125
}
125126

126127
$data['validate'] = true;

src/Service/AccountPayment.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Flutterwave\EventHandlers\AccountEventHandler;
1111
use Flutterwave\Entities\Payload;
1212
use Flutterwave\Traits\Group\Charge;
13+
use Flutterwave\Util\Currency;
1314
use GuzzleHttp\Exception\GuzzleException;
1415
use InvalidArgumentException;
1516
use Psr\Http\Client\ClientExceptionInterface;
@@ -20,13 +21,14 @@ class AccountPayment extends Service implements Payment
2021
use Charge;
2122

2223
public const ENDPOINT = 'charge';
23-
public const DEBIT_NG = 'debit_ng_account';
24-
public const DEBIT_UK = 'debit_uk_account';
24+
public const DEBIT_NG = 'mono';
25+
public const DEBIT_UK = 'account-ach-uk';
2526
public const TYPE = 'account';
2627
protected array $accounts = [
27-
'NG' => self::DEBIT_NG,
28-
'UK' => self::DEBIT_UK,
29-
];
28+
Currency::NGN => self::DEBIT_NG,
29+
Currency::GBP => self::DEBIT_UK,
30+
Currency::EUR => self::DEBIT_UK
31+
];
3032
protected string $country = 'NG';
3133
private AccountEventHandler $eventHandler;
3234

@@ -55,6 +57,12 @@ public function setCountry(string $country): void
5557
*/
5658
public function initiate(Payload $payload): array
5759
{
60+
if($payload->has('currency') && !key_exists($payload->get('currency'), $this->accounts)) {
61+
$msg = 'Account Service: The Currency passed is not supported. kindy pass NGN, GBP or EUR.';
62+
$this->logger->info($msg);
63+
throw new InvalidArgumentException($msg);
64+
}
65+
5866
if ($this->checkPayloadIsValid($payload, 'account_details')) {
5967
return $this->charge($payload);
6068
}
@@ -73,14 +81,17 @@ public function charge(Payload $payload): array
7381
{
7482
$this->logger->notice('Account Service::Charging Account ...');
7583

76-
$this->checkSpecialCasesParams($payload);
84+
if($payload->has('currency') && $payload->get('currency') === Currency::NGN ) {
85+
$this->checkSpecialCasesParams($payload);
86+
}
87+
7788
$payload = $payload->toArray(self::TYPE);
7889

7990
//request payload
8091
$body = $payload;
8192

8293
//check which country was passed.
83-
$account = $this->accounts[$payload['country']];
94+
$account = $this->accounts[$payload['currency']];
8495

8596
unset($body['country']);
8697
unset($body['address']);

src/Service/AchPayment.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Flutterwave\EventHandlers\AchEventHandler;
1111
use Flutterwave\Entities\Payload;
1212
use Flutterwave\Traits\Group\Charge;
13+
use Flutterwave\Util\Currency;
1314
use GuzzleHttp\Exception\GuzzleException;
1415
use stdClass;
1516

@@ -18,12 +19,11 @@ class AchPayment extends Service implements Payment
1819
use Charge;
1920

2021
public const TYPE = 'ach_payment';
21-
public const USD = 'USD';
22-
public const ZAR = 'ZAR';
22+
2323
protected string $country = 'US';
2424
protected array $currency = [
25-
self::USD => 'US',
26-
self::ZAR => 'ZA',
25+
Currency::USD => 'US',
26+
Currency::ZAR => 'ZA',
2727
];
2828
private AchEventHandler $eventHandler;
2929

src/Util/Currency.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ class Currency
1919
public const XAF = 'XAF';
2020
public const XOF = 'XOF';
2121
public const EGP = 'EGP';
22+
public const GBP = 'GBP';
2223
}

tests/Unit/Service/AccountTest.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function setUp(): void
1515
Flutterwave::bootstrap();
1616
}
1717

18-
public function testAuthModeReturn()
18+
public function testNgnAuthModeReturn()
1919
{
2020
//currently returning "Sorry, we could not connect to your bank";
2121

@@ -41,11 +41,8 @@ public function testAuthModeReturn()
4141

4242
$data['customer'] = $customerObj;
4343
$payload = $accountpayment->payload->create($data);
44-
$this->expectException(\Exception::class);
4544
$result = $accountpayment->initiate($payload);
46-
47-
//check mode returned is either OTP or Redirect
48-
// $this->assertTrue($result['mode'] === AuthMode::OTP || $result['mode'] === AuthMode::REDIRECT );
45+
$this->assertTrue( $result['mode'] === AuthMode::REDIRECT );
4946
}
5047

5148
public function testInvalidParam()
@@ -69,4 +66,32 @@ public function testInvalidParam()
6966
$this->expectException(\InvalidArgumentException::class);
7067
$result = $accountpayment->initiate($payload);
7168
}
69+
70+
public function testUKBankAccountAuthMode() {
71+
$data = [
72+
"amount" => 2000,
73+
"currency" => Currency::NGN,
74+
"tx_ref" => uniqid().time(),
75+
"additionalData" => [
76+
"account_details" => [
77+
"account_bank" => "044",
78+
"account_number" => "0690000034",
79+
"country" => "UK" //or EU
80+
]
81+
],
82+
];
83+
84+
$accountpayment = \Flutterwave\Flutterwave::create("account");
85+
$customerObj = $accountpayment->customer->create([
86+
"full_name" => "Jake Jesulayomi Ola",
87+
"email" => "[email protected]",
88+
"phone" => "+2349067985861"
89+
]);
90+
91+
$data['customer'] = $customerObj;
92+
$payload = $accountpayment->payload->create($data);
93+
$result = $accountpayment->initiate($payload);
94+
95+
$this->assertTrue( $result['mode'] === AuthMode::REDIRECT );
96+
}
7297
}

0 commit comments

Comments
 (0)