SHARE
TWEET

ini code lk untuk payment virtual account

a guest Jul 18th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Class Interface Library
  5.  * Creator: Elka
  6.  * Email: elka@tokodistributor.com
  7.  * Date: 2019/07/09
  8.  */
  9.  
  10. namespace App\Libs;
  11.  
  12. use App\Model\Mysqld\App\PaymentMethodReseller;
  13. use App\Model\Mysqld\Config\PaymentMethod;
  14. use App\Model\Mysqld\Transaction\OrderSummary;
  15.  
  16. class PaymentVirtualAccount extends PaymentMethodAbstract
  17. {
  18.  
  19.     const CATEGORIES = 'VIRTUAL_ACCOUNT';
  20.     const BNI_CODE_LENGTH = 8;
  21.     const BNI = 'BNI';
  22.  
  23.     public function beforeTrans(
  24.         OrderSummary $order
  25.     ): bool {
  26.         $this->setModel($order);
  27.         $this->setOrderData($this->parseOrder($order));
  28.         $serviceName = $this->getService();
  29.         switch ($serviceName) {
  30.             case self::BNI:
  31.                 if (!$this->createVaBNI()) {
  32.                     return false;
  33.                 }
  34.             break;
  35.         }
  36.         return true;
  37.     }
  38.  
  39.     private function createVaBNI(&$seq = 0)
  40.     {
  41.         $serviceName = self::BNI;
  42.         $success = false;
  43.         $orderData = $this->getOrderData();
  44.         $paymentMethodModel = new PaymentMethod();
  45.         $qry = $paymentMethodModel->where(
  46.                 'categories', $this->getCategories()
  47.             )
  48.             ->where('name', $serviceName)
  49.             ->first();
  50.         if (!$qry) {
  51.             $this->setErrMsg(__('payment_method.NOT_FOUND'));
  52.             return false;
  53.         }
  54.         $pMethodResModel = new PaymentMethodReseller();
  55.         $paymentData = [
  56.             'reseller_id' => $orderData['resellerId'],
  57.             'reseller_code' => $orderData['resellerId'],
  58.             'payment_method_id' => $qry->id,
  59.             'is_active' => 1,
  60.             'expired_at' => $orderData['orderExpired'],
  61.             'payment_method_name' => $serviceName
  62.         ];
  63.         $vaCode = '';
  64.         $pMethodResModel = new PaymentMethodReseller();
  65.         if ($qryMethod = $pMethodResModel->create($paymentData)) {
  66.             $lengthLeft = self::BNI_CODE_LENGTH - strlen($qryMethod->id);
  67.             $vaCode = str_repeat(0, $lengthLeft).$qryMethod->id;
  68.             $qryMethod->code = $vaCode;
  69.             $qryMethod->save();
  70.         }
  71.         $content = [
  72.             'trx_id' => (string) $orderData['orderId'],
  73.             'trx_amount' => (string) $orderData['orderAmount'],
  74.             'billing_type' => 'c',
  75.             'customer_name' => $orderData['resellerName'],
  76.             'customer_email' => $orderData['resellerEmail'],
  77.             'customer_phone' => $orderData['resellerPhone'],
  78.             'virtual_account' => $qry->no_rekening.$vaCode,
  79.             'datetime_expired' => $orderData['orderExpired'],
  80.             'description' => $orderData['orderNumber']
  81.         ];
  82.         $host = config('server.HOST_VA_BNI');
  83.         $url = $host.'/create';
  84.         $client = new \GuzzleHttp\Client();
  85.         $response = $client->request('POST', $url, [
  86.             'body' => json_encode($content),
  87.             'headers' => [
  88.                 'Content-Type' => 'application/json',
  89.             ]
  90.         ]);
  91.         $resp = json_decode($response->getBody(), true);
  92.         $seq++;
  93.         if ($resp['status'] == 202) {
  94.             if ($seq < self::TRY_LOOP) {
  95.                 $this->createVaBNI($seq);
  96.             } else {
  97.                 $this->setErrMsg($resp['message']);
  98.             }
  99.         } else {
  100.             $model = OrderSummary::find(
  101.                 $orderData['orderId']
  102.             );
  103.             $paymentDataModel = json_decode($model->payment_data, true);
  104.             $paymentDataModel['name'] = $orderData['resellerName'];
  105.             $paymentDataModel['number'] = $content['virtual_account'];
  106.             $paymentDataModel['description'] = $content['description'];
  107.             $model->payment_data = json_encode($paymentDataModel);
  108.             $updateOrderModel = $model->save();
  109.             if ($updateOrderModel) {
  110.                 $success = true;
  111.             }
  112.         }
  113.         return $success;
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top