first commit
This commit is contained in:
241
app/Http/Controllers/OrderController.php
Normal file
241
app/Http/Controllers/OrderController.php
Normal file
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\GoodsResource;
|
||||
use App\Http\Resources\OrderListResource;
|
||||
use App\Http\Resources\OrderResource;
|
||||
use App\Models\Goods;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderDetail;
|
||||
// use App\Utils\WXBizDataCrypt;
|
||||
use Illuminate\Http\Request;
|
||||
use Orders;
|
||||
use Validator;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 全部订单
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2019-04-15T09:21:38+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$list = Order::where('user_id', $this->user->id)
|
||||
->whereIn('state', [Order::ORDER_PAID])
|
||||
->orderBy('id', 'desc')
|
||||
->paginate();
|
||||
|
||||
return OrderListResource::collection($list)->additional([
|
||||
'status' => 'SUCCESS',
|
||||
'status_code' => 200,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public function show(Order $order)
|
||||
{
|
||||
return $this->success(new OrderResource($order));
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public function cancel(Order $order)
|
||||
{
|
||||
try {
|
||||
$order->cancel(2);
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
return $this->failed('取消失败' . $e->getmessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function orderPay($order, $payway)
|
||||
{
|
||||
|
||||
if ($payway == 'wxpay') {
|
||||
|
||||
if (!$order->canPay()) {
|
||||
throw new \Exception(" 当前订单不可支付" . $order->canPay());
|
||||
}
|
||||
// 创建支付订单
|
||||
$payment = $order->payment()->create([
|
||||
'type' => $payway,
|
||||
]);
|
||||
|
||||
$app = app('wechat.payment');
|
||||
|
||||
$result = $app->order->unify([
|
||||
'body' => '商品订单支付-' . $order->orderid,
|
||||
'out_trade_no' => $payment->out_trade_no,
|
||||
'total_fee' => $order->amount * 100,
|
||||
'notify_url' => route('order.notify'),
|
||||
'trade_type' => 'JSAPI',
|
||||
'openid' => $this->user->openid,
|
||||
]);
|
||||
|
||||
if ($result->result_code == 'SUCCESS') {
|
||||
$result['config'] = $app->jssdk->bridgeConfig($result->prepay_id, false);
|
||||
$result['order'] = $order;
|
||||
return $result;
|
||||
} else {
|
||||
throw new \Exception($result->err_code_des . 'skyxu');
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("支付方式选择有误");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 单品立即购买
|
||||
* @param
|
||||
* @return array
|
||||
*/
|
||||
public function directBuy(Request $request)
|
||||
{
|
||||
$goodsID = $request->goods_id;
|
||||
$number = 1;
|
||||
|
||||
$goods = Goods::find($goodsID);
|
||||
|
||||
if (!$goods) {
|
||||
return $this->failed('商品包未找到或已下架');
|
||||
}
|
||||
|
||||
if ($goods->canOrder() !== true) {
|
||||
return $this->failed($goods->canOrder());
|
||||
}
|
||||
|
||||
$data['total'] = $goods->price * $number;
|
||||
$data['goods'] = new GoodsResource($goods);
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 立即购买下单
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function directSave(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'goods_id' => 'required',
|
||||
'company' => 'required',
|
||||
'area' => 'required',
|
||||
'encryptedData' => 'required',
|
||||
'iv' => 'required',
|
||||
// 'code' => 'required',
|
||||
], [
|
||||
'goods_id.required' => '必须提交产品id',
|
||||
'company.required' => '请输入企业名称',
|
||||
'area.required' => '请输入企业注册地',
|
||||
'encryptedData.required' => '手机号获取失败',
|
||||
'iv.required' => '手机号获取失败',
|
||||
'code.required' => '手机号获取失败',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->failed($validator->errors()->first());
|
||||
}
|
||||
|
||||
$goodsID = $request->goods_id;
|
||||
$number = 1;
|
||||
$company = $request->company;
|
||||
$area = $request->area;
|
||||
$encryptedData = urldecode($request->encryptedData);
|
||||
$iv = urldecode($request->iv);
|
||||
$code = urldecode($request->code);
|
||||
|
||||
if ($iv == 'undefined') {
|
||||
return $this->failed('手机号获取失败');
|
||||
}
|
||||
|
||||
if ($encryptedData == 'undefined') {
|
||||
return $this->failed('手机号获取失败');
|
||||
}
|
||||
|
||||
$app = app('wechat.mini_program');
|
||||
if ($code) {
|
||||
$session = $app->auth->session($code);
|
||||
|
||||
if ($session->errcode > 0) {
|
||||
return $this->failed('提交订单失败');
|
||||
}
|
||||
$session_key = $this->session_key;
|
||||
} else {
|
||||
$session_key = $this->user->session_key;
|
||||
}
|
||||
|
||||
//解密手机号
|
||||
// $pc = new WXBizDataCrypt(config('wechat.mini_program.default.app_id'), $session_key);
|
||||
// $errCode = $pc->decryptData($encryptedData, $iv, $decryptedData);
|
||||
|
||||
// if ($errCode != 0) {
|
||||
// return $this->failed('提交订单失败p');
|
||||
// }
|
||||
// $mobile = $decryptedData->phoneNumber;
|
||||
|
||||
//解密手机号
|
||||
$decryptedData = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
|
||||
$mobile = $decryptedData['phoneNumber'];
|
||||
|
||||
$goods = Goods::find($goodsID);
|
||||
|
||||
if ($this->user->session_key != $session_key) {
|
||||
$this->user->session_key = $session_key;
|
||||
$this->user->save();
|
||||
}
|
||||
|
||||
if (!$goods) {
|
||||
return $this->failed('商品未找到或已下架');
|
||||
}
|
||||
|
||||
if ($goods->canOrder() !== true) {
|
||||
return $this->failed($goods->canOrder());
|
||||
}
|
||||
|
||||
$company = ['company' => $company, 'area' => $area, 'mobile' => $mobile];
|
||||
|
||||
try {
|
||||
$items = array();
|
||||
array_push($items, new OrderDetail(['goods' => $goods, 'number' => $number]));
|
||||
$order = Orders::create($this->user->id, $items, $goods->type, null, json_encode($company));
|
||||
|
||||
$data = $this->orderPay($order, 'wxpay');
|
||||
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->failed('下单失败' . $e->getmessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接付款,适用于未付款订单,再次发起付款
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2019-05-08T15:50:10+0800
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function pay(Request $request)
|
||||
{
|
||||
$order = Order::where('orderid', $request->orderid)->first();
|
||||
|
||||
if ($order && $order->canPay()) {
|
||||
$data = $this->orderPay($order, 'wxpay');
|
||||
return $this->success($data);
|
||||
} else {
|
||||
return $this->failed('订单状态不可支付');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user