LevelOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\User;
  4. use app\common\model\UserLevel;
  5. use app\common\model\LevelOrder as LOM;
  6. use think\Db;
  7. /**
  8. * @title 会员等级订单
  9. * @controller LevelOrder
  10. * @group base
  11. */
  12. class LevelOrder extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. parent::checkLogin();
  18. }
  19. /**
  20. * @title 开通会员(续费)订单
  21. * @desc 开通会员(续费)订单
  22. * @author qc
  23. * @url /api/Level_order/createOrder
  24. * @method POST
  25. * @header name:Authorization require:1 desc:Token
  26. * @param name:level_id type:int require:1 default:0 desc:会员等级
  27. * @param name:level_key type:int require:1 default:0 desc:等级价格的key值
  28. * @return name:order_id type:int default:0 desc:订单id
  29. */
  30. public function createOrder(){
  31. $level_id = input('post.level_id');
  32. $level_key = input('post.level_key');
  33. $user_info = User::where('id',$this->user_id)->field('id,group_id,is_first,account_type')->find();
  34. if($user_info['account_type'] == 1 && $user_info['is_first'] == 0) $this->error('请联系公司管理员进行会员开通');
  35. $level_set = UserLevel::where('id', $level_id)->find()->toArray();
  36. if (!$level_set['price']) $this->error('会员价格设置有误');
  37. $level_price = json_decode($level_set['price'], true);
  38. if (!isset($level_price[$level_key])) $this->error('会员价格设置有误.');
  39. $order_insert = [
  40. 'user_id' => $this->user_id,
  41. 'order_no' => get_order_sn(),
  42. 'level_id' => $level_id,
  43. 'level_key' => $level_key,
  44. 'month' => $level_price[$level_key]['time'],
  45. 'price_total' =>$level_price[$level_key]['price'],
  46. ];
  47. $order_info = LOM::create($order_insert);
  48. $this->success('ok', ['order_id' => $order_info->id]);
  49. }
  50. /**
  51. * @title 获取订单详情
  52. * @desc 获取订单详情
  53. * @author qc
  54. * @method GET
  55. * @url /api/Level_order/getOrderDetail
  56. * @header name:Authorization require:1 desc:Token
  57. * @param name:order_id type:int default:-- desc:订单id
  58. * @return name:id type:int default:-- desc:订单id
  59. * @return name:order_no type:string default:-- desc:订单号
  60. * @return name:price_total type:float default:-- desc:待支付金额
  61. * @return name:level_id type:float default:-- desc:等级id
  62. * @return name:level_name type:float default:-- desc:等级名称
  63. * @return name:month type:float default:-- desc:时长(月)
  64. * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
  65. * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝,5.h5微信内支付)
  66. * @return name:pay_no type:string default:-- desc:支付号
  67. * @return name:pay_at type:string default:-- desc:支付时间
  68. * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
  69. * @return name:cancel_at type:string default:-- desc:取消时间
  70. * @return name:cancel_desc type:string default:-- desc:取消原因
  71. * @return name:cancel_desc type:string default:-- desc:取消原因
  72. * @return name:level_set type:array default:-- desc:购买会员设置
  73. * @return name:level_set.title type:string default:-- desc:标题
  74. * @return name:level_set.price type:float default:-- desc:价格
  75. * @return name:level_set.time type:int default:-- desc:时间(月)
  76. */
  77. public function getOrderDetail()
  78. {
  79. $order_id = input('get.order_id');
  80. $detail = LOM::where('id',$order_id)->find()->toArray();
  81. $level_set = UserLevel::where('id', $detail['level_id'])->find()->toArray();
  82. $detail['level_name'] = $level_set['name'];
  83. $detail['level_set'] = json_decode($level_set['price'],true)[$detail['level_key']];
  84. $this->success('',['detail'=>$detail]);
  85. }
  86. /**
  87. * @title 订单支付
  88. * @desc 订单支付
  89. * @author qc
  90. * @url /api/Level_order/payOrder
  91. * @method POST
  92. * @header name:Authorization require:1 desc:Token
  93. * @param name:order_id type:int require:1 default:0 desc:订单id
  94. * @param name:pay_type type:int require:1 default:0 desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝)
  95. * @return name:config type:array default:0 desc:小程序支付配置
  96. * @return name:config.appId type:string default:-- desc:公众账号ID
  97. * @return name:config.signType type:string default:-- desc:签名类型
  98. * @return name:config.paySign type:string default:-- desc:签名
  99. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  100. * @return name:config.timestamp type:string default:-- desc:时间戳
  101. * @return name:config.partnerid type:string default:-- desc:商户号
  102. * @return name:config.prepayid type:string default:-- desc:唯一支付号
  103. * @return name:config.package type:string default:-- desc:package
  104. * @return name:config.mch_id type:string default:-- desc:商户号(H5)
  105. * @return name:config.pay_no type:string default:-- desc:订单支付号(H5)
  106. * @return name:config.notify_url type:string default:-- desc:回调地址(H5)
  107. * @return name:config.total_fee type:int default:-- desc:支付金额(分!!!)
  108. * @return name:code_url type:string default:-- desc:付款码url
  109. * @return name:openid type:string default:-- desc:openid
  110. */
  111. public function payOrder()
  112. {
  113. $order_id = input('post.order_id');
  114. $pay_type = input('post.pay_type',1);
  115. $order_info = LOM::where('id',$order_id)->find()->toArray();
  116. if($order_info['status'] != 0) $this->error('订单状态错误');
  117. if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
  118. if($order_info['price_total'] <= 0) $this->error('订单金额错误');
  119. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();// 支付单号
  120. if(!$order_info['pay_no']) LOM::where('id',$order_id)->update(['pay_no'=>$pay_no]);
  121. $user_info = User::where('id',$this->user_id)->find()->toArray();
  122. $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>'','openid'=>$user_info['openid']];
  123. Db::startTrans();
  124. LOM::where('id',$order_id)->update(['pay_type'=>$pay_type]);
  125. try {
  126. switch ($pay_type){
  127. case 1://微信
  128. $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
  129. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'MWEB');
  130. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  131. $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
  132. $pay_config['config']['pay_no'] = $pay_no;
  133. $pay_config['config']['notify_url'] = $notify_url;
  134. $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
  135. $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
  136. $ret_data['config'] = $pay_config['config'];
  137. break;
  138. case 2 :
  139. $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
  140. $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
  141. $config = Alipay::ali_pay_pc('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
  142. $ret_data['config']['ali_url'] = $config;
  143. $ret_data['ali_url'] = $config;
  144. break;
  145. case 3:
  146. $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
  147. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
  148. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  149. $ret_data['config'] = $pay_config['config'];
  150. break;
  151. case 4:
  152. $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
  153. $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url);
  154. $ret_data['config']['ali_url'] = $config;
  155. $ret_data['ali_url'] = $config;
  156. break;
  157. }
  158. Db::commit();
  159. }catch (\Exception $e){
  160. $this->is_commit = false;
  161. $this->ret_msg = $e->getMessage();
  162. Db::rollback();
  163. }
  164. $this->transReturn($ret_data);
  165. }
  166. /**
  167. * @title 开通会员
  168. * @desc 开通会员
  169. * @author qc
  170. * @url /api/Level_order/payOrderNew
  171. * @method POST
  172. * @header name:Authorization require:1 desc:Token
  173. * @param name:level_id type:int require:1 default:0 desc:会员等级
  174. * @param name:level_key type:int require:1 default:0 desc:等级价格的key值
  175. * @param name:pay_type type:int require:1 default:0 desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝,5.h5微信内支付)
  176. * @return name:config type:array default:0 desc:支付配置
  177. * @return name:config.appId type:string default:-- desc:公众账号ID
  178. * @return name:config.signType type:string default:-- desc:签名类型
  179. * @return name:config.paySign type:string default:-- desc:签名
  180. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  181. * @return name:config.timestamp type:string default:-- desc:时间戳
  182. * @return name:config.partnerid type:string default:-- desc:商户号
  183. * @return name:config.prepayid type:string default:-- desc:唯一支付号
  184. * @return name:config.package type:string default:-- desc:package
  185. * @return name:config.mch_id type:string default:-- desc:商户号(H5)
  186. * @return name:config.pay_no type:string default:-- desc:订单支付号(H5)
  187. * @return name:config.notify_url type:string default:-- desc:回调地址(H5)
  188. * @return name:config.total_fee type:int default:-- desc:支付金额(分!!!)
  189. * @return name:code_url type:string default:-- desc:付款码url
  190. * @return name:openid type:string default:-- desc:openid
  191. */
  192. public function payOrderNew()
  193. {
  194. $level_id = input('post.level_id');
  195. $level_key = input('post.level_key');
  196. $pay_type = input('post.pay_type',1);
  197. $user_info = User::where('id',$this->user_id)->find();
  198. if($user_info['account_type'] == 1 && $user_info['is_first'] == 0) $this->error('请联系公司管理员进行会员开通');
  199. $level_set = UserLevel::where('id', $level_id)->find()->toArray();
  200. if (!$level_set['price']) $this->error('会员价格设置有误');
  201. $level_price = json_decode($level_set['price'], true);
  202. if (!isset($level_price[$level_key])) $this->error('会员价格设置有误.');
  203. $order_insert = [
  204. 'user_id' => $this->user_id,
  205. 'order_no' => get_order_sn(),
  206. 'level_id' => $level_id,
  207. 'level_key' => $level_key,
  208. 'pay_type' => $pay_type,
  209. 'month' => $level_price[$level_key]['time'],
  210. 'price_total' =>$level_price[$level_key]['price'],
  211. 'pay_no' =>get_order_sn()
  212. ];
  213. $order_info = LOM::create($order_insert);
  214. $order_info = $order_info->toArray();
  215. $pay_no = $order_info['pay_no'];
  216. $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>'','openid'=>$user_info['openid']];
  217. Db::startTrans();
  218. try {
  219. switch ($pay_type){
  220. case 1://微信
  221. $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
  222. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'MWEB');
  223. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  224. $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
  225. $pay_config['config']['pay_no'] = $pay_no;
  226. $pay_config['config']['notify_url'] = $notify_url;
  227. $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
  228. $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
  229. $ret_data['config'] = $pay_config['config'];
  230. break;
  231. case 2 :
  232. $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
  233. $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
  234. $config = Alipay::ali_pay_pc('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
  235. $ret_data['config']['ali_url'] = $config;
  236. $ret_data['ali_url'] = $config;
  237. break;
  238. case 3:
  239. $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
  240. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
  241. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  242. $ret_data['config'] = $pay_config['config'];
  243. break;
  244. case 4:
  245. $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
  246. $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url);
  247. $ret_data['config']['ali_url'] = $config;
  248. $ret_data['ali_url'] = $config;
  249. break;
  250. case 5 :
  251. if(!$user_info['openid']) $this->exception('请绑定openid');
  252. $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
  253. $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  254. if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
  255. $ret_data['config'] = $pay_config['config'];
  256. break;
  257. }
  258. Db::commit();
  259. }catch (\Exception $e){
  260. $this->is_commit = false;
  261. $this->ret_msg = $e->getMessage();
  262. Db::rollback();
  263. }
  264. $this->transReturn($ret_data);
  265. }
  266. }