-
-
Save ak4bento/78c2c4be762d78832b51122ee3d69f9c to your computer and use it in GitHub Desktop.
Example Payment With Midtrans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Akill\Payment\Midtrans\Midtrans; | |
include "Midtrans.php"; | |
include "Veritrans.php"; | |
Midtrans::$serverKey = 'SB-Mid-server-key'; | |
$midtrans = new Midtrans; | |
$transaction_details = array( | |
'order_id' => 'TEST-0002', | |
'gross_amount' => 200000 | |
); | |
// Populate items | |
$items = [ | |
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' => "[email protected]", | |
'phone' => "081322311801", | |
'billing_address' => $billing_address, | |
'shipping_address'=> $shipping_address | |
); | |
$credit_card['secure'] = true; | |
//$credit_card['save_card'] = true; | |
$time = time(); | |
$custom_expiry = array( | |
'start_time' => date("Y-m-d H:i:s O",$time), | |
'unit' => 'hour', | |
'duration' => 2 | |
); | |
$transaction_data = array( | |
'transaction_details'=> $transaction_details, | |
// 'item_details' => $items, | |
// 'customer_details' => $customer_details, | |
'credit_card' => $credit_card, | |
// 'expiry' => $custom_expiry | |
); | |
$snap_token = $midtrans->getSnapToken($transaction_data); | |
$token = $snap_token; | |
?> | |
<html> | |
<title>Checkout</title> | |
<head> | |
<script type="text/javascript" | |
src="https://app.sandbox.midtrans.com/snap/snap.js" | |
data-client-key="SB-Mid-client-key"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
</head> | |
<body> | |
<button id="pay-button">Pay!</button> | |
<script type="text/javascript"> | |
window.onload = function(event) { | |
snap.pay('<?php echo $token; ?>'); // store your snap token here | |
// your code here | |
}; | |
var payButton = document.getElementById('pay-button'); | |
payButton.addEventListener('click', function () { | |
snap.pay('<?php echo $token; ?>'); // store your snap token here | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment