Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Helpers;
- use App\Models\DokuPayment;
- use App\Models\mPenjualan;
- use App\Models\mPerson;
- use Carbon\Carbon;
- use Exception;
- use Illuminate\Support\Facades\DB;
- use Ramsey\Uuid\Uuid;
- class DokuCheckoutPayment
- {
- public static function calculateExpiredMinutes($datetime_due)
- {
- if (null == $datetime_due) {
- throw new Exception('ERR_INVALID_DATETIME_DUE_1', 1);
- }
- if (!is_string($datetime_due)) {
- throw new Exception('ERR_INVALID_DATETIME_DUE_2', 1);
- }
- // valid format: 0000-00-00
- if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datetime_due)) {
- throw new Exception('ERR_INVALID_DATETIME_DUE_3', 1);
- }
- $int_now = now()->timestamp;
- $int_due = Carbon::parse($datetime_due . ' ' . '24:59:59')->timestamp;
- if ($int_now > $int_due) {
- return 0;
- }
- $seconds = $int_due - $int_now;
- $minutes = $seconds / 60;
- return $minutes;
- }
- public static function createTempCheckoutByTagihan($daftar_tagihan_id, $user_id, $payment_due_date, $tipe_pembayaran)
- {
- $pelanggan = mPerson::find($user_id);
- $penjualan = mPenjualan::where('pjl_kode', $daftar_tagihan_id)->where('pjl_status', 'belum lunas')->first();
- $final_total_tagihan = $penjualan->grand_total;
- $final_total_bayar = $penjualan->sisa_tagihan;
- if ($tipe_pembayaran == "virtual_account") {
- // GENERATE PAYMENT REQUEST DOKU FOR VIRTUAL ACCOUNT
- $payment_method_types = [
- "VIRTUAL_ACCOUNT_BCA",
- "VIRTUAL_ACCOUNT_BANK_MANDIRI",
- "VIRTUAL_ACCOUNT_BANK_SYARIAH_MANDIRI",
- "VIRTUAL_ACCOUNT_DOKU",
- "VIRTUAL_ACCOUNT_BRI",
- "VIRTUAL_ACCOUNT_BNI",
- "VIRTUAL_ACCOUNT_BANK_PERMATA",
- "VIRTUAL_ACCOUNT_BANK_CIMB",
- "VIRTUAL_ACCOUNT_BANK_DANAMON",
- ];
- } else if ($tipe_pembayaran == "credit_card") {
- // GENERATE PAYMENT REQUEST DOKU FOR CREDIT CARD ONLY
- $payment_method_types = [
- 'CREDIT_CARD',
- ];
- } else if ($tipe_pembayaran == "online_to_offline") {
- // GENERATE PAYMENT REQUEST DOKU FOR ONLINE TO OFFLINE
- $payment_method_types = [
- 'ONLINE_TO_OFFLINE_ALFA',
- ];
- } else if ($tipe_pembayaran == "paylater") {
- // GENERATE PAYMENT REQUEST DOKU FOR PAYLATTER
- $payment_method_types = [
- 'PEER_TO_PEER_AKULAKU'
- ];
- } else if ($tipe_pembayaran == "qris") {
- // GENERATE PAYMENT REQUEST DOKU FOR QRIS
- $payment_method_types = [
- 'QRIS',
- ];
- }
- $customer_id = $pelanggan->id_person;
- $customer_name = $pelanggan->nama;
- $customer_email = $pelanggan->email;
- $customer_phone = $pelanggan->no_telepon;
- $customer_address = $pelanggan->alamat ?? 'Indonesia';
- $order_amount = round($final_total_bayar);
- // $order_invoice_number = ExtendedDokuHelper::genInvoice();
- $order_invoice_number = $penjualan->no_penjualan;
- logger('createTempCheckoutByTagihan:', [
- 'pelanggan_id' => $pelanggan->id_person,
- 'tagihan_id' => $penjualan->pjl_kode,
- 'order_invoice_number' => $order_invoice_number,
- 'final_total_tagihan' => $final_total_tagihan,
- 'final_total_bayar' => $final_total_bayar,
- ]);
- $doku_body = ExtendedDokuHelper::genJokulCheckout(
- $order_amount,
- $order_invoice_number,
- $customer_id,
- $customer_name,
- $customer_email,
- $customer_phone,
- $customer_address,
- $payment_method_types,
- $payment_due_date,
- $tipe_pembayaran
- );
- if ('SUCCESS' != $doku_body['message'][0]) {
- return [
- 'error' => 'generate_payment_failed',
- 'success' => null,
- ];
- }
- $doku_response = $doku_body['response'];
- logger('createTempCheckoutByTagihan:', [
- 'doku_response' => $doku_response,
- ]);
- DB::beginTransaction();
- $p = new DokuPayment();
- $p->id = Uuid::uuid4()->toString();
- $p->invoice_number = $doku_response['order']['invoice_number'];
- $p->currency = $doku_response['order']['currency'];
- $p->amount = $doku_response['order']['amount'];
- $p->session_id = $doku_response['order']['session_id'];
- $p->callback_url = $doku_response['order']['callback_url'];
- $p->payment_method_types = json_encode($doku_response['payment']['payment_method_types']);
- $p->payment_due_date = $doku_response['payment']['payment_due_date']; // number of minutes
- $p->payment_token_id = $doku_response['payment']['token_id'];
- $p->payment_url = $doku_response['payment']['url'];
- $p->payment_expired_date = value(function () use ($doku_response) {
- $str = $doku_response['payment']['expired_date']; // format: "YYYYmmddHHiiss"
- // "YYYYmmddHHiiss"
- // "01234567890123"
- $Y = substr($str, 0, 4);
- $m = substr($str, 4, 2);
- $d = substr($str, 6, 2);
- $H = substr($str, 8, 2);
- $i = substr($str, 10, 2);
- $s = substr($str, 12, 2);
- return Carbon::parse($Y . '-' . $m . '-' . $d . ' ' . $H . ':' . $i . ':' . $s)
- ->format('Y-m-d H:i:s');
- });
- $p->customer_email = $doku_response['customer']['email'];
- $p->customer_name = $doku_response['customer']['name'];
- $p->response_uuid = $doku_response['uuid']; // number eg: 2.2252107290737E+39
- $p->h_request_id = $doku_response['headers']['request_id'];
- $p->h_signature = $doku_response['headers']['signature'];
- $p->h_date = $doku_response['headers']['date']; // ISO: eg: "2021-07-29T00:11:51Z"
- $p->created_by = $pelanggan->nama;
- $p->updated_by = $pelanggan->nama;
- $p->save();
- DB::commit();
- $model = DokuPayment::find($p->id);
- unset($p);
- $result_data = [
- 'pelanggan_id' => $pelanggan->id_person,
- 'invoice_number' => $model->invoice_number,
- 'request_id' => $model->h_request_id,
- 'amount' => $model->amount,
- 'payment_url' => $model->payment_url,
- 'payment_expired_date' => $model->payment_expired_date,
- ];
- return [
- 'error' => null,
- 'success' => [
- 'data' => $result_data,
- ],
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement