Pay.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\RecoveryBill;
  4. use app\admin\model\Recoveryorder;
  5. use app\admin\model\TablewareBill;
  6. use EasyWeChat\Factory;
  7. use app\common\controller\Api;
  8. class Pay extends Api
  9. {
  10. protected $noNeedLogin =['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * @return void
  14. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  15. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  16. * @throws \GuzzleHttp\Exception\GuzzleException
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. * 商城支付
  21. */
  22. public function Pay()
  23. {
  24. $pay_no = input('pay_no');
  25. if(empty($pay_no)){
  26. $this->error('支付信息不能为空');
  27. }
  28. $pay_model = new \app\admin\model\Pay();
  29. $user_model = new \app\admin\model\User();
  30. $pay_info = $pay_model->where('pay_no',$pay_no)->find();
  31. $openId = $user_model->where('id',$pay_info['user_id'])->value('openID');
  32. $notify_url = common_url().'/index.php/api/Notify/good_notify';
  33. $config = [
  34. // 必要配置
  35. 'app_id' => 'wxdc450b7c5d0a0f80',
  36. 'mch_id' => '1635096198',
  37. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  38. 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
  39. ];
  40. $app = Factory::payment($config);
  41. $paymentData = $app->order->unify([
  42. 'body' => '商品购买',
  43. 'out_trade_no' => $pay_info['pay_no'],
  44. 'total_fee' => $pay_info['price']*100,
  45. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  46. 'openid' => $openId
  47. ]);
  48. if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
  49. //必须使用二次签名
  50. $appId = $paymentData['appid'];
  51. $nonceStr = $paymentData['nonce_str'];
  52. $prepay_id = $paymentData['prepay_id'];
  53. $timeStamp = time();
  54. $key = $config['key'];
  55. $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
  56. // 返回给小程序使用
  57. $data = [];
  58. $data['nonceStr'] = $nonceStr;
  59. $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
  60. $data['package'] = "prepay_id=" . $prepay_id;
  61. $data['paySign'] = $paySign;
  62. $data['signType'] = 'MD5';
  63. // 提交事务
  64. $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
  65. }
  66. else{
  67. $this->success('快去拉起支付',$paymentData);
  68. }
  69. }
  70. /**
  71. * @return void
  72. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  73. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  74. * @throws \GuzzleHttp\Exception\GuzzleException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. * 餐具订单月结支付
  79. */
  80. public function tableware_pay(){
  81. $pay_no = input('order_no');
  82. if(empty($pay_no)){
  83. $this->error('支付信息不能为空');
  84. }
  85. $tableware_bill_model = new TablewareBill();
  86. $user_model = new \app\admin\model\User();
  87. $pay_info = $tableware_bill_model->where('order_no',$pay_no)->find();
  88. $openId = $user_model->where('id',$pay_info['c_user_id'])->value('openID');
  89. $notify_url = common_url().'/index.php/api/Notify/tableware_notify';
  90. $config = [
  91. // 必要配置
  92. 'app_id' => 'wxdc450b7c5d0a0f80',
  93. 'mch_id' => '1635096198',
  94. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  95. 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
  96. ];
  97. $app = Factory::payment($config);
  98. $paymentData = $app->order->unify([
  99. 'body' => '商品购买',
  100. 'out_trade_no' => $pay_info['order_no'],
  101. 'total_fee' => $pay_info['amount_money']*100,
  102. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  103. 'openid' => $openId
  104. ]);
  105. if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
  106. //必须使用二次签名
  107. $appId = $paymentData['appid'];
  108. $nonceStr = $paymentData['nonce_str'];
  109. $prepay_id = $paymentData['prepay_id'];
  110. $timeStamp = time();
  111. $key = $config['key'];
  112. $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
  113. // 返回给小程序使用
  114. $data = [];
  115. $data['nonceStr'] = $nonceStr;
  116. $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
  117. $data['package'] = "prepay_id=" . $prepay_id;
  118. $data['paySign'] = $paySign;
  119. $data['signType'] = 'MD5';
  120. // 提交事务
  121. $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
  122. }
  123. else{
  124. $order_no_new = 'CJYJ' . order_no($pay_info['c_user_id']);
  125. $tableware_bill_model->save(['order_no'=>$order_no_new],['order_no'=>$pay_no]);
  126. $this->error($paymentData['err_code_des']);
  127. }
  128. }
  129. /**
  130. * @return void
  131. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  132. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  133. * @throws \GuzzleHttp\Exception\GuzzleException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. * 回收订单月结支付
  138. */
  139. public function recovery_pay(){
  140. $order_no = input('order_no');
  141. if(empty($order_no)){
  142. $this->error('支付信息不能为空');
  143. }
  144. $recovery_bill_model = new RecoveryBill();
  145. $user_model = new \app\admin\model\User();
  146. $bill_info = $recovery_bill_model->where('order_no',$order_no)->find();
  147. $openId = $user_model->where('id',$bill_info['c_user_id'])->value('openID');
  148. $notify_url = common_url().'/index.php/api/Notify/recovery_notify';
  149. $config = [
  150. // 必要配置
  151. 'app_id' => 'wxdc450b7c5d0a0f80',
  152. 'mch_id' => '1635096198',
  153. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  154. 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
  155. ];
  156. $app = Factory::payment($config);
  157. $paymentData = $app->order->unify([
  158. 'body' => '商品购买',
  159. 'out_trade_no' => $bill_info['order_no'],
  160. 'total_fee' => $bill_info['amount_money']*100,
  161. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  162. 'openid' => $openId
  163. ]);
  164. if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
  165. //必须使用二次签名
  166. $appId = $paymentData['appid'];
  167. $nonceStr = $paymentData['nonce_str'];
  168. $prepay_id = $paymentData['prepay_id'];
  169. $timeStamp = time();
  170. $key = $config['key'];
  171. $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
  172. // 返回给小程序使用
  173. $data = [];
  174. $data['nonceStr'] = $nonceStr;
  175. $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
  176. $data['package'] = "prepay_id=" . $prepay_id;
  177. $data['paySign'] = $paySign;
  178. $data['signType'] = 'MD5';
  179. // 提交事务
  180. $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
  181. }
  182. else{
  183. $order_no_new = 'HSYJ' . order_no($bill_info['c_user_id']);
  184. $recovery_bill_model->save(['order_no'=>$order_no_new],['order_no'=>$order_no]);
  185. $this->error($paymentData['err_code_des']);
  186. }
  187. }
  188. }