Giter Club home page Giter Club logo

xendit-php's Introduction

Xendit API PHP Client

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


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);

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);

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 Payment

\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.

OVO
$ovoParams = [
    'external_id' => 'demo_' . time(),
    'amount' => 32000,
    'phone' => '081298498259',
    'ewallet_type' => 'OVO'
];

$createOvo = \Xendit\EWallets::create($ovoParams);
var_dump($createOvo);
DANA
$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);
LinkAja
$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);

Get Payment Status

\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);

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.

xendit-php's People

Contributors

0505gonzalez avatar afik avatar boxbag avatar edwinyoyada avatar ellennugroho avatar ervanadetya avatar hakiemaul avatar kevindavee avatar liealberttriadrian avatar michaellimair avatar qriouslad avatar stanleynguyen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.