<?php
/**
 * 微信小程序支付模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class WxsmallpayModel extends Model{
    protected $tableName = 'Goods';
    /**
     * 生成预支付交易单，返回正确的预支付交易后调起小程序支付
     * @param array $param 订单与会员信息
     * @return array 返回小程序支付的必须信息
     */
    public function pay_done($param){
        $return = array('status'=>2,'msg'=>'获取支付信息失败');
		$id=$param['id'];
        if(empty($id))return $return;
        $where = array();
		$where['id']=$id;
        $where['is_paid']=array('neq',1);
		$pay_info = M('PayLog')->where($where)->field(true)->find();
		if(empty($pay_info)){
            $return['msg'] = '支付单号不存在';
            return $return;
        }
        $body_name=$param['body'];if(empty($body_name))$body_name="微信小程序支付";
		$pay_code = $pay_info['pay_code'];
        $where = array();
        $where['status']=1;
        $where['code']=$pay_code;
		$payconfig=M('Payment')->where($where)->field(true)->find();
		if(empty($pay_code)){
            $return['msg'] = '请选择支付方式';
            return $return;
        }
        $openid = idtoname($param['uid'],'Member','wxsmall_openid','id',false);
		$pay = new \Common\Vendor\Pay($pay_code,$payconfig);
		$vo = new \Common\Vendor\Pay\PayVo();
		$vo ->setBody($body_name)
            ->setFee($pay_info['need_pay'])
            ->setOrderNo($pay_info['order_sn'])
            ->setUserOpenid($openid);
		$RequestForm=$pay->buildRequestForm($vo);
		if($RequestForm['status']==1){
            return $RequestForm;
        }else{
            return $return;
        }
	}
    public function pay_moneydone($param){
        $return = array('status'=>2,'msg'=>'获取支付信息失败');
        $id=$param['id'];
        if(empty($id))return $return;
        $where = array();
        $where['id']=$id;
        $where['is_paid']=array('neq',1);
        $pay_info = M('PayLog')->where($where)->field(true)->find();
        $pay_info['is_wxsmall']=1;
        if(empty($pay_info)){
            $return['msg'] = '支付单号不存在';
            return $return;
        }
        $pay_code = $pay_info['pay_code'];
        $where = array();
        $where['status']=1;
        $where['code']=$pay_code;
        $payconfig=M('Payment')->where($where)->field(true)->find();
        if(empty($pay_code)){
            $return['msg'] = '请选择支付方式';
            return $return;
        }
        $openid = idtoname($param['uid'],'Member','wxsmall_openid','id',false);
        $pay = new \Common\Vendor\Pay($pay_code,$payconfig);
        $vo = new \Common\Vendor\Pay\PayVo();
        $title_name=I('get.title');if(empty($title_name))$title_name="支付";
        $vo->setBody($title_name)
            ->setFee($pay_info['need_pay'])
            ->setTitle($title_name)
            ->setOrderNo($pay_info['order_sn'])
            ->setParam($pay_info)
            ->setProduct_id($id)
            ->setUserOpenid($openid);
        $RequestForm=$pay->buildRequestForm($vo);
        if($RequestForm['status']==1){
            return $RequestForm;
        }else{
            return $return;
        }
    }
}
