This library is the abstraction of Xendit API for access from applications written with PHP.
- Documentation
- Installation
- Usage
- Methods' Signature and Examples
- Exceptions
- Contributing
For the API documentation, check Xendit API Reference.
Install xendit-php-clients with composer by following command:
composer require xendit/xendit-phpor add it manually in your composer.json file.
To update xendit-php-clients with composer, use the following command:
composer update xendit/xendit-phpTo migrate, see MIGRATE.md for more information.
Configure package with your account's secret key obtained from Xendit Dashboard.
Xendit::setApiKey('secretKey');See example codes for more details.
A custom HTTP Client that implements HttpClientInterface can be injected like so
Xendit::setHttpClient($client);Checkout custom http client example for implementation reference.
\Xendit\Balance::getBalance(string $account_type);Usage example:
$getBalance = \Xendit\Balance::getBalance('CASH');
var_dump($getBalance);\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);\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);\Xendit\Cards::capture(string $id, array $params);Usage example:
$id = 'charge-id';
$params = ['amount' => 100000];
$captureCharge = \Xendit\Cards::capture($id, $params);
var_dump($captureCharge);\Xendit\Cards::retrieve(string $id);Usage example:
$id = 'charge-id';
$getCharge = \Xendit\Cards::retrieve($id);
var_dump($getCharge);\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);\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' => 'customer@yourwebsite.com',
'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);\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);\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);\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' => ['test+to@xendit.co'],
'email_cc' => ['test+cc@xendit.co'],
'email_bcc' => ['test+bcc1@xendit.co', 'test+bcc2@xendit.co']
]
]
];
$createBatchDisbursements = \Xendit\Disbursements::createBatch($batch_params);
var_dump($createBatchDisbursements);\Xendit\Disbursements::retrieve(string $id);Usage example:
$id = 'disbursements-id';
$getDisbursements = \Xendit\Disbursements::retrieve($id);
var_dump($getDisbursements);\Xendit\Disbursements::retrieveExternal(string $external_id);Usage example:
$external_id = 'disbursements-ext-id';
$getDisbursementsByExt = \Xendit\Disbursements::retrieveExternal($external_id);
var_dump($getDisbursementsByExt);\Xendit\Disbursements::getAvailableBanks();Usage example:
$getDisbursementsBanks = \Xendit\Disbursements::getAvailableBanks();
var_dump($getDisbursementsBanks);\Xendit\EWallets::create(array $params);To create payment, each e-wallet has its own required params. For more information, please check Xendit API Reference - E-Wallets.
$ovoParams = [
'external_id' => 'demo_' . time(),
'amount' => 32000,
'phone' => '081298498259',
'ewallet_type' => 'OVO'
];
$createOvo = \Xendit\EWallets::create($ovoParams);
var_dump($createOvo);$danaParams = [
'external_id' => 'demo_' . time(),
'amount' => 32000,
'phone' => '081298498259',
'expiration_date' => '2020-02-20T00:00:00.000Z',
'callback_url' => 'https://my-shop.com/callbacks',
'redirect_url' => 'https://my-shop.com/home',
'ewallet_type' => 'DANA'
];
$createDana = \Xendit\EWallets::create($danaParams);
var_dump($createDana);$linkajaParams = [
'external_id' => 'demo_' . time(),
'amount' => 32000,
'phone' => '081298498259',
'items' => [
[
'id' => '123123',
'name' => 'Phone Case',
'price' => 100000,
'quantity' => 1
],
[
'id' => '345678',
'name' => 'Powerbank',
'price' => 200000,
'quantity' => 1
]
],
'callback_url' => 'https =>//yourwebsite.com/callback',
'redirect_url' => 'https =>//yourwebsite.com/order/123',
'ewallet_type' => 'LINKAJA'
];
$createLinkaja = \Xendit\EWallets::create($linkajaParams);
var_dump($createLinkaja);\Xendit\EWallets::getPaymentStatus(string $external_id, string $ewallet_type);Usage example:
$external_id = 'external-ID';
$ewallet_type = 'OVO';
$getPayments = \Xendit\EWallets::getPaymentStatus($external_id, $ewallet_type);\Xendit\Invoice::create(array $params);Usage example:
$params = ['external_id' => 'demo_147580196270',
'payer_email' => 'sample_email@xendit.co',
'description' => 'Trip to Bali',
'amount' => 32000
];
$createInvoice = \Xendit\Invoice::create($params);
var_dump($createInvoice);\Xendit\Invoice::retrieve(string $id);Usage example:
$id = 'invoice-id';
$getInvoice = \Xendit\Invoice::retrieve($id);
var_dump($getInvoice);\Xendit\Invoice::retrieveAll();Usage example:
$getAllInvoice = \Xendit\Invoice::retrieveAll();
var_dump(($getAllInvoice));\Xendit\Invoice::expireInvoice(string $id);Usage example:
$id = 'invoice-id';
$expireInvoice = \Xendit\Invoice::expireInvoice($id);
var_dump($expireInvoice);\Xendit\Payouts::create(array $params);Usage example:
$params = [
'external_id' => 'payouts-123456789',
'amount' => 50000
];
$createPayout = \Xendit\Payouts::create($params);
var_dump($createPayout);\Xendit\Payouts::retrieve(string $id);Usage example:
$id = 'payout-id';
$getPayout = \Xendit\Payouts::retrieve($id);
var_dump($getPayout);\Xendit\Payouts::void(string $id);Usage example:
$id = 'payout-id';
$voidPayout = \Xendit\Payouts::void($id);
var_dump($voidPayout);\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)\Xendit\QRCode::get(string $external_id);Usage example:
$qr_code = \Xendit\QRCode::get('external_123');
var_dump($qr_code);\Xendit\Recurring::create(array $params);Usage example:
$params = [
'external_id' => 'demo_147580196270',
'payer_email' => 'sample_email@xendit.co',
'description' => 'Trip to Bali',
'amount' => 32000,
'interval' => 'MONTH',
'interval_count' => 1
];
$createRecurring = \Xendit\Recurring::create($params);
var_dump($createRecurring);\Xendit\Recurring::retrieve(string $id);Usage example:
$id = 'recurring-payment-id';
$getRecurring = \Xendit\Recurring::retrieve($id);
var_dump($getRecurring);\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);\Xendit\Recurring::stop(string $id);Usage example:
$id = 'recurring-payment-id';
$stopRecurring = \Xendit\Recurring::stop($id);
var_dump($stopRecurring);\Xendit\Recurring::pause(string $id);Usage example:
$id = 'recurring-payment-id';
$pauseRecurring = \Xendit\Recurring::pause($id);
var_dump($pauseRecurring);\Xendit\Recurring::resume(string $id);Usage example:
$id = 'recurring-payment-id';
$resumeRecurring = \Xendit\Recurring::resume($id);
var_dump($resumeRecurring);\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);\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);\Xendit\Retail::retrieve(string $id);Usage example:
$id = 'FPC-id';
$getFPC = \Xendit\Retail::retrieve($id);
var_dump($getFPC);\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);\Xendit\VirtualAccounts::getVABanks();Usage example:
$getVABanks = \Xendit\VirtualAccounts::getVABanks();
var_dump($getVABanks);\Xendit\VirtualAccounts::retrieve(string $id);Usage example:
$id = 'VA-id';
$getVA = \Xendit\VirtualAccounts::retrieve($id);
var_dump($getVA);\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);\Xendit\VirtualAccounts::getFVAPayment(string $paymentID);Usage example:
$paymentID = 'payment-ID';
$getFVAPayment = \Xendit\VirtualAccounts::getFVAPayment($paymentID);
var_dump($getFVAPayment);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 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());
}For any requests, bugs, or comments, please open an issue or submit a pull request.
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 installvendor\bin\phpunit testsphp examples\InvoiceExample.phpThere is a pre-commit hook to run phpcs and phpcbf. Please make sure they passed before making commits/pushes.