You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
9.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Weixin;
  3. use Illuminate\Http\Request;
  4. use App\Common\ReturnData;
  5. use App\Common\Helper;
  6. class OrderController extends BaseController
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. //订单列表
  13. public function orderList(Request $request)
  14. {
  15. $pagesize = 10;
  16. $offset = 0;
  17. if (isset($_REQUEST['page'])) {
  18. $offset = ($_REQUEST['page'] - 1) * $pagesize;
  19. }
  20. $status = $request->input('status', -1);
  21. $postdata = array(
  22. 'limit' => $pagesize,
  23. 'offset' => $offset,
  24. 'status' => $status, //0或者不传表示全部,1待付款,2待发货,3待收货,4待评价,5退款/售后
  25. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  26. );
  27. $url = env('APP_API_URL') . "/order_list";
  28. $res = curl_request($url, $postdata, 'GET');
  29. $data['list'] = $res['data']['list'];
  30. $data['totalpage'] = ceil($res['data']['count'] / $pagesize);
  31. if (isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax'] == 1) {
  32. $html = '';
  33. if ($res['data']['list']) {
  34. foreach ($res['data']['list'] as $k => $v) {
  35. $html .= '<li><a href="' . $v['goods']['goods_detail_url'] . '"><span class="goods_thumb"><img alt="' . $v['goods']['title'] . '" src="' . env('APP_URL') . $v['goods']['litpic'] . '"></span></a>';
  36. $html .= '<div class="goods_info"><p class="goods_tit">' . $v['goods']['title'] . '</p>';
  37. $html .= '<p class="goods_price">¥<b>' . $v['goods']['price'] . '</b></p>';
  38. $html .= '<p class="goods_des fr"><span id="del_history" onclick="delconfirm(\'' . route('weixin_user_goods_history_delete', array('id' => $v['id'])) . '\')">删除</span></p>';
  39. $html .= '</div></li>';
  40. }
  41. }
  42. exit(json_encode($html));
  43. }
  44. return view('weixin.order.orderList', $data);
  45. }
  46. //订单详情
  47. public function orderDetail(Request $request)
  48. {
  49. $id = $request->input('id', '');
  50. $postdata = array(
  51. 'id' => $id,
  52. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  53. );
  54. $url = env('APP_API_URL') . "/order_detail";
  55. $res = curl_request($url, $postdata, 'GET');
  56. $data['post'] = $res['data'];
  57. if (empty($data['post'])) {
  58. $this->error_jump('订单不存在');
  59. }
  60. return view('weixin.order.orderDetail', $data);
  61. }
  62. //订单评价
  63. public function orderComment(Request $request)
  64. {
  65. if (Helper::isPostRequest()) {
  66. if ($_POST['comment']) {
  67. foreach ($_POST['comment'] as $k => $v) {
  68. $_POST['comment'][$k]['comment_type'] = 0;
  69. $_POST['comment'][$k]['comment_rank'] = 5;
  70. }
  71. } else {
  72. $this->error_jump('评论失败');
  73. }
  74. $postdata = array(
  75. 'order_id' => $_POST['order_id'],
  76. 'comment' => json_encode($_POST['comment']),
  77. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  78. );
  79. $url = env('APP_API_URL') . "/comment_batch_add";
  80. $res = curl_request($url, $postdata, 'POST');
  81. if ($res['code'] != 0) {
  82. $this->error_jump('评论失败');
  83. }
  84. $this->success_jump('评论成功', route('weixin_order_list'));
  85. }
  86. $id = $request->input('id', '');
  87. if ($id == '') {
  88. $this->error_jump('您访问的页面不存在或已被删除!');
  89. }
  90. $postdata = array(
  91. 'id' => $id,
  92. 'order_status' => 3,
  93. 'refund_status' => 0,
  94. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  95. );
  96. $url = env('APP_API_URL') . "/order_detail";
  97. $res = curl_request($url, $postdata, 'GET');
  98. $data['post'] = $res['data'];
  99. if (empty($data['post'])) {
  100. $this->error_jump('您访问的页面不存在或已被删除!');
  101. }
  102. return view('weixin.order.orderComment', $data);
  103. }
  104. //订单支付
  105. public function pay($id)
  106. {
  107. //获取订单详情
  108. $postdata = array(
  109. 'id' => $id, //要支付的订单id
  110. 'order_status' => 0,
  111. 'pay_status' => 0,
  112. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  113. );
  114. $url = env('APP_API_URL') . "/order_detail";
  115. $res = curl_request($url, $postdata, 'GET');
  116. $data['order_detail'] = $res['data'];
  117. $data['order_id'] = $id;
  118. if ($res['code'] != 0 || empty($data['order_detail'])) {
  119. $this->error_jump('订单不存在或已过期');
  120. }
  121. //获取会员信息
  122. $postdata = array(
  123. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  124. );
  125. $url = env('APP_API_URL') . "/user_info";
  126. $res = curl_request($url, $postdata, 'GET');
  127. $data['user_info'] = $res['data'];
  128. //判断余额是否足够
  129. $is_balance_enough = 1; //足够
  130. if ($data['order_detail']['order_amount'] > $data['user_info']['money']) {
  131. $is_balance_enough = 0;
  132. }
  133. $data['is_balance_enough'] = $is_balance_enough;
  134. return view('weixin.order.pay', $data);
  135. }
  136. public function dopay(Request $request)
  137. {
  138. $order_id = $request->input('order_id', '');
  139. $payment_id = $request->input('payment_id', '');
  140. if ($order_id == '' || $payment_id == '') {
  141. $this->error_jump(ReturnData::PARAMS_ERROR);
  142. }
  143. $url = '';
  144. if ($payment_id == 1) //余额支付
  145. {
  146. $url = route('weixin_order_yuepay', array('order_id' => $order_id));
  147. } elseif ($payment_id == 2) //微信支付
  148. {
  149. $url = route('weixin_order_wxpay', array('order_id' => $order_id));
  150. }
  151. if ($url == '') {
  152. $this->error_jump('订单不存在或已过期');
  153. } else {
  154. header('Location: ' . $url);
  155. exit;
  156. }
  157. }
  158. //订单余额支付
  159. public function orderYuepay(Request $request)
  160. {
  161. $order_id = $request->input('order_id', '');
  162. //修改订单状态
  163. $postdata = array(
  164. 'id' => $order_id,
  165. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  166. );
  167. $url = env('APP_API_URL') . "/order_yue_pay";
  168. $res = curl_request($url, $postdata, 'POST');
  169. if ($res['code'] == ReturnData::SUCCESS) {
  170. $this->success_jump('支付成功', route('weixin_order_list'));
  171. }
  172. $this->error_jump('支付失败');
  173. }
  174. //订单-微信支付
  175. public function orderWxpay(Request $request)
  176. {
  177. $order_id = $request->input('order_id', '');
  178. //获取订单详情
  179. $postdata = array(
  180. 'id' => $order_id, //要支付的订单id
  181. 'order_status' => 0,
  182. 'pay_status' => 0,
  183. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  184. );
  185. $url = env('APP_API_URL') . "/order_detail";
  186. $res = curl_request($url, $postdata, 'GET');
  187. if ($res['code'] != 0) {
  188. $this->error_jump('订单不存在或已过期');
  189. }
  190. $data['order_detail'] = $res['data'];
  191. $data['order_id'] = $order_id;
  192. //微信支付-start
  193. require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类
  194. require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类
  195. $body = '订单支付';//订单详情
  196. $out_trade_no = $data['order_detail']['order_sn'];//订单号
  197. $total_fee = floatval($data['order_detail']['order_amount'] * 100);//价格0.01
  198. $attach = 'pay_type=2'; //附加数据,pay_type=2订单支付,示例:xxx=1&yyy=2
  199. $notify_url = route('notify_wxpay_jsapi');//通知地址
  200. $wxconfig = \WxPayConfig::wxconfig();
  201. //=========步骤1:网页授权获取用户openid============
  202. $jsApi = new \JsApi_pub($wxconfig);
  203. $openid = $jsApi->getOpenid();
  204. //=========步骤2:使用统一支付接口,获取prepay_id============
  205. //使用统一支付接口
  206. $unifiedOrder = new \UnifiedOrder_pub($wxconfig);
  207. //设置统一支付接口参数
  208. //设置必填参数
  209. //appid已填,商户无需重复填写
  210. //mch_id已填,商户无需重复填写
  211. //noncestr已填,商户无需重复填写
  212. //spbill_create_ip已填,商户无需重复填写
  213. //sign已填,商户无需重复填写
  214. $unifiedOrder->setParameter("openid", "$openid");//微信用户openid,trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
  215. $unifiedOrder->setParameter("body", "$body");//商品描述
  216. $unifiedOrder->setParameter("out_trade_no", "$out_trade_no");//商户订单号
  217. $unifiedOrder->setParameter("total_fee", "$total_fee");//总金额
  218. $unifiedOrder->setParameter("attach", "$attach"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用,示例:a=1&b=2
  219. $unifiedOrder->setParameter("notify_url", "$notify_url");//通知地址
  220. $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型,JSAPI,NATIVE,APP...
  221. $prepay_id = $unifiedOrder->getPrepayId();
  222. //=========步骤3:使用jsapi调起支付============
  223. $jsApi->setPrepayId($prepay_id);
  224. $jsApiParameters = $jsApi->getParameters();
  225. $data['jsApiParameters'] = $jsApiParameters;
  226. $data['returnUrl'] = route('weixin_order_list'); //支付完成要跳转的url,跳转到用户订单列表页面
  227. return view('weixin.order.orderWxpay', $data);
  228. }
  229. }