Skip to content

Commit

Permalink
Test mobile data
Browse files Browse the repository at this point in the history
  • Loading branch information
aksalj committed Jul 26, 2019
1 parent 9f0ac64 commit 9752364
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ Instantiating the class will give you an object with available methods
- **$options:** optional associative array with the following keys:
- `idempotencyKey`: Key to use when making idempotent requests

- `mobileData($parameters, $options)`: Send mobile data to customers

- **$parameters:** associative array with the following keys:
- `productName`: Payment product on Africa's Talking. `REQUIRED`
- `recipients`: A list of recipients. Each recipient has:

- `phoneNumber`: Customer phone number (in international format). `REQUIRED`
- `quantity`: Mobile data amount. `REQUIRED`
- `unit`: Mobile data unit. Can either be `MB` or `GB`. `REQUIRED`
- `validity`: How long the mobile data is valid for. Must be one of `Daily`, `Weekly` and `Monthly`. `REQUIRED`
- `metadata`: Additional data to associate with the tranasction.

- **$options:** optional associative array with the following keys:
- `idempotencyKey`: Key to use when making idempotent requests

- `bankCheckoutCharge($parameters, $options)`: Charge a customers bank account

- **$parameters:** associative array with the following keys:
Expand Down
2 changes: 1 addition & 1 deletion example/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Simple SDK usage",
"require": {
"php": ">=5.3.0",
"africastalking/africastalking": "2.0.1",
"africastalking/africastalking": "2.2.0",
"altorouter/altorouter": "1.1.0"
}
}
58 changes: 58 additions & 0 deletions src/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,64 @@ protected function doMobileB2B($parameters, $options = [])
return $this->success($response);
}

protected function doMobileData($parameters, $options = [])
{
// Check if productName is set
if (!isset($parameters['productName'])) {
return $this->error('productName must be defined');
}
$productName = $parameters['productName'];

// Check if recipients array is provided
if (!isset($parameters['recipients'])) {
return $this->error('recipients must be an array containing phoneNumber, unit, quatity, validity and metadata');
} else if (isset($parameters['recipients']) && is_array($parameters['recipients'])) {
$recipients = $parameters['recipients'];

foreach ($recipients as $r) {
if (!isset($r['phoneNumber']) ||
!isset($r['quantity']) ||
!isset($r['unit']) ||
!isset($r['validity'])) {

return $this->error('recipients must be an array containing phoneNumber, quantity, unit, validity');
}

if (isset($r['validity'])) {
if (!in_array($r['validity'], ['Daily', 'Monthly', 'Weekly'])) {
return $this->error('validity must be one of Daily, Weekly, Monthly');
}
}

if (isset($r['unit'])) {
if (!in_array($r['unit'], ['MB', 'GB'])) {
return $this->error('unit must be one of MB, GB');
}
}
}
}

// Make request data array
$requestData = [
'username' => $this->username,
'productName' => $productName,
'recipients' => $recipients
];

$requestOptions = [
'json' => $requestData,
];

if(isset($options['idempotencyKey'])) {
$requestOptions['headers'] = [
'Idempotency-Key' => $options['idempotencyKey'],
];
}

$response = $this->client->post('mobile/data/request', $requestOptions);
return $this->success($response);
}

protected function doWalletTransfer($options)
{
// Check if productName is set
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ class Fixtures
'reason' => 'SalaryPayment',
'metadata' => ['notes' => 'Salary for January 2018']
]);
public static $MobileDataRecipients = array([
'phoneNumber' => '254718769882',
'quatity' => 7,
'unit' => 'MB',
'validity' => 'Daily',
'metadata' => ['notes' => 'Data for January 2018']
]);
}
10 changes: 10 additions & 0 deletions tests/PaymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ public function testMobileB2CIdempotency()
]);

$this->assertEquals(1, $response['data']->numQueued);
}

public function testMobileData()
{
$response = $this->client->mobileData([
'productName' => Fixtures::$productName,
'recipients' => Fixtures::$MobileDataRecipients,
]);

$this->assertEquals(1, count($response['entries']));
}

public function testMobileB2B()
Expand Down

0 comments on commit 9752364

Please sign in to comment.