Skip to content

Instantly share code, notes, and snippets.

@tamathecxder
Last active January 21, 2023 06:13
Show Gist options
  • Save tamathecxder/8b185132be588e98358416022c3c2548 to your computer and use it in GitHub Desktop.
Save tamathecxder/8b185132be588e98358416022c3c2548 to your computer and use it in GitHub Desktop.
method checkout
public function checkout_v2(Request $request)
{
$this->validate($request, [
'product_categories_id' => 'required|exists:ppob_categories,id',
'product_sub_categories_id' => 'required|exists:ppob_sub_categories,id',
'product_code' => 'required',
'hp' => 'required',
]);
$check10MenitTransaksiPerHP = $this->check10MenitTransaksiPerHP($request->hp);
if ($check10MenitTransaksiPerHP) {
return response()->json([
'status' => 'Failed',
'message' => 'Silahkan tunggu 2Menit',
], 400);
}
$ppob_category = PpobCategory::findOrFail($request->product_categories_id);
$ppob_sub_category = PpobSubCategory::findOrFail($request->product_sub_categories_id);
// $produk = $this->price_list_by_product_code($ppob_category->type, $ppob_sub_category->operator,$request->product_code);
$produk = PpobProduct::where('ppob_sub_category_id', $ppob_sub_category->id)->where('code', $request->product_code)->first();
if ($produk == null) {
return response()->json([
'status' => 'Failed',
'message' => 'Produk Tidak Ada',
], 400);
}
$product_code = $produk['code'];
// $price = round($produk['price'] + ($produk['price'] * $produk['fee'] / 100));
$price = round($produk['price'] + 1100);
$product_nominal = $produk['nominal'];
$product_name = $produk['type'] . ' ' . $produk['name'] . ' ' . $produk['nominal'];
if ($price <= 0) {
return response()->json([
'status' => 'Failed',
'message' => 'Nominal produk tidak boleh kurang dari sama dengan 0',
], 400);
};
$cekBalance = MemberBalanceHelper::cekCredit($price);
if ($cekBalance == false) {
return response()->json([
'status' => 'Failed',
'message' => 'Saldo Kurang',
], 400);
}
//ref id
$ref_id = Random::ref_id('ppob_orders', 'ref_id');
//Menambahkan PpobOrder Terlebih Dahulu
$order = PpobOrder::create([
'id' => Uuid::uuid4(),
'ref_id' => $ref_id,
'tr_id' => null,
'product_code' => $product_code,
'ppob_categories_id' => $request->product_categories_id,
'ppob_product_nominal' => $product_nominal,
'ppob_product_name' => $product_name,
'member_id' => $request->user()->id,
'type_of_transction' => 'PRA',
'hp' => $request->hp,
'selling_price' => $price,
'purchase_price' => 0,
'commission' => 0,
'status' => 'Process',
]);
if ($request->no_meter_pln) {
$order->update([
'no_meter_pln' => $request->no_meter_pln
]);
}
$history = MemberBalanceHelper::transaction($price, $order);
//Menambah Notif Terlebih Dahulu
$notif = Notification::create([
'recipient_id' => $request->user()->id,
'actor_id' => $request->user()->id,
'readt_at' => null,
'notifiable_id' => $order->id,
'notifiable_type' => 'Transaksi',
'title' => 'Notifikasi Transaksi',
'body' => 'Transaksi ' . $order->ppob_product_name . ' sedang di proses'
]);
$path = 'transaksi/pembelian';
if ($ppob_category->type == 'PLN') {
$params = [
'inquiry' => 'PLN',
'code' => $product_code,
'phone' => $request->hp,
'no_meter_pln' => $request->no_meter_pln ?? '',
'api_trxid' => $ref_id,
'pin' => '1337'
];
} else {
$params = [
'inquiry' => 'I',
'code' => $product_code,
'phone' => $request->hp,
'api_trxid' => $ref_id,
'pin' => '1337'
];
}
$data = TripayPpob::uri($path, $params, 'POST');
if (!$data) {
// return response()->json([
// 'message' => 'Server tripay error',
// ], 500);
$status = 'Failed';
$data = [
'success' => false,
'message' => 'Server tripay error'
];
}
if (!$data['success']) {
$status = 'Failed';
$data = [
'success' => false,
'message' => $data['message']
];
} else {
$status = $this->getStatus($data['success']);
}
DB::beginTransaction();
if ($status != 'Failed') {
$sc = 'success';
//Update Oprder
$order->update([
'ref_id' => $data['api_trxid'],
'tr_id' => $data['trxid'],
'purchase_price' => $produk->price,
'commission' => $price - $produk->price,
'status' => 'Process',
]);
//Update Notif
$notif->update([
'body' => 'Transaksi ' . $order->category['name'] . ' .' . $order->ppob_product_nominal . ' sedang di proses'
]);
$data['created_at'] = Carbon::parse($order['created_at'])->format('Y-m-d H:i:s');
DB::commit();
return response()->json([
'status' => $sc,
'data' => $data,
], 200);
} else {
//Update Oprder
$order->update([
'status' => $status,
'ket' => $data['message'] ?? 'undefined'
]);
//Update Notif
$notif->update([
'body' => 'Transaksi ' . $order->category['name'] . ' .' . $order->ppob_product_nominal . ' Gagal'
]);
$history = MemberBalanceHelper::refund_transaction($price, $order);
$sc = 'Failed';
DB::commit();
return response()->json([
'status' => $sc,
'data' => $data,
], 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment