PHP code example of wildanmzaki / midtrans-ci3

1. Go to this page and download the library: Download wildanmzaki/midtrans-ci3 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wildanmzaki / midtrans-ci3 example snippets


use WildanMZaki\MidtransCI3\CoreAPI;

// This snippet would getting parameter based on method and option that inputted to the 'method' function
$params = CoreAPI::method($method, $option)
    ->invoice('JL-1234566')
    ->total(100000)
    ->va('89619925681')
    ->message('Test Payment')
    ->options([
        'alfamart_free_text_1' => 'jasdfkas'
    ])
    ->params();

$response = CoreAPI::method($method, $option)
    // set up parameters
    ->send();

/**
* 'method': this function is initialized the CoreAPI param setup object
*/
::method($method, $option) // initialization method to create instance of CoreAPI object statically


/**
* config : set up which config file would be used by the library
*/
->config('mymidtransconfigfile')


/**
* invoice method: set up the order id
*/
->invoice($inv)

// Variasi penggunaan: bisa diisikan callback function yang mereturn data dengan type string
->invoice(function() {
    return 'string of order id'
})


/**
* inv method : similiar with invoice, but it will use library utility to generate the invoice in format Prefix-Ymd000x
*/
->inv('INV-', 2)  // this would set up order id to 'INV-20240515002'

->inv('INV-', 2, 5) // this variant would set up the order_id to: 'INV-2024-051500001'  (The third parameter is digits it's mean how long the order id number would be set up)

// other variant: use callback that return int in second parameter
->inv('INV-', function(string $prefix) {
    // $prefix is prefix that already set up for the invoice including the date when the invoice generated.
    // This can help you to count how many invoice that have same prefix (get by like query)

    return 5;
})


/**
* total : this set up the gross_amount of the transaction
*/
->total(100000)

/**
* va : this set up custom va_number and also bill_key for echannel method
* But you need to noted that not all bank use same limitation like min length or max length for the length of this custom va number
* https://docs.midtrans.com/docs/coreapi-core-api-bank-transfer-integration
*/
->va('12345567')


/**
* message: Give payment description to users like in display console alfamart or indomaret
*/
->message('Write your message here')


/**
* options: this set up the sometime 

$response = CoreAPI::method($method, $option)
    ->config('midtrans')
    ->invoice('JL-1234566')
    ->total(100000)
    ->va('89619925681')
    ->message('Test Payment')
    ->items([
        [
            'id' => 'a01',
            'price' => 7000,
            'quantity' => 1,
            'name' => 'Apple'
        ]
    ])
    ->customer([
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => '[email protected]',
        'phone' => '+1234567890',
        'billing_address' => [
            // billing address details
        ],
        'shipping_address' => [
            // shipping address details
        ]
    ])
    ->card_token('card_token_here')
    ->options([
        'alfamart_free_text_1' => 'jasdfkas'
    ])
    ->params();

try {
    $response = CoreAPI::method($method, $option)
        // set up parameters
        ->send();
    // Handle success response
} catch (\Exception $e) {
    // Handle exception
}