123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace app\api\controller;
- use app\admin\model\RecoveryBill;
- use app\admin\model\Recoveryorder;
- use app\admin\model\TablewareBill;
- use EasyWeChat\Factory;
- use app\common\controller\Api;
- class Pay extends Api
- {
- protected $noNeedLogin =['*'];
- protected $noNeedRight = ['*'];
- /**
- * @return void
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * 商城支付
- */
- public function Pay()
- {
- $pay_no = input('pay_no');
- if(empty($pay_no)){
- $this->error('支付信息不能为空');
- }
- $pay_model = new \app\admin\model\Pay();
- $user_model = new \app\admin\model\User();
- $pay_info = $pay_model->where('pay_no',$pay_no)->find();
- $openId = $user_model->where('id',$pay_info['user_id'])->value('openID');
- $notify_url = common_url().'/index.php/api/Notify/good_notify';
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $paymentData = $app->order->unify([
- 'body' => '商品购买',
- 'out_trade_no' => $pay_info['pay_no'],
- 'total_fee' => $pay_info['price']*100,
- 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
- 'openid' => $openId
- ]);
- if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
- //必须使用二次签名
- $appId = $paymentData['appid'];
- $nonceStr = $paymentData['nonce_str'];
- $prepay_id = $paymentData['prepay_id'];
- $timeStamp = time();
- $key = $config['key'];
- $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
- // 返回给小程序使用
- $data = [];
- $data['nonceStr'] = $nonceStr;
- $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
- $data['package'] = "prepay_id=" . $prepay_id;
- $data['paySign'] = $paySign;
- $data['signType'] = 'MD5';
- // 提交事务
- $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
- }
- else{
- $this->success('快去拉起支付',$paymentData);
- }
- }
- /**
- * @return void
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * 餐具订单月结支付
- */
- public function tableware_pay(){
- $pay_no = input('order_no');
- if(empty($pay_no)){
- $this->error('支付信息不能为空');
- }
- $tableware_bill_model = new TablewareBill();
- $user_model = new \app\admin\model\User();
- $pay_info = $tableware_bill_model->where('order_no',$pay_no)->find();
- $openId = $user_model->where('id',$pay_info['c_user_id'])->value('openID');
- $notify_url = common_url().'/index.php/api/Notify/tableware_notify';
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $paymentData = $app->order->unify([
- 'body' => '商品购买',
- 'out_trade_no' => $pay_info['order_no'],
- 'total_fee' => $pay_info['amount_money']*100,
- 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
- 'openid' => $openId
- ]);
- if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
- //必须使用二次签名
- $appId = $paymentData['appid'];
- $nonceStr = $paymentData['nonce_str'];
- $prepay_id = $paymentData['prepay_id'];
- $timeStamp = time();
- $key = $config['key'];
- $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
- // 返回给小程序使用
- $data = [];
- $data['nonceStr'] = $nonceStr;
- $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
- $data['package'] = "prepay_id=" . $prepay_id;
- $data['paySign'] = $paySign;
- $data['signType'] = 'MD5';
- // 提交事务
- $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
- }
- else{
- $order_no_new = 'CJYJ' . order_no($pay_info['c_user_id']);
- $tableware_bill_model->save(['order_no'=>$order_no_new],['order_no'=>$pay_no]);
- $this->error($paymentData['err_code_des']);
- }
- }
- /**
- * @return void
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * 回收订单月结支付
- */
- public function recovery_pay(){
- $order_no = input('order_no');
- if(empty($order_no)){
- $this->error('支付信息不能为空');
- }
- $recovery_bill_model = new RecoveryBill();
- $user_model = new \app\admin\model\User();
- $bill_info = $recovery_bill_model->where('order_no',$order_no)->find();
- $openId = $user_model->where('id',$bill_info['c_user_id'])->value('openID');
- $notify_url = common_url().'/index.php/api/Notify/recovery_notify';
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => $notify_url, // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $paymentData = $app->order->unify([
- 'body' => '商品购买',
- 'out_trade_no' => $bill_info['order_no'],
- 'total_fee' => $bill_info['amount_money']*100,
- 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
- 'openid' => $openId
- ]);
- if ($paymentData['return_code'] == 'SUCCESS' && $paymentData['result_code'] == 'SUCCESS') {
- //必须使用二次签名
- $appId = $paymentData['appid'];
- $nonceStr = $paymentData['nonce_str'];
- $prepay_id = $paymentData['prepay_id'];
- $timeStamp = time();
- $key = $config['key'];
- $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名!
- // 返回给小程序使用
- $data = [];
- $data['nonceStr'] = $nonceStr;
- $data['timeStamp'] = strval($timeStamp); // 小程序支付的timeStamp参数,必须使用这个 timeStamp,因为已经计算到了paySign中
- $data['package'] = "prepay_id=" . $prepay_id;
- $data['paySign'] = $paySign;
- $data['signType'] = 'MD5';
- // 提交事务
- $this->success('快去拉起支付',['data'=>$data,'notify_url'=>$notify_url]);
- }
- else{
- $order_no_new = 'HSYJ' . order_no($bill_info['c_user_id']);
- $recovery_bill_model->save(['order_no'=>$order_no_new],['order_no'=>$order_no]);
- $this->error($paymentData['err_code_des']);
- }
- }
- }
|