作者 何书鹏

测试代付

@@ -13,6 +13,8 @@ use app\api\model\Third; @@ -13,6 +13,8 @@ use app\api\model\Third;
13 use app\api\model\UserAddress; 13 use app\api\model\UserAddress;
14 use app\api\model\UserCoupon; 14 use app\api\model\UserCoupon;
15 use app\common\controller\Api; 15 use app\common\controller\Api;
  16 +use fast\Http;
  17 +use think\Config;
16 use think\Db; 18 use think\Db;
17 use think\exception\PDOException; 19 use think\exception\PDOException;
18 use Yansongda\Pay\Pay; 20 use Yansongda\Pay\Pay;
@@ -584,7 +586,7 @@ class Order extends Api @@ -584,7 +586,7 @@ class Order extends Api
584 * @ApiMethod (POST) 586 * @ApiMethod (POST)
585 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") 587 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
586 * @ApiParams (name="id", type="integer", required=true, description="订单ID") 588 * @ApiParams (name="id", type="integer", required=true, description="订单ID")
587 - * @ApiParams (name="openid", type="string", required=false, description="代付人openid") 589 + * @ApiParams (name="code", type="string", required=false, description="代付人code")
588 * @ApiReturn ({ 590 * @ApiReturn ({
589 'code':'1', 591 'code':'1',
590 'msg':'返回成功' 592 'msg':'返回成功'
@@ -593,7 +595,7 @@ class Order extends Api @@ -593,7 +595,7 @@ class Order extends Api
593 public function pay() 595 public function pay()
594 { 596 {
595 $order_id = $this->request->post('id'); 597 $order_id = $this->request->post('id');
596 - $openid = $this->request->post('openid'); 598 + $code = $this->request->post('code');
597 empty($order_id) && $this->error('缺少必需参数'); 599 empty($order_id) && $this->error('缺少必需参数');
598 600
599 $model = new \app\api\model\Order(); 601 $model = new \app\api\model\Order();
@@ -607,14 +609,30 @@ class Order extends Api @@ -607,14 +609,30 @@ class Order extends Api
607 ->find(); 609 ->find();
608 if (!$third) $this->error('下单人未注册'); 610 if (!$third) $this->error('下单人未注册');
609 611
  612 + // 获取代付人的openid
  613 + $openid = $third['openid'];
  614 + if(!empty($code)){
  615 + $wxapi = Http::get('https://api.weixin.qq.com/sns/jscode2session',[
  616 + 'js_code' => $code,
  617 + 'grant_type' => 'authorization_code',
  618 + 'secret' => Config::get('site.secret'),
  619 + 'appid' => Config::get('site.appid')
  620 + ]);//请求openid
  621 + $wxapi = json_decode($wxapi,true);
  622 + if (isset($wxapi['errcode'])){
  623 + $this->error($wxapi['errmsg']);
  624 + }
  625 + $openid = $wxapi['openid'];
  626 + }
  627 +
610 $params = [ 628 $params = [
611 'type' => 'wechat', 629 'type' => 'wechat',
612 'orderid' => $order['order_no'], 630 'orderid' => $order['order_no'],
613 'title' => '订单号-'.$order['order_no'], 631 'title' => '订单号-'.$order['order_no'],
614 - 'amount' => $order['pay_price'],  
615 -// 'amount' => 0.01, 632 +// 'amount' => $order['pay_price'],
  633 + 'amount' => 0.01,
616 'method' => 'miniapp', 634 'method' => 'miniapp',
617 - 'openid' => !empty($openid) ? $openid : $third['openid'], 635 + 'openid' => $openid,
618 'notifyurl' => $this->request->domain().'/api/notify/orderNotify/type/wechat', 636 'notifyurl' => $this->request->domain().'/api/notify/orderNotify/type/wechat',
619 'returnurl' => '', 637 'returnurl' => '',
620 ]; 638 ];