Skip to content

Commit

Permalink
feat(cardless credit): calculate payment type (#110)
Browse files Browse the repository at this point in the history
* feat(cardless credit): calculate payment type

Added calculate payment type implementation, example, and unit test

* fix(cardless credit): rename calculate payment type

Rename calcPaymentType to calculatePaymentTypes

* docs(README) add cardless credit payment calculation

Co-authored-by: Stanley Nguyen <[email protected]>
  • Loading branch information
asokawotulo and stanleynguyen authored Nov 7, 2020
1 parent ad66cb8 commit cb65fee
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 59 deletions.
149 changes: 90 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,66 @@

This library is the abstraction of Xendit API for access from applications written with PHP.

- [Documentation](#documentation)
- [Installation](#installation)
- [Usage](#usage)
- [Methods' Signature and Examples](#methods-signature-and-examples)
- [Balance](#balance)
- [Get Balance](#get-balance)
- [Cards](#cards)
- [Create Charge](#create-charge)
- [Reverse Authorization](#reverse-authorization)
- [Capture Charge](#capture-charge)
- [Get Charge](#get-charge)
- [Create Refund](#create-refund)
- [Cardless Credit](#cardless-credit)
- [Create Cardless Credit Payment](#create-cardless-credit-payment)
- [Disbursements](#disbursements)
- [Create Disbursement](#create-disbursement)
- [Create Batch Disbursement](#create-batch-disbursement)
- [Get Disbursement by ID](#get-disbursement-by-id)
- [Get Disbursement by External ID](#get-disbursement-by-external-id)
- [Get Disbursement Available Banks](#get-disbursement-available-banks)
- [E-Wallets](#e-wallets)
- [Create Payment](#create-payment)
- [Get Payment Status](#get-payment-status)
- [Invoice](#invoice)
- [Create Invoice](#create-invoice)
- [Get Invoice](#get-invoice)
- [Get All Invoice](#get-all-invoice)
- [Expire Invoice](#expire-invoice)
- [Payouts](#payouts)
- [Create a Payout](#create-payout)
- [Get a Payout](#get-payout)
- [Void a Payout](#void-payout)
- [QR Code](#qr-code)
- [Create a QR Code](#create-a-qr-code)
- [Get QR Code](#get-qr-code)
- [Recurring](#recurring-payments)
- [Create a Recurring Payment](#create-a-recurring-payment)
- [Get a Recurring Payment](#get-a-recurring-payment)
- [Edit a Recurring Payment](#edit-recurring-payment)
- [Pause a Recurring Payment](#pause-recurring-payment)
- [Stop a Recurring Payment](#stop-recurring-payment)
- [Resume a Recurring Payment](#resume-recurring-payment)
- [Retail Outlets](#retail-outlets)
- [Create Fixed Payment Code](#create-fixed-payment-code)
- [Update Fixed Payment Code](#update-fixed-payment-code)
- [Get Fixed Payment Code](#get-fixed-payment-code)
- [Virtual Accounts](#virtual-accounts)
- [Create Fixed Virtual Account](#create-fixed-virtual-account)
- [Get Virtual Account Bank](#get-virtual-account-bank)
- [Get Fixed Virtual Account](#get-fixed-virtual-account)
- [Update Fixed Virtual Account](#update-fixed-virtual-account)
- [Get Fixed Virtual Account Payment](#get-fixed-virtual-account-payment)
- [Exceptions](#exceptions)
- [InvalidArgumentException](#invalidargumentexception)
- [ApiException](#apiexception)
- [Contributing](#contributing)
- [Test](#tests)
- [Running test suite](#running-test-suite)
- [Running examples](#running-examples)
- [Documentation](#documentation)
- [Installation](#installation)
- [Usage](#usage)
- [Methods' Signature and Examples](#methods-signature-and-examples)
- [Balance](#balance)
- [Get Balance](#get-balance)
- [Cards](#cards)
- [Create Charge](#create-charge)
- [Reverse Authorization](#reverse-authorization)
- [Capture Charge](#capture-charge)
- [Get Charge](#get-charge)
- [Create Refund](#create-refund)
- [Cardless Credit](#cardless-credit)
- [Create Cardless Credit Payment](#create-cardless-credit-payment)
- [Calculate Payment Types](#calculate-payment-types)
- [Disbursements](#disbursements)
- [Create Disbursement](#create-disbursement)
- [Create Batch Disbursement](#create-batch-disbursement)
- [Get Disbursement by ID](#get-disbursement-by-id)
- [Get Disbursement by External ID](#get-disbursement-by-external-id)
- [Get Disbursement Available Banks](#get-disbursement-available-banks)
- [E-Wallets](#e-wallets)
- [Create Payment](#create-payment)
- [Get Payment Status](#get-payment-status)
- [Invoice](#invoice)
- [Create Invoice](#create-invoice)
- [Get Invoice](#get-invoice)
- [Get All Invoice](#get-all-invoice)
- [Expire Invoice](#expire-invoice)
- [Payouts](#payouts)
- [Create a Payout](#create-payout)
- [Get a Payout](#get-payout)
- [Void a Payout](#void-payout)
- [QR Code](#qr-code)
- [Create a QR Code](#create-a-qr-code)
- [Get QR Code](#get-qr-code)
- [Recurring](#recurring-payments)
- [Create a Recurring Payment](#create-a-recurring-payment)
- [Get a Recurring Payment](#get-a-recurring-payment)
- [Edit a Recurring Payment](#edit-recurring-payment)
- [Pause a Recurring Payment](#pause-recurring-payment)
- [Stop a Recurring Payment](#stop-recurring-payment)
- [Resume a Recurring Payment](#resume-recurring-payment)
- [Retail Outlets](#retail-outlets)
- [Create Fixed Payment Code](#create-fixed-payment-code)
- [Update Fixed Payment Code](#update-fixed-payment-code)
- [Get Fixed Payment Code](#get-fixed-payment-code)
- [Virtual Accounts](#virtual-accounts)
- [Create Fixed Virtual Account](#create-fixed-virtual-account)
- [Get Virtual Account Bank](#get-virtual-account-bank)
- [Get Fixed Virtual Account](#get-fixed-virtual-account)
- [Update Fixed Virtual Account](#update-fixed-virtual-account)
- [Get Fixed Virtual Account Payment](#get-fixed-virtual-account-payment)
- [Exceptions](#exceptions)
- [InvalidArgumentException](#invalidargumentexception)
- [ApiException](#apiexception)
- [Contributing](#contributing)
- [Test](#tests)
- [Running test suite](#running-test-suite)
- [Running examples](#running-examples)

---

Expand Down Expand Up @@ -289,6 +290,34 @@ $createPayment = \Xendit\CardlessCredit::create($params);
var_dump($createPayment);
```

#### Calculate Payment Types

```php
\Xendit\CardlessCredit::calculatePaymentTypes(array $params);
```

Usage example:

```php
$params = [
'cardless_credit_type' => 'KREDIVO',
'amount' => 2000000,
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 1000000,
'type' => 'Smartphone',
'url' => 'http://example.com/phone/phone_case',
'quantity' => 2
]
]
];

$calculatePaymentTypes = \Xendit\CardlessCredit::calculatePaymentTypes($params);
var_dump($calculatePaymentTypes);
```

### Disbursements

#### Create Disbursement
Expand Down Expand Up @@ -637,6 +666,7 @@ var_dump($qr_code)
```

Usage example:

```php
$qr_code = \Xendit\QRCode::get('external_123');
var_dump($qr_code);
Expand Down Expand Up @@ -920,6 +950,7 @@ try {
For any requests, bugs, or comments, please open an [issue](https://github.com/xendit/xendit-php-clients/issues) or [submit a pull request](https://github.com/xendit/xendit-php-clients/pulls).

### Installing Packages

Before you start to code, run this command to install all of the required packages. Make sure you have `composer` installed in your computer

```bash
Expand Down
18 changes: 18 additions & 0 deletions examples/CardlessCreditExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,21 @@

$createPayment = \Xendit\CardlessCredit::create($params);
var_dump($createPayment);

$params = [
'cardless_credit_type' => 'KREDIVO',
'amount' => 2000000,
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 1000000,
'type' => 'Smartphone',
'url' => 'http://example.com/phone/phone_case',
'quantity' => 2
]
]
];

$calculatePaymentTypes = \Xendit\CardlessCredit::calculatePaymentTypes($params);
var_dump($calculatePaymentTypes);
22 changes: 22 additions & 0 deletions src/CardlessCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,26 @@ public static function createReqParams()
'callback_url'
];
}

/**
* Calculate payment types
*
* @param array $params user's parameters
* @return array
* @throws ApiException
*/
public static function calculatePaymentTypes($params = [])
{
$requiredParams = [
'cardless_credit_type',
'amount',
'items',
];

self::validateParams($params, $requiredParams);

$url = static::classUrl() . '/payment-types';

return static::_request('POST', $url, $params);
}
}
104 changes: 104 additions & 0 deletions tests/Xendit/CardlessCreditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,108 @@ public function testIsCreatableThrowInvalidArgumentException()

CardlessCredit::create($params);
}

/**
* Calculate payment types
* Should pass
*
* @return void
*/
public function testIsGettable()
{
$params = [
'cardless_credit_type' => 'KREDIVO',
'amount' => 2000000,
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 1000000,
'type' => 'Smartphone',
'url' => 'http://example.com/phone/phone_case',
'quantity' => 2
]
]
];

$response = [
"message" => "Available payment types are listed.",
"payments" => [
[
"raw_monthly_installment" => 187534.66680439,
"name" => "Bayar dalam 6 bulan",
"amount" => 1000000,
"installment_amount" => 1125240,
"raw_amount" => 1000000,
"rate" => 2.95,
"down_payment" => 0,
"monthly_installment" => 187540,
"discounted_monthly_installment" => 0,
"tenure" => 6,
"id" => "6_months",
],
[
"raw_monthly_installment" => 1020000,
"name" => "Bayar dalam 30 hari",
"amount" => 1020000,
"installment_amount" => 1020000,
"raw_amount" => 1020000,
"rate" => 0,
"down_payment" => 0,
"monthly_installment" => 1020000,
"discounted_monthly_installment" => 0,
"tenure" => 1,
"id" => "30_days",
],
[
"raw_monthly_installment" => 356786.46273702,
"name" => "Bayar dalam 3 bulan",
"amount" => 1000000,
"installment_amount" => 1070370,
"raw_amount" => 1000000,
"rate" => 2.95,
"down_payment" => 0,
"monthly_installment" => 356790,
"discounted_monthly_installment" => 0,
"tenure" => 3,
"id" => "3_months",
],
],
];

$this->stubRequest(
'POST',
'/cardless-credit/payment-types',
$params,
[],
$response
);

$result = CardlessCredit::calculatePaymentTypes($params);

$this->assertArrayHasKey('message', $result);
$this->assertArrayHasKey('payments', $result);
}

public function testIsGettableThrowApiException()
{
$this->expectException(\Xendit\Exceptions\ApiException::class);

$params = [
'cardless_credit_type' => 'KREDIVO',
'amount' => 2000000,
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 1000000,
'type' => 'Smartphone',
'url' => 'http://example.com/phone/phone_case',
'quantity' => 2
]
]
];

CardlessCredit::calculatePaymentTypes($params);
}
}

0 comments on commit cb65fee

Please sign in to comment.