Advertisement
deyan_ardi

DokuCheckPaymentHelper

Mar 10th, 2023
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers;
  4.  
  5. use App\Models\DokuPayment;
  6. use App\Models\mPenjualan;
  7. use App\Models\mPerson;
  8. use Carbon\Carbon;
  9. use Exception;
  10. use Illuminate\Support\Facades\DB;
  11. use Ramsey\Uuid\Uuid;
  12.  
  13. class DokuCheckoutPayment
  14. {
  15.     public static function calculateExpiredMinutes($datetime_due)
  16.     {
  17.         if (null == $datetime_due) {
  18.             throw new Exception('ERR_INVALID_DATETIME_DUE_1', 1);
  19.         }
  20.  
  21.         if (!is_string($datetime_due)) {
  22.             throw new Exception('ERR_INVALID_DATETIME_DUE_2', 1);
  23.         }
  24.  
  25.         // valid format: 0000-00-00
  26.         if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datetime_due)) {
  27.             throw new Exception('ERR_INVALID_DATETIME_DUE_3', 1);
  28.         }
  29.  
  30.         $int_now = now()->timestamp;
  31.         $int_due = Carbon::parse($datetime_due . ' ' . '24:59:59')->timestamp;
  32.  
  33.         if ($int_now > $int_due) {
  34.             return 0;
  35.         }
  36.  
  37.         $seconds = $int_due - $int_now;
  38.         $minutes = $seconds / 60;
  39.  
  40.         return $minutes;
  41.     }
  42.  
  43.     public static function createTempCheckoutByTagihan($daftar_tagihan_id, $user_id, $payment_due_date, $tipe_pembayaran)
  44.     {
  45.         $pelanggan = mPerson::find($user_id);
  46.         $penjualan = mPenjualan::where('pjl_kode', $daftar_tagihan_id)->where('pjl_status', 'belum lunas')->first();
  47.  
  48.         $final_total_tagihan = $penjualan->grand_total;
  49.         $final_total_bayar = $penjualan->sisa_tagihan;
  50.  
  51.         if ($tipe_pembayaran == "virtual_account") {
  52.             // GENERATE PAYMENT REQUEST DOKU FOR VIRTUAL ACCOUNT
  53.             $payment_method_types = [
  54.                 "VIRTUAL_ACCOUNT_BCA",
  55.                 "VIRTUAL_ACCOUNT_BANK_MANDIRI",
  56.                 "VIRTUAL_ACCOUNT_BANK_SYARIAH_MANDIRI",
  57.                 "VIRTUAL_ACCOUNT_DOKU",
  58.                 "VIRTUAL_ACCOUNT_BRI",
  59.                 "VIRTUAL_ACCOUNT_BNI",
  60.                 "VIRTUAL_ACCOUNT_BANK_PERMATA",
  61.                 "VIRTUAL_ACCOUNT_BANK_CIMB",
  62.                 "VIRTUAL_ACCOUNT_BANK_DANAMON",
  63.             ];
  64.         } else if ($tipe_pembayaran == "credit_card") {
  65.             // GENERATE PAYMENT REQUEST DOKU FOR CREDIT CARD ONLY
  66.             $payment_method_types = [
  67.                 'CREDIT_CARD',
  68.             ];
  69.         } else if ($tipe_pembayaran == "online_to_offline") {
  70.             // GENERATE PAYMENT REQUEST DOKU FOR ONLINE TO OFFLINE
  71.             $payment_method_types = [
  72.                 'ONLINE_TO_OFFLINE_ALFA',
  73.             ];
  74.         } else if ($tipe_pembayaran == "paylater") {
  75.             // GENERATE PAYMENT REQUEST DOKU FOR PAYLATTER
  76.             $payment_method_types = [
  77.                 'PEER_TO_PEER_AKULAKU'
  78.             ];
  79.         } else if ($tipe_pembayaran == "qris") {
  80.             // GENERATE PAYMENT REQUEST DOKU FOR QRIS
  81.             $payment_method_types = [
  82.                 'QRIS',
  83.             ];
  84.         }
  85.  
  86.         $customer_id = $pelanggan->id_person;
  87.         $customer_name = $pelanggan->nama;
  88.         $customer_email = $pelanggan->email;
  89.         $customer_phone = $pelanggan->no_telepon;
  90.         $customer_address = $pelanggan->alamat ?? 'Indonesia';
  91.  
  92.         $order_amount = round($final_total_bayar);
  93.         // $order_invoice_number = ExtendedDokuHelper::genInvoice();
  94.  
  95.         $order_invoice_number =  $penjualan->no_penjualan;
  96.  
  97.         logger('createTempCheckoutByTagihan:', [
  98.             'pelanggan_id' => $pelanggan->id_person,
  99.             'tagihan_id' => $penjualan->pjl_kode,
  100.             'order_invoice_number' => $order_invoice_number,
  101.             'final_total_tagihan' => $final_total_tagihan,
  102.             'final_total_bayar' => $final_total_bayar,
  103.         ]);
  104.  
  105.  
  106.         $doku_body = ExtendedDokuHelper::genJokulCheckout(
  107.             $order_amount,
  108.             $order_invoice_number,
  109.             $customer_id,
  110.             $customer_name,
  111.             $customer_email,
  112.             $customer_phone,
  113.             $customer_address,
  114.             $payment_method_types,
  115.             $payment_due_date,
  116.             $tipe_pembayaran
  117.         );
  118.  
  119.         if ('SUCCESS' != $doku_body['message'][0]) {
  120.             return [
  121.                 'error' => 'generate_payment_failed',
  122.                 'success' => null,
  123.             ];
  124.         }
  125.  
  126.         $doku_response = $doku_body['response'];
  127.  
  128.         logger('createTempCheckoutByTagihan:', [
  129.             'doku_response' => $doku_response,
  130.         ]);
  131.  
  132.         DB::beginTransaction();
  133.  
  134.         $p = new DokuPayment();
  135.         $p->id = Uuid::uuid4()->toString();
  136.         $p->invoice_number = $doku_response['order']['invoice_number'];
  137.         $p->currency = $doku_response['order']['currency'];
  138.         $p->amount = $doku_response['order']['amount'];
  139.         $p->session_id = $doku_response['order']['session_id'];
  140.         $p->callback_url = $doku_response['order']['callback_url'];
  141.         $p->payment_method_types = json_encode($doku_response['payment']['payment_method_types']);
  142.         $p->payment_due_date = $doku_response['payment']['payment_due_date']; // number of minutes
  143.         $p->payment_token_id = $doku_response['payment']['token_id'];
  144.         $p->payment_url = $doku_response['payment']['url'];
  145.         $p->payment_expired_date = value(function () use ($doku_response) {
  146.             $str = $doku_response['payment']['expired_date']; // format: "YYYYmmddHHiiss"
  147.  
  148.             // "YYYYmmddHHiiss"
  149.             // "01234567890123"
  150.             $Y = substr($str, 0, 4);
  151.             $m = substr($str, 4, 2);
  152.             $d = substr($str, 6, 2);
  153.             $H = substr($str, 8, 2);
  154.             $i = substr($str, 10, 2);
  155.             $s = substr($str, 12, 2);
  156.  
  157.             return Carbon::parse($Y . '-' . $m . '-' . $d . ' ' . $H . ':' . $i . ':' . $s)
  158.                 ->format('Y-m-d H:i:s');
  159.         });
  160.         $p->customer_email = $doku_response['customer']['email'];
  161.         $p->customer_name = $doku_response['customer']['name'];
  162.         $p->response_uuid = $doku_response['uuid']; // number eg: 2.2252107290737E+39
  163.         $p->h_request_id = $doku_response['headers']['request_id'];
  164.         $p->h_signature = $doku_response['headers']['signature'];
  165.         $p->h_date = $doku_response['headers']['date']; // ISO: eg: "2021-07-29T00:11:51Z"
  166.         $p->created_by = $pelanggan->nama;
  167.         $p->updated_by = $pelanggan->nama;
  168.         $p->save();
  169.  
  170.         DB::commit();
  171.  
  172.         $model = DokuPayment::find($p->id);
  173.         unset($p);
  174.  
  175.         $result_data = [
  176.             'pelanggan_id' => $pelanggan->id_person,
  177.             'invoice_number' => $model->invoice_number,
  178.             'request_id' => $model->h_request_id,
  179.             'amount' => $model->amount,
  180.             'payment_url' => $model->payment_url,
  181.             'payment_expired_date' => $model->payment_expired_date,
  182.         ];
  183.  
  184.         return [
  185.             'error' => null,
  186.             'success' => [
  187.                 'data' => $result_data,
  188.             ],
  189.         ];
  190.     }
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement