Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PaymentOrderCollection and PaymentOrder #55

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ PHP framework agnostic library for working with BillPlz API v3 and beyond...
- [Banking](#banking)
+ [Registration Check by Bank Account](#registration-check-by-bank-account)
+ [Get FPX Banks List](#get-fpx-banks-list)
- [Payment Order Collection](#payment-order-collection)
+ [Create a Payment Order Collection](#create-a-payment-order-collection)
+ [Get existing Payment Order Collection](#get-a-payment-order-collection)
- [Payment Order](#payment-order)
+ [Create a Payment Order](#create-payment-order)
+ [Get existing Payment Order](#get-a-payment-order)
+ [Get Payment Order Limit](#get-a-payment-order-limit)
* [Handling Response](#handling-response)
- [Getting the Response](#getting-the-response)
- [Checking the Response HTTP Status](#checking-the-response-http-status)
Expand Down Expand Up @@ -887,6 +894,84 @@ return [

> **Note**: You will hit 401, Invalid access error if you have not enabled Bank Direct by Billplz. Contact Billplz for information.

<a name="payment-order-collection"></a>

## Payment Order Collection
Create an instance of Payment Order Collection:

```php
$paymentOrderCollection = $billplz->paymentOrderCollection();
```

<a name="create-a-payment-order-collection"></a>

### Create Payment Order Collection
```php
$response = $paymentOrderCollection->create(
'My First API Payment Order Collection'
);

var_dump($response->toArray());
```

<a name="get-a-payment-order-collection"></a>

### Get Payment Order Collection
```php
$response = $paymentOrderCollection->get(
'8f4e331f-ac71-435e-a870-72fe520b4563'
);

var_dump($response->toArray());
```
<a name="payment-order"></a>

## Payment Order
Create an instance of Payment Order:

```php
$paymentOrder = $billplz->paymentOrder();
```

<a name="create-a-payment-order"></a>

### Create Payment Order
```php
$response = $paymentOrder->create(
'8f4e331f-ac71-435e-a870-72fe520b4563',
'MBBEMYKL',
'543478924652',
'820808062202',
'Michael Yap',
'Maecenas eu placerat ante.',
2000
);

var_dump($response->toArray());
```

> **Note**: You will hit 422, You do not have enough payments error if you are trying to make a payment with total that are exceeding your Payment Order Limit.

<a name="get-a-payment-order"></a>

### Get Payment Order
```php
$response = $paymentOrder->get(
'cc92738f-dfda-4969-91dc-22a44afc7e26'
);

var_dump($response->toArray());
```

<a name="get-a-payment-order-limit"></a>

### Get Payment Order Limit
```php
$response = $paymentOrder->limit();

var_dump($response->toArray());
```

## Handling Response

Every request made to Billplz would return `\Laravie\Codex\Response` which can fallback to `\Psr\Http\Message\ResponseInterface` which would allow developer to further inspect the response.
Expand Down
17 changes: 17 additions & 0 deletions examples/v5/creating-payment-order-collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
require 'vendor/autoload.php';

$api = '';
$signatureKey = '';
$paymentOrderCollectionId = '';

$billplz = Billplz\Client::make($api, $signatureKey)->useSandbox();

$response = $billplz->paymentOrderCollection()->create(
"test",
[
'callback_url' => 'http://example.com/webhook',
],
);

var_dump($response->getStatusCode(), $response->toArray());
20 changes: 20 additions & 0 deletions examples/v5/creating-payment-order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
require 'vendor/autoload.php';

$api = '';
$signatureKey = '';
$paymentOrderCollectionId = '';

$billplz = Billplz\Client::make($api, $signatureKey)->useSandbox();

$response = $billplz->paymentOrder()->create(
$paymentOrderCollectionId,
"MBBEMYKL",
"123456789012",
"123456789012",
"Ameer Shah",
"Payment Order",
1000,
);

var_dump($response->getStatusCode(), $response->toArray());
14 changes: 14 additions & 0 deletions examples/v5/getting-payment-order-collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require 'vendor/autoload.php';

$api = '';
$signatureKey = '';
$paymentOrderCollectionId = '';

$billplz = Billplz\Client::make($api, $signatureKey)->useSandbox();

$response = $billplz->paymentOrderCollection()->get(
$paymentOrderCollectionId
);

var_dump($response->getStatusCode(), $response->toArray());
11 changes: 11 additions & 0 deletions examples/v5/getting-payment-order-limit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
require 'vendor/autoload.php';

$api = '';
$signatureKey = '';

$billplz = Billplz\Client::make($api, $signatureKey)->useSandbox();

$response = $billplz->paymentOrder()->limit();

var_dump($response->getStatusCode(), $response->toArray());
14 changes: 14 additions & 0 deletions examples/v5/getting-payment-order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require 'vendor/autoload.php';

$api = '';
$signatureKey = '';
$paymentOrderId = '';

$billplz = Billplz\Client::make($api, $signatureKey)->useSandbox();

$response = $billplz->paymentOrder()->get(
$paymentOrderId
);

var_dump($response->getStatusCode(), $response->toArray());
12 changes: 12 additions & 0 deletions src/Checksum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Billplz;

class Checksum
{
//V5 API introduces new security measures.
public static function create(string $key, array $attributes): string
{
return hash_hmac('sha512', implode('', $attributes), (string) $key);
}
}
21 changes: 21 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Client extends \Laravie\Codex\Client
protected $supportedVersions = [
'v3' => 'Three',
'v4' => 'Four',
'v5' => 'Five',
];

/**
Expand Down Expand Up @@ -162,6 +163,26 @@ final public function payout(): Contracts\Payout
return $this->uses('Payout', 'v4');
}

/**
* Get payout instruction resource.
*
* @return \Billplz\Contracts\PaymentOrder
*/
final public function paymentOrder(): Contracts\PaymentOrder
{
return $this->uses('PaymentOrder', 'v5');
}

/**
* Get payout instruction collection resource.
*
* @return \Billplz\Contracts\PaymentOrderCollection
*/
final public function paymentOrderCollection(): Contracts\PaymentOrderCollection
{
return $this->uses('PaymentOrderCollection', 'v5');
}

/**
* Get bank resource.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Contracts/PaymentOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Billplz\Contracts;

use Laravie\Codex\Contracts\Request;
use Laravie\Codex\Contracts\Response;

interface PaymentOrder extends Request
{
/**
* Create a Payment Order
*
* @param int $total
* @param array<string, mixed> $optional
*/
public function create(
string $paymentOrderCollectionId,
string $bankCode,
string $bankAccountNumber,
string $identityNumber,
string $name,
string $description,
$total,
array $optional = []
): Response;

/**
* Get a Payment Order
*/
public function get(
string $paymentOrderId,
): Response;

/**
* Get a Payment Order Limit
*/
public function limit(): Response;
}
25 changes: 25 additions & 0 deletions src/Contracts/PaymentOrderCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Billplz\Contracts;

use Laravie\Codex\Contracts\Request;
use Laravie\Codex\Contracts\Response;

interface PaymentOrderCollection extends Request
{
/**
* Create a Payment Order Collection
*
* @param \Money\Money|\Duit\MYR|int $amount
* @param array<string, mixed> $optional
*/
public function create(
string $title,
array $optional = []
): Response;

/**
* Get a Payment Order Collection
*/
public function get(string $paymentOrderCollectionId): Response;
}
88 changes: 88 additions & 0 deletions src/Five/PaymentOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Billplz\Five;

use Billplz\Checksum;
use Billplz\Contracts\PaymentOrder as Contract;
use Billplz\Request;
use Laravie\Codex\Contracts\Response;

class PaymentOrder extends Request implements Contract
{
/**
* Version namespace.
*
* @var string
*/
protected $version = 'v5';

/**
* Create a Payment Order
*
* @param int $total
*/
public function create(
string $paymentOrderCollectionId,
string $bankCode,
string $bankAccountNumber,
string $identityNumber,
string $name,
string $description,
$total,
array $optional = []
): Response {
$epoch = time();

$body['payment_order_collection_id'] = $paymentOrderCollectionId;
$body['bank_code'] = $bankCode;
$body['bank_account_number'] = $bankAccountNumber;
$body['identity_number'] = $identityNumber;
$body['name'] = $name;
$body['description'] = $description;
$body['total'] = $total;
$body['epoch'] = $epoch;
$body['checksum'] = Checksum::create($this->client->getSignatureKey(), [
$paymentOrderCollectionId,
$bankAccountNumber,
$total,
$epoch
]);

$body = array_merge($body, $optional);

return $this->send('POST', 'payment_orders', [], $body);
}

/**
* Get a Payment Order
*/
public function get(
string $paymentOrderId,
): Response {
$epoch = time();

$body['payment_order_id'] = $paymentOrderId;
$body['epoch'] = $epoch;
$body['checksum'] = Checksum::create($this->client->getSignatureKey(), [
$paymentOrderId,
$epoch
]);

return $this->send('GET', "payment_orders/{$paymentOrderId}", [], $body);
}

/**
* Get a Payment Order Limit
*/
public function limit(
): Response {
$epoch = time();

$body['epoch'] = $epoch;
$body['checksum'] = Checksum::create($this->client->getSignatureKey(), [
$epoch
]);

return $this->send('GET', "payment_order_limit", [], $body);
}
}
Loading
Loading