<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付接口
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\AllwinEcard;
use app\allwin\model\AllwinEcardOrder;
use app\allwin\model\AllwinEcardUser;
use app\allwin\widget\Card;
use app\common\model\SystemMemberForm;
use app\common\facade\WechatPay;

class Ecarddopay extends Base{
   
    /**
     * 初始化当前应用是否登录
     * @return void
     */
    public function initialize() {
        parent::initialize();
        if(!$this->user){
            exit(json_encode(['code'=>404,'msg'=>'用户认证失败']));
        }
    }

    /**
     * 购买商品
     */
    public function goods(){
        if (request()->isPost()) {
            $data = [
                'member_miniapp_id' => $this->miniapp_id,
                'uid'               => $this->user->id,
                'card_id'           => $this->request->param('card_id/s'),
                'order_no'          => $this->request->param('order_no/s'),
            ];
            $validate = $this->validate($data, 'Dopay.ecard');
            if (true !== $validate) {
                return enjson(403,$validate);
            }
            //判断会员卡是否设置
            $ecard = AllwinEcard::where(['member_miniapp_id' => $this->miniapp_id])->find();
            if(!$ecard){
                return enjson(403,'会员卡未设置');
            }
            //判断是否有库存
            $info = Card::info($ecard['card_id'],$this->miniapp_id);
            if($info['quantity'] == 0){
                return enjson(403,'会员卡暂无库存');
            }
            $ecardUser = AllwinEcardUser::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id,'card_id' => $data['card_id'],'is_del' => 0])->find();
            if($ecardUser){
                return enjson(403,'请不要重复购买会员卡');
            }
            //唤醒微信支付参数
            $order_no = empty($data['order_no']) ? 'ECARD'.order_no() :$data['order_no'];
            $payparm = [
                'openid'     => $this->user->miniapp_uid,
                'miniapp_id' => $this->miniapp_id,
                'name'       => '会员卡开卡',
                'order_no'   => $order_no,
                'total_fee'  => $ecard['annual_fee']*100,
                'notify_url' => api(1,'allwin/ecardNotify/ecard',$this->miniapp_id),
            ];
            //读取配置
            $setting = model('AllwinConfig')->getConfig($this->miniapp_id);
            if($setting->is_psp == 1){
                $default_mchid = model('MchId')->getMch(0, $this->miniapp_id);  //默认收款账户
                if (empty($default_mchid)) {
                    return enjson(403,'未找到商户号');
                }
                $payparm['mchid'] = $default_mchid->mchid;
            }
            $ispay = WechatPay::orderPay($payparm);
            if ($ispay['code'] == 0) {
                return enjson(403,$ispay['msg']);
            }
            //创建记录
            $data['amount'] = $ecard['annual_fee'];
            empty($data['order_no']) ?: $rel = AllwinEcardOrder::insertOrder($data,$order_no);
            if($rel){
                //把支付packageID添加到数据库
                $package = $ispay['data']['package'];
                SystemMemberForm::addForm($this->miniapp_id,$this->user->id,substr($package,10));
                return enjson(200,'成功',$ispay['data']);
            }else{
                return enjson(204,'购买商品失败');
            }
        }
    }
}