Skip to main content

Sample Codes

For the convenience of your integration process, we have prepared multiple sets of sample codes for your reference.

Android App#

For the merchants who integrate with mobile app, we have created a demo application with sample codes for reference. Detailed setup can be found in the Github page.

PHP#

Below is sample code for requesting create hosted checkout: /hosted-checkout/create as a reference.

hosted-checkout-sample.php
<?php
$info = pathinfo($_SERVER['PHP_SELF']);
define('TEST_SERVER', "https://".str_replace("//","/",$_SERVER['HTTP_HOST'].$info['dirname']."/"));
// Change your test merchant ID here
$merchantId = 'xxxxxx';
// Testing environment
$payment_url = 'https://payapi.sit.bbmsl.com';
$url_fail = TEST_SERVER.'test_bbmsl_fail.php';
$url_success = TEST_SERVER.'test_bbmsl_success.php';
$url_cancel = TEST_SERVER.'test_bbmsl_cancel.php';
$url_notify = TEST_SERVER.'test_bbmsl_notify.php';
$payment_code = 'M'.date('YmdHis');
$amount = 10;
$productname = 'Book';
$arr_request = array([
'merchantId' => $merchantId,
'amount' => $amount,
'isRecurring'=> 0,
'callbackUrl' => array(
"fail" => $url_fail,
"success" => $url_success,
"cancel" => $url_cancel,
),
'merchantReference' => $payment_code,
"lineItems" => array(
array(
"quantity" => 1,
'priceData' => array(
"unitAmount" => $amount,
"name" => $productname
)
)
),
]);
$request_content = json_encode($arr_request, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$request_content = trim($request_content, '[]');
// Change your private key here
$privateKey = "xxxxxxxxxxxxxxxxxxx";
$privateKeyPem = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
$request = $request_content;
openssl_sign($request, $encrypted, $privateKeyPem, OPENSSL_ALGO_SHA256);
$signature = base64_encode($encrypted);
$url = $payment_url.'/hosted-checkout/create';
// Create a new cURL resource
$ch = curl_init($url);
echo '===========================';
echo '<br><br>';
echo 'request for signature';
echo '<br><br>';
echo '===========================';
echo '<br><br>';
echo $request;
echo '<br><br>';
echo '===========================';
echo '<br><br>';
echo 'signature';
echo '<br><br>';
echo '===========================';
echo '<br><br>';
echo $signature;
echo '<br><br>';
echo '===========================';
echo '<br><br>';
echo 'Details';
echo '<br><br>';
echo '===========================';
$request = str_replace("\\","",$request);
$arr_payload = array([
"request" => $request,
'signature' => $signature
]);
$payload = json_encode($arr_payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$payload = trim($payload, '[]');
echo '<br><br>';
echo "URL : " . $url ;
echo '<br><br>';
echo "Request : " . $payload;
echo '<br><br>';
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
// Execute the POST request
$result = curl_exec($ch);
// Get the POST request header status
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL resource
curl_close($ch);
// if you need to process the response from the API further
$response = json_decode($result, true);
echo "Response : " . $result;
?>