Xendit API PHP Client
This library is the abstraction of Xendit API for access from applications written with PHP.
- Documentation
- Installation
- Usage
- Methods' Signature and Examples
- Exceptions
- Contributing
Documentation
For the API documentation, check Xendit API Reference.
Installation
Install xendit-php-clients with composer by following command:
composer require xendit/xendit-php
or add it manually in your composer.json
file.
Update from v1.4.0 to v2.0.0
To update xendit-php-clients with composer, use the following command:
composer update xendit/xendit-php
To migrate, see MIGRATE.md for more information.
Usage
Configure package with your account's secret key obtained from Xendit Dashboard.
Xendit::setApiKey('secretKey');
See example codes for more details.
Use Custom HTTP Client
A custom HTTP Client that implements HttpClientInterface can be injected like so
Xendit::setHttpClient($client);
Checkout custom http client example for implementation reference.
Methods' Signature and Examples
Balance
Get Balance
\Xendit\Balance::getBalance(string $account_type);
Usage example:
$getBalance = \Xendit\Balance::getBalance('CASH');
var_dump($getBalance);
Cards
Create Charge
\Xendit\Cards::create(array $params);
Usage example:
$params = [
'token_id' => '5e2e8231d97c174c58bcf644',
'external_id' => 'card_' . time(),
'authentication_id' => '5e2e8658bae82e4d54d764c0',
'amount' => 100000,
'card_cvn' => '123',
'capture' => false
];
$createCharge = \Xendit\Cards::create($params);
var_dump($createCharge);
Reverse Authorization
\Xendit\Cards::reverseAuthorization(string $id, array $params);
Usage example:
$id = 'charge-id';
$params = ['external_id' => 'ext-id'];
$reverseAuth = \Xendit\Cards::reverseAuthorization(
$id,
$params
);
var_dump($reverseAuth);
Capture Charge
\Xendit\Cards::capture(string $id, array $params);
Usage example:
$id = 'charge-id';
$params = ['amount' => 100000];
$captureCharge = \Xendit\Cards::capture($id, $params);
var_dump($captureCharge);
Get Charge
\Xendit\Cards::retrieve(string $id);
Usage example:
$id = 'charge-id';
$getCharge = \Xendit\Cards::retrieve($id);
var_dump($getCharge);
Create Refund
\Xendit\Cards::createRefund(string $id, array $params);
Usage examples
Without idempotency key:
$params = [
'external_id' => 'ext-id',
'amount' => 20000
];
$refund = \Xendit\Cards::createRefund($id, $params);
var_dump($refund);
With idempotency key:
$params = [
'external_id' => 'ext-id',
'amount' => 20000,
'X-IDEMPOTENCY-KEY' => 'unique-id'
];
$refund = \Xendit\Cards::createRefund($id, $params);
var_dump($refund);
Create Promotion
\Xendit\Promotion::create(array $params);
usage examples:
$params = [
'reference_id' => 'reference_123',
'description' => 'test promotion',
'currency' => 'IDR',
'start_time' => '2021-01-01T00:00:00.000Z',
'end_time' => '2021-01-02T00:00:00.000Z',
'promo_code' => 'testpromo',
'discount_amount' => 5000
];
$createPromotion = \Xendit\Promotion::create($params);
var_dump($createPromotion);
Cardless Credit
Create Cardless Credit Payment
\Xendit\CardlessCredit::create(array $params);
Usage example:
$params = [
'cardless_credit_type' => 'KREDIVO',
'external_id' => 'test-cardless-credit-02',
'amount' => 800000,
'payment_type' => '3_months',
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 200000,
'type' => 'Smartphone',
'url' => 'http=>//example.com/phone/phone_case',
'quantity' => 2
],
[
'id' => '234567',
'name' => 'Bluetooth Headset',
'price' => 400000,
'type' => 'Audio',
'url' => 'http=>//example.com/phone/bluetooth_headset',
'quantity' => 1
]
],
'customer_details' => [
'first_name' => 'customer first name',
'last_name' => 'customer last name',
'email' => '[email protected]',
'phone' => '081513114262'
],
'shipping_address' => [
'first_name' => 'first name',
'last_name' => 'last name',
'address' => 'Jalan Teknologi No. 12',
'city' => 'Jakarta',
'postal_code' => '12345',
'phone' => '081513114262',
'country_code' => 'IDN'
],
'redirect_url' => 'https://example.com',
'callback_url' => 'http://example.com/callback-cardless-credit'
];
$createPayment = \Xendit\CardlessCredit::create($params);
var_dump($createPayment);
Calculate Payment Types
\Xendit\CardlessCredit::calculatePaymentTypes(array $params);
Usage example:
$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
\Xendit\Disbursements::create(array $params);
Usage examples
Without idempotency key:
$params = [
'external_id' => 'disb-12345678',
'amount' => 15000,
'bank_code' => 'BCA',
'account_holder_name' => 'Joe',
'account_number' => '1234567890',
'description' => 'Disbursement from Example'
];
$createDisbursements = \Xendit\Disbursements::create($params);
var_dump($createDisbursements);
With idempotency key:
$params = [
'external_id' => 'disb-12345678',
'amount' => 15000,
'bank_code' => 'BCA',
'account_holder_name' => 'Joe',
'account_number' => '1234567890',
'description' => 'Disbursement from Example',
'X-IDEMPOTENCY-KEY' => 'unique-id'
];
$createDisbursements = \Xendit\Disbursements::create($params);
var_dump($createDisbursements);
Create Batch Disbursement
\Xendit\Disbursements::createBatch(array $params);
Usage example:
$batch_params = [
'reference' => 'disb_batch-12345678',
'disbursements' => [
[
'amount' => 20000,
'bank_code' => 'BCA',
'bank_account_name' => 'Fadlan',
'bank_account_number' => '1234567890',
'description' => 'Batch Disbursement',
'external_id' => 'disbursement-1'
],
[
'amount' => 30000,
'bank_code' => 'MANDIRI',
'bank_account_name' => 'Lutfi',
'bank_account_number' => '1234567891',
'description' => 'Batch Disbursement with email notifications',
'external_id' => 'disbursement-2',
'email_to' => ['[email protected]'],
'email_cc' => ['[email protected]'],
'email_bcc' => ['[email protected]', '[email protected]']
]
]
];
$createBatchDisbursements = \Xendit\Disbursements::createBatch($batch_params);
var_dump($createBatchDisbursements);
Get Disbursement by ID
\Xendit\Disbursements::retrieve(string $id);
Usage example:
$id = 'disbursements-id';
$getDisbursements = \Xendit\Disbursements::retrieve($id);
var_dump($getDisbursements);
Get Disbursement by External ID
\Xendit\Disbursements::retrieveExternal(string $external_id);
Usage example:
$external_id = 'disbursements-ext-id';
$getDisbursementsByExt = \Xendit\Disbursements::retrieveExternal($external_id);
var_dump($getDisbursementsByExt);
Get Disbursement Available Banks
\Xendit\Disbursements::getAvailableBanks();
Usage example:
$getDisbursementsBanks = \Xendit\Disbursements::getAvailableBanks();
var_dump($getDisbursementsBanks);
E-Wallets
Create E-Wallet Charge
\Xendit\EWallets::createEWalletCharge(array $params);
For more information about the params, please check Xendit API Reference - E-Wallets.
Usage example:
$ewalletChargeParams = [
'reference_id' => 'test-reference-id',
'currency' => 'IDR',
'amount' => 50000,
'checkout_method' => 'ONE_TIME_PAYMENT',
'channel_code' => 'ID_SHOPEEPAY',
'channel_properties' => [
'success_redirect_url' => 'https://yourwebsite.com/order/123',
],
'metadata' => [
'meta' => 'data'
]
];
$createEWalletCharge = \Xendit\EWallets::createEWalletCharge($ewalletChargeParams);
var_dump($createEWalletCharge);
Get E-Wallet Charge Status
\Xendit\EWallets::getEWalletChargeStatus(string $charge_id);
Usage example:
$charge_id = 'ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c';
$getEWalletChargeStatus = \Xendit\EWallets::getEWalletChargeStatus($charge_id);
var_dump($getEWalletChargeStatus);
Invoice
Create Invoice
\Xendit\Invoice::create(array $params);
Usage example:
$params = ['external_id' => 'demo_147580196270',
'payer_email' => '[email protected]',
'description' => 'Trip to Bali',
'amount' => 32000
];
$createInvoice = \Xendit\Invoice::create($params);
var_dump($createInvoice);
Get Invoice
\Xendit\Invoice::retrieve(string $id);
Usage example:
$id = 'invoice-id';
$getInvoice = \Xendit\Invoice::retrieve($id);
var_dump($getInvoice);
Get All Invoice
\Xendit\Invoice::retrieveAll();
Usage example:
$getAllInvoice = \Xendit\Invoice::retrieveAll();
var_dump(($getAllInvoice));
Expire Invoice
\Xendit\Invoice::expireInvoice(string $id);
Usage example:
$id = 'invoice-id';
$expireInvoice = \Xendit\Invoice::expireInvoice($id);
var_dump($expireInvoice);
Payouts
Create Payout
\Xendit\Payouts::create(array $params);
Usage example:
$params = [
'external_id' => 'payouts-123456789',
'amount' => 50000
];
$createPayout = \Xendit\Payouts::create($params);
var_dump($createPayout);
Get Payout
\Xendit\Payouts::retrieve(string $id);
Usage example:
$id = 'payout-id';
$getPayout = \Xendit\Payouts::retrieve($id);
var_dump($getPayout);
Void Payout
\Xendit\Payouts::void(string $id);
Usage example:
$id = 'payout-id';
$voidPayout = \Xendit\Payouts::void($id);
var_dump($voidPayout);
QR Code
Create a QR Code
\Xendit\QRCode::create(array $params);
Usage example:
$params = [
'external_id' => 'demo_123456',
'type' => 'STATIC',
'callback_url' => 'https://webhook.site',
'amount' => 10000,
];
$qr_code = \Xendit\QRCode::create($params);
var_dump($qr_code)
Get QR Code
\Xendit\QRCode::get(string $external_id);
Usage example:
$qr_code = \Xendit\QRCode::get('external_123');
var_dump($qr_code);
Recurring Payments
Create a Recurring Payment
\Xendit\Recurring::create(array $params);
Usage example:
$params = [
'external_id' => 'demo_147580196270',
'payer_email' => '[email protected]',
'description' => 'Trip to Bali',
'amount' => 32000,
'interval' => 'MONTH',
'interval_count' => 1
];
$createRecurring = \Xendit\Recurring::create($params);
var_dump($createRecurring);
Get a Recurring Payment
\Xendit\Recurring::retrieve(string $id);
Usage example:
$id = 'recurring-payment-id';
$getRecurring = \Xendit\Recurring::retrieve($id);
var_dump($getRecurring);
Edit Recurring Payment
\Xendit\Recurring::update(string $id, array $params);
Usage example:
$id = 'recurring-payment-id';
$params = ['amount' => 10000];
$editRecurring = \Xendit\Recurring::update($id, $params);
var_dump($editRecurring);
Stop Recurring Payment
\Xendit\Recurring::stop(string $id);
Usage example:
$id = 'recurring-payment-id';
$stopRecurring = \Xendit\Recurring::stop($id);
var_dump($stopRecurring);
Pause Recurring Payment
\Xendit\Recurring::pause(string $id);
Usage example:
$id = 'recurring-payment-id';
$pauseRecurring = \Xendit\Recurring::pause($id);
var_dump($pauseRecurring);
Resume Recurring Payment
\Xendit\Recurring::resume(string $id);
Usage example:
$id = 'recurring-payment-id';
$resumeRecurring = \Xendit\Recurring::resume($id);
var_dump($resumeRecurring);
Retail Outlets
Create Fixed Payment Code
\Xendit\Retail::create(array $params);
Usage example:
$params = [
'external_id' => 'TEST-123456789',
'retail_outlet_name' => 'ALFAMART',
'name' => 'JOHN DOE',
'expected_amount' => 25000
];
$createFPC = \Xendit\Retail::create($params);
var_dump($createFPC);
Update Fixed Payment Code
\Xendit\Retail::update(string $id, array $params);
Usage example:
$id = 'FPC-id';
$updateParams = ['expected_amount' => 20000];
$updateFPC = \Xendit\Retail::update($id, $updateParams);
var_dump($updateFPC);
Get Fixed Payment Code
\Xendit\Retail::retrieve(string $id);
Usage example:
$id = 'FPC-id';
$getFPC = \Xendit\Retail::retrieve($id);
var_dump($getFPC);
Virtual Accounts
Create Fixed Virtual Account
\Xendit\VirtualAccounts::create(array $params);
Usage example:
$params = ["external_id" => "VA_fixed-12341234",
"bank_code" => "MANDIRI",
"name" => "Steve Wozniak"
];
$createVA = \Xendit\VirtualAccounts::create($params);
var_dump($createVA);
Get Virtual Account Bank
\Xendit\VirtualAccounts::getVABanks();
Usage example:
$getVABanks = \Xendit\VirtualAccounts::getVABanks();
var_dump($getVABanks);
Get Fixed Virtual Account
\Xendit\VirtualAccounts::retrieve(string $id);
Usage example:
$id = 'VA-id';
$getVA = \Xendit\VirtualAccounts::retrieve($id);
var_dump($getVA);
Update Fixed Virtual Account
\Xendit\VirtualAccounts::update(string $id, array $params);
Usage example:
$id = 'VA-id';
$updateParams = ["suggested_amount" => 1000];
$updateVA = \Xendit\VirtualAccounts::update($id, $updateParams);
var_dump($updateVA);
Get Fixed Virtual Account Payment
\Xendit\VirtualAccounts::getFVAPayment(string $paymentID);
Usage example:
$paymentID = 'payment-ID';
$getFVAPayment = \Xendit\VirtualAccounts::getFVAPayment($paymentID);
var_dump($getFVAPayment);
Exceptions
InvalidArgumentException
InvalidArgumentException
will be thrown if the argument provided by user is not sufficient to create the request.
For example, there are required arguments such as external_id
, payer_email
, description
, and amount
to create an invoice. If user lacks one or more arguments when attempting to create one, InvalidArgumentException
will be thrown.
InvalidArgumentException
is derived from PHP's InvalidArgumentException
. For more information about this Exception methods and properties, please check PHP Documentation.
ApiException
ApiException
wraps up Xendit API error. This exception will be thrown if there are errors from Xendit API side, e.g. get fixed virtual account with invalid id
.
To get exception message:
try {
$getInvoice = \Xendit\Invoice::retrieve('123');
} catch (\Xendit\Exceptions\ApiException $e) {
var_dump($e->getMessage());
}
To get exception HTTP error code:
try {
$getInvoice = \Xendit\Invoice::retrieve('123');
} catch (\Xendit\Exceptions\ApiException $e) {
var_dump($e->getCode());
}
To get exception Xendit API error code:
try {
$getInvoice = \Xendit\Invoice::retrieve('123');
} catch (\Xendit\Exceptions\ApiException $e) {
var_dump($e->getErrorCode());
}
Contributing
For any requests, bugs, or comments, please open an issue or submit a pull request.
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
composer install
Tests
Running test suite:
vendor\bin\phpunit tests
Running examples:
php examples\InvoiceExample.php
There is a pre-commit hook to run phpcs and phpcbf. Please make sure they passed before making commits/pushes.