<?php if($_GET['p']==""){
    // SAMPLE HIT API iPaymu v2 PHP //
// Setting Waktu Server
date_default_timezone_set('Asia/Jakarta');
date_default_timezone_get();
	
    $va           = '1179002216172317'; //get on iPaymu dashboard
   // Sandbox
    $secret    = '1422F4CA-D3F7-4D51-9A3F-A7A4AE18A211'; //get on iPaymu dashboard
    // Production
	//$secret    = 'A52DC406-C589-412E-89C7-931A358DC8EB'; //get on iPaymu dashboard

    $url          = 'https://sandbox.ipaymu.com/api/v2/payment'; //url
    //$url          = 'https://my.ipaymu.com/api/v2/payment'; //url
    $method       = 'POST'; //method
    
    $raw = file_get_contents("php://input");
    $data = json_decode($raw, true);

    //Request Body//
	$body['name'] = "GENERASI MEDIA"; 
	$body['email']  = "cs@generasimedia.com";
	$body['phone']  = "085223189523";
	$body['referenceId']  = $data['reference'];
    $body['product']    = array($data['item'].' ('.$data['reference'].')');
    $body['qty']        = array('1');
    $body['price']      = array($data['amount']);
	//$body['weight']  = array('1', '1');
	//$body['dimension']  = array("0:0:0","0:0:0");
	//$body['pickupArea']  = "46196";
	//$body['pickupAddress']  = "Awipari, Cibeureum, Tasikmalaya";
	$body['buyerName'] = "Teguh Aljauza"; 
	$body['buyerEmail']  = "teguhaljz@gmail.com";
	$body['buyerPhone']  = "082216172317";
	//$body['paymentMethod']  = "cstore"; 
	// cstore, bniva, bagva, mandiriva, niagava, bcatransfer, cc, cod, cstore, akulaku, qris
	//$body['paymentChannel']  = "alfamart"; // alfamart, indomaret
    $body['comments']  = 'Ini Percobaan Transaksi Pertama';
	$body['expired']  = '24';
	$body['expiredType']  = 'hours';
	$body['returnUrl']  = 'http://localhost/ipaymu/?p=thankyou';
    $body['cancelUrl']  = 'http://localhost/ipaymu/?p=cancel';
    $body['notifyUrl']  = 'http://localhost/ipaymu/?p=notify';
	
 
	
    //End Request Body//

    //Generate Signature
    // *Don't change this
    $jsonBody     = json_encode($body, JSON_UNESCAPED_SLASHES);
    $requestBody  = strtolower(hash('sha256', $jsonBody));
    $stringToSign = strtoupper($method) . ':' . $va . ':' . $requestBody . ':' . $secret;
    $signature    = hash_hmac('sha256', $stringToSign, $secret);
    $timestamp    = Date('YmdHis');
    //End Generate Signature


    $ch = curl_init($url);

    $headers = array(
        'Accept: application/json',
        'Content-Type: application/json',
        'va: ' . $va,
        'signature: ' . $signature,
        'timestamp: ' . $timestamp
    );

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_POST, count($body));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $err = curl_error($ch);
    $ret = curl_exec($ch);
    curl_close($ch);
    if($err) {
        echo $err;
    } else {
 $ret = json_decode($ret);
/*
        //Response
       
        if($ret->Status == 200) {
            $sessionId  = $ret->Data->SessionID;
            $url        =  $ret->Data->Url;
            header('Location:' . $url);
        } else {
			$ret= json_decode( json_encode($ret), true);
            print_r($ret);
        }
        //End Response
*/

if ($ret->Status == 200) {
    echo json_encode([
        'status'     => 200,
        'message'    => 'Success',
        'sessionId'  => $ret->Data->SessionID,
        'url'        => $ret->Data->Url
    ]);
} else {
    echo json_encode([
        'status'  => $ret->Status,
        'message' => $ret->Message ?? 'Gagal membuat transaksi! Coba lagi..'
    ]);
}

        
    }
}
?>