<?php
/*Install Midtrans PHP Library (https://github.com/Midtrans/midtrans-php)
composer require midtrans/midtrans-php
                              
Alternatively, if you are not using **Composer**, you can download midtrans-php library 
(https://github.com/Midtrans/midtrans-php/archive/master.zip), and then require 
the file manually.   

require_once dirname(__FILE__) . '/pathofproject/Midtrans.php'; */

require_once dirname(__FILE__) . '/midtrans-php-master/Midtrans.php';

//SAMPLE REQUEST START HERE

// Set your Merchant Server Key
\Midtrans\Config::$serverKey = 'SB-Mid-server-8jtE0Mp44ve5K9NFDcUi7W0B';
// 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;

//Rebuild item details
$items = json_decode($_POST['items'], true);
$item_details = array();
foreach($items as $each){
    $item = array(
        'id' => $each['id'],
        'price' => $each['price'],
        'quantity' => $each['quantity'],
        'name' => $each['name'].' - '. $each['selectedSize'],
        );
    $item_details[] = $item;
}
//Add transport fee
$item_details[] = array(
    'id' => 'BP',
    'price' => 9000,
    'quantity' => 1,
    'name' => "Biaya Kirim"
);

/*
$params = array(
    'transaction_details' => array('order_id' => rand(),'gross_amount' => $_POST['total']),
    //'item_details' => json_decode($_POST['items'], true),
    'item_details' => $item_details,
    'customer_details' => array(
        'first_name' => $_POST['name'],
        //'last_name' => $_POST['address'],
        'phone' => $_POST['phone'],
        'address' => $_POST['address'],
    ),
);
*/

$transaction_details = array(
    'order_id' => rand(),
    'gross_amount' => $_POST['total'], // no decimal allowed for creditcard
);

// Optional
/*
$item1_details = array(
    'id' => 'a1',
    'price' => 100,
    'quantity' => 1,
    'name' => "Ebook Belajar PHP OOP at qadrLabs"
);

// Optional
$item2_details = array(
    'id' => 'a2',
    'price' => 100,
    'quantity' => 1,
    'name' => "Ebook Belajar Laravel 8 at qadrLabs"
);

// Optional
$item_details = array($item1_details, $item2_details);
*/

// Optional
$billing_address = array(
    'first_name'    => $_POST['name'],
    //'last_name'     => "Muchdas",
    'address'       => $_POST['address'],
    //'city'          => "Jakarta Selatan",
    //'postal_code'   => "12520",
    'phone'         => $_POST['phone'],
    //'country_code'  => 'IDN'
);

// Optional
$shipping_address = array(
    'first_name'    => $_POST['name'],
    //'last_name'     => "Muchdas",
    'address'       => $_POST['address'],
    //'city'          => "Jakarta Selatan",
    //'postal_code'   => "12520",
    'phone'         => $_POST['phone'],
    //'country_code'  => 'IDN'
);

// Optional
$customer_details = array(
    'first_name'    => $_POST['name'],
    //'last_name'     => "Muchdas",
    //'email'         => "simetris_id@yahoo.co.id",
    'phone'         => $_POST['phone'],
    'billing_address'  => $billing_address,
    'shipping_address' => $shipping_address
);

// Optional, remove this to display all available payment methods
//$enable_payments = array('credit_card','cimb_clicks','mandiri_clickpay','echannel');

// Fill transaction details
$params = array(
    //'enabled_payments' => $enable_payments,
    'transaction_details' => $transaction_details,
    'customer_details' => $customer_details,
    'item_details' => $item_details,
);

$snapToken = \Midtrans\Snap::getSnapToken($params);
echo $snapToken;