/** * Veritrans Credit Card * * 1. Check Order * 2. Save Payment * * @return Response */ public function veritranscc() { if (!Input::has('order_id')) { return new JSend('error', (array) Input::all(), 'Tidak ada data order id.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Sale Parameter $order = Input::only('order_id', 'gross_amount', 'payment_type', 'masked_card', 'transaction_id'); //1a. Get original data $sale_data = \App\Models\Sale::findorfail($order['order_id']); //2. Save Payment $paid_data = new \App\Models\Payment(); $payment['transaction_id'] = $sale_data['id']; $payment['method'] = $order['payment_type']; $payment['destination'] = 'Veritrans'; $payment['account_name'] = $order['masked_card']; $payment['account_number'] = $order['transaction_id']; $payment['ondate'] = \Carbon\Carbon::parse($order['transaction_time'])->format('Y-m-d H:i:s'); $payment['amount'] = $order['gross_amount']; $paid_data = $paid_data->fill($payment); if (!$paid_data->save()) { $errors->add('Log', $paid_data->getError()); } if ($errors->count()) { DB::rollback(); return response()->json(new JSend('error', (array) Input::all(), $errors), 404); } DB::commit(); $final_sale = \App\Models\Sale::id($sale_data['id'])->with(['voucher', 'transactionlogs', 'user', 'transactiondetails', 'transactiondetails.varian', 'transactiondetails.varian.product', 'paidpointlogs', 'payment', 'shipment', 'shipment.address', 'shipment.courier', 'transactionextensions', 'transactionextensions.productextension'])->first()->toArray(); return response()->json(new JSend('success', (array) $final_sale), 200); }
/** * observe payment event saving * 1. check haven't been paid * 2. recalculate shipping_cost * 3. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); if ($model->package == '') { $model->package = 'regular'; } //1. check haven't been paid if ($model->sale()->count() && !in_array($model->sale->status, ['na', 'cart', 'wait']) && isset($model->getDirty()['address_id'])) { $errors->add('Shipment', 'Tidak dapat mengubah destinasi pengiriman.'); } //2. recalculate shipping_cost if ($model->address()->count()) { $shippingcost = ShippingCost::courierid($model->courier_id)->postalcode($model->address->zipcode)->first(); if ($shippingcost && $model->sale()->count() && $model->sale->transactiondetails()->count()) { $shipping_cost = $model->CountShippingCost($model->sale->transactiondetails, $shippingcost['cost']); $sale = Sale::findorfail($model->transaction_id); $sale->fill(['shipping_cost' => $shipping_cost]); if (!$sale->save()) { $errors->add('Shipment', $sale->getError()); } } else { $errors->add('Shipment', 'Tidak ada kurir ke tempat anda (Silahkan periksa kembali kode pos anda).'); } } elseif ($model->sale()->count()) { $sale = Sale::findorfail($model->transaction_id); $sale->fill(['shipping_cost' => 0]); if (!$sale->save()) { $errors->add('Shipment', $sale->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Store a sale * * 1. Save Sale * 2. Save Payment or shipment * 3. Save Transaction Log * * @return Response */ public function status() { if (!Input::has('sale')) { return new JSend('error', (array) Input::all(), 'Tidak ada data sale.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Sale Parameter $sale = Input::get('sale'); if (is_null($sale['id'])) { return new JSend('error', (array) Input::all(), 'Tidak ada data sale.'); } else { $is_new = false; } //1a. Get original data $sale_data = \App\Models\Sale::findorfail($sale['id']); //1b. Check if there were statuses differences if ($sale_data['status'] == $sale['status']) { $errors->add('Sale', 'Tidak ada perubahan status.'); } //2. Check if status = paid if (!$errors->count() && in_array($sale['status'], ['paid'])) { if (!isset($sale['payment']) || is_null($sale['payment'])) { $errors->add('Sale', 'Tidak ada data pembayaran.'); } else { $paid_data = \App\Models\Payment::findornew($sale['payment']['id']); $payment_rule = ['transaction_id' => 'exists:transactions,id|' . (!$paid_data ? '' : 'in:' . $sale_data['id']), 'method' => 'required|max:255', 'destination' => 'required|max:255', 'account_name' => 'required|max:255', 'account_number' => 'required|max:255', 'ondate' => 'required|date_format:"Y-m-d H:i:s"', 'amount' => 'required|numeric|in:' . $sale_data['bills']]; $validator = Validator::make($sale['payment'], $payment_rule); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $sale['payment']['transaction_id'] = $sale['id']; $paid_data = $paid_data->fill($sale['payment']); if (!$paid_data->save()) { $errors->add('Log', $paid_data->getError()); } } } } //2. Check if status = shipping if (!$errors->count() && in_array($sale['status'], ['shipping'])) { if (is_null($sale['shipment']['receipt_number'])) { $errors->add('Sale', 'Tidak ada nomor resi.'); } else { $shipping_data = \App\Models\Shipment::id($sale['shipment']['id'])->first(); if ($shipping_data) { $shipment_rule = ['receipt_number' => 'required|max:255']; $validator = Validator::make($sale['shipment'], $shipment_rule); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $sale['shipment']['transaction_id'] = $sale['id']; $shipping_data = $shipping_data->fill($sale['shipment']); if (!$shipping_data->save()) { $errors->add('Log', $shipping_data->getError()); } } } else { $errors->add('Log', 'Shipment tidak valid.'); } } } //3. Check if status = others if (!$errors->count() && !in_array($sale['status'], ['paid', 'shipping'])) { $log_rules = ['transaction_id' => 'exists:transactions,id|' . ($is_new ? '' : 'in:' . $sale_data['id']), 'status' => 'required|max:255|in:cart,wait,payment_process,paid,packed,shipping,delivered,canceled,abandoned']; $validator = Validator::make($sale, $log_rules); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $log_data = new \App\Models\TransactionLog(); $log_data = $log_data->fill(['status' => $sale['status'], 'transaction_id' => $sale_data['id'], 'notes' => isset($sale['notes']) ? $sale['notes'] : '']); if (!$log_data->save()) { $errors->add('Log', $log_data->getError()); } } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_sale = \App\Models\Sale::id($sale_data['id'])->with(['voucher', 'transactionlogs', 'user', 'transactiondetails', 'transactiondetails.varian', 'transactiondetails.varian.product', 'paidpointlogs', 'payment', 'shipment', 'shipment.address', 'shipment.courier', 'transactionextensions', 'transactionextensions.productextension'])->first()->toArray(); return new JSend('success', (array) $final_sale); }