wildanmzaki / midtrans
这是一个用于使用midtrans核心API并简化操作的扩展包
1.0.0
2024-06-04 07:50 UTC
Requires
- midtrans/midtrans-php: ^2.5
README
这是一个PHP库,用于将Midtrans支付网关集成到Laravel应用程序中。此库简化了设置和通过Midtrans支付网关进行交易的过程。
安装
您可以通过Composer安装此库
composer require wildanmzaki/midtrans
用法
基本设置
首先,导入必要的类
use WildanMZaki\Midtrans\CoreAPI;
接下来:您可以将midtrans.php文件复制到您的应用程序/config目录,并更改此文件中的所有值
但是,我强烈建议您使用.env文件。您可以使用.env.references来设置.env文件中所需的关键字,如果您使用vlucas/php-dot-env包,则可以使用midtrans.env.example文件的内容,并将其全部粘贴到您的应用程序/config文件中的midtrans.php中
示例用法
要开始使用库,初始化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();
发送支付请求
要向Midtrans API发送支付请求,请使用send()
方法
$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 required for some payment method, like alfamart_free_text_1 in cstore : alfamart method */ ->options([ 'alfamart_free_text_1' => 'Text 1 here...', .... ]) /** * items: set up the items detail that ordered by user * please note this items parameter will also count the gross_amount of the transaction by multiplying quantity and price property * in each items detail. You can see detailed description in example usage below */ ->items([ [ id' => 'a01', 'price' => 7000, 'quantity' => 1, 'name' => 'Apple' ], ]) // Note you can customize you price and qty column by passing config parameter like below: ->items([ [ id' => 'a01', 'price' => 7000, 'qty' => 1, 'name' => 'Apple' ], ], [ 'price_column' => 'price', 'qty_column' => 'qty' ]) /** * customer: this set up customer detail for the transaction */ ->customer([ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john@example.com', 'phone' => '+1234567890', 'billing_address' => [ // billing address details ], 'shipping_address' => [ // shipping address details ] ]) # result method /** * params: it would return the parameter that already setted up by the librar */ ->params(); // Note: You can use this params method as setter also. But please remember that this method would replace all params created by library ->params($yourparamhere) /** * send: send the request, it is like calling \Midtrans\CoreAPI::charge($params) in the library automatically */ ->send(); // Note: if you send same order id to the server the send method would catch error from midtrans and returning the status of the order id that is used previously
其他示例
您可以使用提供的方法设置附加选项,例如商品、客户详情和卡令牌
$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' => 'john@example.com', '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 }
许可
此库受MIT许可协议的许可 - 有关详细信息,请参阅LICENSE文件。