中期php
中期trans❤️php!
这是与作曲家兼容的Mid Trans付款API的官方PHP包装器/图书馆。有关该产品的更多信息,请访问https://*midtra*ns.c*om,并在http://docs.midtrans.com上查看文档,以获取更多技术详细信息。启动版2.6,该库现在支持Snap-bi。您可以转到此文档以了解有关Snap-Bi的更多信息。
1。安装
1.作曲家安装
如果您使用的是作曲家,则可以通过Composer CLI安装:
composer require midtrans/midtrans-php
或者
将此要求行添加到您的composer.json文件:
{
\"require\" : {
\"midtrans/midtrans-php\" : \" 2.* \"
}
}
并在终端上运行composer install 。
注意:如果您使用的是Laravel框架,在某些情况下,您还需要运行
composer dumpautoload
然后,
/Midtrans将作为您的Laravel项目中的对象可用(自动加载)。
1.B手动安装
如果您不使用作曲家,则可以克隆或下载此存储库。
然后,您应该在代码上需要/autoLoad Midtrans.php文件。
require_once dirname ( __FILE__ ) . \' /pathofproject/Midtrans.php \' ; // my code goes here
2。如何使用
2.1一般设置
// Set your Merchant Server Key \\ Midtrans \\Config:: $ serverKey = \' <your server key> \' ; // Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction). \\ Midtrans \\Config:: $ isProduction = false ; // Set sanitization on (default) \\ Midtrans \\Config:: $ isSanitized = true ; // Set 3DS transaction for credit card to true \\ Midtrans \\Config:: $ is3ds = true ;
覆盖通知URL
您可以选择更改或添加每个事务上的自定义通知URL。可以通过将其他HTTP标头添加到负责请求中来实现。
// Add new notification url(s) alongside the settings on Midtrans Dashboard Portal (MAP) Config:: $ appendNotifUrl = \" https://ex**am*ple.com/test1,https://example.com/test2 \" ; // Use new notification url(s) disregarding the settings on Midtrans Dashboard Portal (MAP) Config:: $ overrideNotifUrl = \" https://ex*am*ple.c*om/test1 \" ;
更多细节
注意:当一起使用
appendNotifUrl和overrideNotifUrl时,仅将使用过度overrideNotifUrl。
两个标头只能收到最多3个URL 。
智力 – 键
您可以选择在充电事务上添加divempotency键。可以通过将其他HTTP标头添加到负责请求中来实现。是一个独特的值,它在API请求上放在标题上。 Midtrans API接受标头上的IDEMPOTENCY-KEY,可以安全处理重试请求,而无需两次执行相同的操作。对于由于网络问题或其他意外错误而未收到响应的情况,这很有帮助。
Config:: $ paymentIdempotencyKey = \" Unique-ID \" ;
更多细节
2.2选择产品/方法
我们有3种不同的付款产品,您可以使用:
- SNAP-可自定义的付款弹出窗口将出现在您的Web/App上(无重定向)。 Doc Ref
- SNAP重定向 – 需要将客户重定向到由中型托管托管的付款URL。 Doc Ref
- Core API(VT -Direct) – 基本的后端实现,您可以根据自己的意愿自定义嵌入在Web/应用程序上的前端(无重定向)。 Doc Ref
选择最适合自己独特需求的一种。
2.2
您可以在此处看到快照示例。
获得快照令牌
$ params = array ( \' transaction_details \' => array ( \' order_id \' => rand (), \' gross_amount \' => 10000 , ) ); $ snapToken = \\ Midtrans \\Snap:: getSnapToken ( $ params );
当客户点击付款按钮时,初始化snap js
<script src="https://app.s*and*box.midtr*ans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
<script type="text/javascript">
document.getElementById(\’pay-button\’).onclick = function(){
// SnapToken acquired from previous step
snap.pay(\'<?=$snapToken?>\’, {
// Optional
onSuccess: function(result){
/* You may add your own js here, this is just example */ document.getElementById(\’result-json\’).innerHTML += JSON.stringify(result, null, 2);
},
// Optional
onPending: function(result){
/* You may add your own js here, this is just example */ document.getElementById(\’result-json\’).innerHTML += JSON.stringify(result, null, 2);
},
// Optional
onError: function(result){
/* You may add your own js here, this is just example */ document.getElementById(\’result-json\’).innerHTML += JSON.stringify(result, null, 2);
}
});
};
</script>
</body>
</html>\”>
< html > < body > < button id =\" pay-button \" > Pay! </ button > < pre > < div id =\" result-json \" > JSON result will appear here after payment: < br > </ div > </ pre > <!-- TODO: Remove \".sandbox\" from script src URL for production environment. Also input your client key in \"data-client-key\" --> < script src =\" https://app.sand*box.m*idtr*ans.com/snap/snap.js \" data-client-key =\" <Set your ClientKey here> \" > </ script > < script type =\" text/javascript \" > document . getElementById ( \'pay-button\' ) . onclick = function ( ) { // SnapToken acquired from previous step snap . pay ( \'<?=$snapToken?>\' , { // Optional onSuccess : function ( result ) { /* You may add your own js here, this is just example */ document . getElementById ( \'result-json\' ) . innerHTML += JSON . stringify ( result , null , 2 ) ; } , // Optional onPending : function ( result ) { /* You may add your own js here, this is just example */ document . getElementById ( \'result-json\' ) . innerHTML += JSON . stringify ( result , null , 2 ) ; } , // Optional onError : function ( result ) { /* You may add your own js here, this is just example */ document . getElementById ( \'result-json\' ) . innerHTML += JSON . stringify ( result , null , 2 ) ; } } ) ; } ; </ script > </ body > </ html >
实施通知处理程序
请参阅本节
2.2.b快速重定向
您可以在此处看到一些快照重定向示例。
获取付款页面的重定向URL
$ params = array ( \' transaction_details \' => array ( \' order_id \' => rand (), \' gross_amount \' => 10000 , ) ); try { // Get Snap Payment Page URL $ paymentUrl = \\ Midtrans \\Snap:: createTransaction ( $ params )-> redirect_url ; // Redirect to Snap Payment Page header ( \' Location: \' . $ paymentUrl ); } catch ( Exception $ e ) { echo $ e -> getMessage (); }
实施通知处理程序
请参阅本节
2.2.c核心API(VT-Direct)
您可以在此处看到一些核心API示例。
设置客户端密钥
MidtransNew3ds . clientKey = \"<your client key>\" ;
结帐页面
请参考此文件
结帐过程
1。创建交易详细信息
$ transaction_details = array ( \' order_id \' => time (), \' gross_amount \' => 200000 );
2。创建项目详细信息,计费地址,运输地址和客户详细信息(可选)
// Populate items $ items = array ( array ( \' id \' => \' item1 \' , \' price \' => 100000 , \' quantity \' => 1 , \' name \' => \' Adidas f50 \' ), array ( \' id \' => \' item2 \' , \' price \' => 50000 , \' quantity \' => 2 , \' name \' => \' Nike N90 \' ) ); // Populate customer\'s billing address $ billing_address = array ( \' first_name \' => \" Andri \" , \' last_name \' => \" Setiawan \" , \' address \' => \" Karet Belakang 15A, Setiabudi. \" , \' city \' => \" Jakarta \" , \' postal_code \' => \" 51161 \" , \' phone \' => \" 081322311801 \" , \' country_code \' => \' IDN \' ); // Populate customer\'s shipping address $ shipping_address = array ( \' first_name \' => \" John \" , \' last_name \' => \" Watson \" , \' address \' => \" Bakerstreet 221B. \" , \' city \' => \" Jakarta \" , \' postal_code \' => \" 51162 \" , \' phone \' => \" 081322311801 \" , \' country_code \' => \' IDN \' ); // Populate customer\'s info $ customer_details = array ( \' first_name \' => \" Andri \" , \' last_name \' => \" Setiawan \" , \' email \' => \" test@test.com \" , \' phone \' => \" 081322311801 \" , \' billing_address \' => $ billing_address , \' shipping_address \' => $ shipping_address );
3。从结帐页面获取令牌ID
// Token ID from checkout page $ token_id = $ _POST [ \' token_id \' ];
4。创建交易数据
// Transaction data to be sent $ transaction_data = array ( \' payment_type \' => \' credit_card \' , \' credit_card \' => array ( \' token_id \' => $ token_id , \' authentication \' => true , // \'bank\' => \'bni\', // optional to set acquiring bank // \'save_token_id\' => true // optional for one/two clicks feature ), \' transaction_details \' => $ transaction_details , \' item_details \' => $ items , \' customer_details \' => $ customer_details );
5。充电
$ response = \\ Midtrans \\CoreApi:: charge ( $ transaction_data );
6。信用卡3DS身份验证
信用卡收费结果可能包含3DS身份验证的redirect_url 。 3DS身份验证应在前端处理,请参阅API文档
有关信用卡3DS交易的完整示例,请参阅:
- 核心API示例
7。处理交易状态
// Success if ( $ response -> transaction_status == \' capture \' ) { echo \" <p>Transaksi berhasil.</p> \" ; echo \" <p>Status transaksi untuk order id $ response -> order_id : \" . \" $ response -> transaction_status </p> \" ; echo \" <h3>Detail transaksi:</h3> \" ; echo \" <pre> \" ; var_dump ( $ response ); echo \" </pre> \" ; } // Deny else if ( $ response -> transaction_status == \' deny \' ) { echo \" <p>Transaksi ditolak.</p> \" ; echo \" <p>Status transaksi untuk order id . $ response -> order_id : \" . \" $ response -> transaction_status </p> \" ; echo \" <h3>Detail transaksi:</h3> \" ; echo \" <pre> \" ; var_dump ( $ response ); echo \" </pre> \" ; } // Challenge else if ( $ response -> transaction_status == \' challenge \' ) { echo \" <p>Transaksi challenge.</p> \" ; echo \" <p>Status transaksi untuk order id $ response -> order_id : \" . \" $ response -> transaction_status </p> \" ; echo \" <h3>Detail transaksi:</h3> \" ; echo \" <pre> \" ; var_dump ( $ response ); echo \" </pre> \" ; } // Error else { echo \" <p>Terjadi kesalahan pada data transaksi yang dikirim.</p> \" ; echo \" <p>Status message: [ $ response -> status_code ] \" . \" $ response -> status_message </p> \" ; echo \" <pre> \" ; var_dump ( $ response ); echo \" </pre> \" ; }
8。实施通知处理程序
请参阅本节
2.3处理HTTP通知
创建分离的Web端点(通知URL)以接收HTTP Post Notification Callback/Webhook。每当更改事务状态时,HTTP通知将发送。示例也可以在这里提供
$ notif = new \\ Midtrans \\ Notification (); $ transaction = $ notif -> transaction_status ; $ fraud = $ notif -> fraud_status ; error_log ( \" Order ID $ notif -> order_id : \" . \" transaction status = $ transaction , fraud staus = $ fraud \" ); if ( $ transaction == \' capture \' ) { if ( $ fraud == \' challenge \' ) { // TODO Set payment status in merchant\'s database to \'challenge\' } else if ( $ fraud == \' accept \' ) { // TODO Set payment status in merchant\'s database to \'success\' } } else if ( $ transaction == \' cancel \' ) { if ( $ fraud == \' challenge \' ) { // TODO Set payment status in merchant\'s database to \'failure\' } else if ( $ fraud == \' accept \' ) { // TODO Set payment status in merchant\'s database to \'failure\' } } else if ( $ transaction == \' deny \' ) { // TODO Set payment status in merchant\'s database to \'failure\' }
2.4过程交易
获取交易状态
$ status = \\ Midtrans \\Transaction:: status ( $ orderId ); var_dump ( $ status );
批准交易
如果交易fraud_status ==挑战,则可以批准商家仪表板或API的交易:
$ approve = \\ Midtrans \\Transaction:: approve ( $ orderId ); var_dump ( $ approve );
取消交易
您可以通过fraud_status == CHALLENGE取消交易,或使用transaction_status == CAPTURE信用卡事务(在结算之前)
$ cancel = \\ Midtrans \\Transaction:: cancel ( $ orderId ); var_dump ( $ cancel );
到期交易
您可以使用transaction_status == PENDING处理(在结算或到期之前)到期
$ cancel = \\ Midtrans \\Transaction:: cancel ( $ orderId ); var_dump ( $ cancel );
退款交易
退款交易(并非所有付款渠道都允许通过API退款)您可以通过transaction_status == settlement退还交易
$ params = array ( \' refund_key \' => \' order1-ref1 \' , \' amount \' => 10000 , \' reason \' => \' Item out of stock \' ); $ refund = \\ Midtrans \\Transaction:: refund ( $ orderId , $ params ); var_dump ( $ refund );
直接退款交易
退款通过直接退款API退款您可以通过transaction_status == settlement退款交易
$ params = array ( \' refund_key \' => \' order1-ref1 \' , \' amount \' => 10000 , \' reason \' => \' Item out of stock \' ); $ direct_refund = \\ Midtrans \\Transaction:: refundDirect ( $ orderId , $ params ); var_dump ( $ direct_refund );
3。snap-bi(*新功能启动v2.6.0)
STARAR NASIANT OPEN API PEMBAYARAN,即简短的SNAP,是印度尼西亚银行出版的全国付款API标准。要了解更多信息,您可以阅读此文档
3.1一般设置
//These config value are based on the header stated here https://docs.*mid**trans.com/reference/getting-started-1 // Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction). \\ SnapBi \\Config:: $ isProduction = false ; // Set your client id. Merchant’s client ID that will be given by Midtrans, will be used as X-CLIENT-KEY on request’s header in B2B Access Token API. \\ SnapBi \\Config:: $ snapBiClientId = \" YOUR CLIENT ID \" ; // Set your private key here, make sure to add \\n on the private key, you can refer to the examples \\ SnapBi \\Config:: $ snapBiPrivateKey = \" YOUR PRIVATE KEY \" ; // Set your client secret. Merchant’s secret key that will be given by Midtrans, will be used for symmetric signature generation for Transactional API’s header. \\ SnapBi \\Config:: $ snapBiClientSecret = \" YOUR CLIENT SECRET \" ; // Set your partner id. Merchant’s partner ID that will be given by Midtrans, will be used as X-PARTNER-ID on Transactional API’s header. \\ SnapBi \\Config:: $ snapBiPartnerId = \" YOUR PARTNER ID \" ; // Set the channel id here. \\ SnapBi \\Config:: $ snapBiChannelId = \" CHANNEL ID \" ; // Enable logging to see details of the request/response make sure to disable this on production, the default is disabled. \\ SnapBi \\Config:: $ enableLogging = false ; // Set your public key here if you want to verify your webhook notification, make sure to add \\n on the public key, you can refer to the examples \\ SnapBi \\Config:: $ snapBiPublicKey = \" YOUR PUBLIC KEY \"
3.2创建付款
3.2.1直接借方(Gopay,Dana,Shopeepay)
有关使用直接借方创建付款的更多详细信息,请参阅此文档。
date_default_timezone_set ( \' Asia/Jakarta \' );
$ time_stamp = date ( \" c \" );
$ date = new DateTime ( $ time_stamp );
$ external_id = \" uzi-order-testing \" . uniqid ();
// Add 10 minutes validity time
$ date -> modify ( \' +10 minutes \' );
// Format the new date
$ valid_until = $ date -> format ( \' c \' );
$ merchant_id = \" M001234 \" ;//create direct debit request body/ payload
//you can change the payment method on the `payOptionDetails`
$ debitParams = array (
\" partnerReferenceNo \" => $ external_id ,
\" chargeToken \" => \"\" ,
\" merchantId \" => $ merchant_id ,
\" urlParam \" => array (
array (
\" url \" => \" https://www.go**ogl*e.com \" ,
\" type \" => \" PAY_RETURN \" ,
\" isDeeplink \" => \" Y \"
)
),
\" validUpTo \" => $ valid_until ,
\" payOptionDetails \" => array (
array (
\" payMethod \" => \" DANA \" ,
\" payOption \" => \" DANA \" ,
\" transAmount \" => array (
\" value \" => \" 100.0 \" ,
\" currency \" => \" IDR \" //currently we only support `IDR`
)
)
),
\" additionalInfo \" => array (
\" customerDetails \" => array (
\" phone \" => \" 081122334455 \" ,
\" firstName \" => \" Andri \" ,
\" lastName \" => \" Litani \" ,
\" email \" => \" andri@litani.com \" ,
\" billingAddress \" => array (
\" firstName \" => \" Andri \" ,
\" lastName \" => \" Litani \" ,
\" phone \" => \" 081122334455 \" ,
\" address \" => \" billingAddress \" ,
\" city \" => \" billingCity \" ,
\" postalCode \" => \" 12790 \" ,
\" countryCode \" => \" CZH \"
),
\" shippingAddress \" => array (
\" firstName \" => \" Andri \" ,
\" lastName \" => \" Litani \" ,
\" phone \" => \" 081122334455 \" ,
\" address \" => \" shippingAddress \" ,
\" city \" => \" shippingCity \" ,
\" postalCode \" => \" 12790 \" ,
\" countryCode \" => \" CZH \"
)
),
\" items \" => array (
array (
\" id \" => \"
