支付宝手机端支付model及调用单文件,开封即用

调用:

	class AlipaywapApp extends PaybaseApp
	{
	    private $wabapp;
	    public function __construct()
	    {
	        parent::__construct();
	        $this->wabapp = new WebAPP();
	    }
	    /**
	     * 创建支付
	     * @return [type] [description]
	     */
	    public function index()
	    {
	        header('Content-type:text/html; Charset=utf-8');
	        // 获取订单信息
	        $model_order =& m('order');
	        $order_info = $model_order->get('order_id='.$this->oid);
	        if (empty($order_info)) {
	            return $this->wabapp->error('无此订单');
	        }
	        //订单已支付
	        if ($order_info['status'] > ORDER_PENDING) {
	            return $this->wabapp->error('该订单已支付', '查看订单', $this->ORDER_INFO_URL);
	        }
	        // 获取支付方式的相关配置信息
	        $model_payment =& m('payment');
	        $payment_info = $model_payment->get('payment_code="pay3in1"');
	        $payment_config = unserialize($payment_info['config']);
	        if(empty($payment_config)){
	          return $this->wabapp->error('未获取到相关配置信息');
	        }
	        //实例化支付操作模型
	        $pay_class =& m('pay_do');
	        /*** 请填写以下配置信息 ***/
	        $appid = $payment_config['alipay_appid'];			//https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID
 
	        $returnUrl = $pay_class->_create_paynotify_url($this->oid, 'alipaypc');     //付款成功后的同步回调地址 不能有 ? & 符号
	        $notifyUrl = $pay_class->_create_notify_url('alipaypc');     //付款成功后的异步回调地址 不能有 ? & 符号
 
	        $outTradeNo = $order_info['order_sn'];     //你自己的商品订单号,不能重复
	        $payAmount = $order_info['order_amount'];          //付款金额,单位:元
	        //$payAmount = 0.01;          //付款金额,单位:元
	        $orderName = $order_info['order_sn'];    //订单标题
	        $signType = 'RSA2';			//签名算法类型,支持RSA2和RSA,推荐使用RSA2
	        $rsaPrivateKey = $payment_config['alipay_rsaPrivateKey'];		//商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
	        /*** 配置结束 ***/
	        $aliPay =& m('alipaywap');
	        $aliPay->setAppid($appid);
	        $aliPay->setReturnUrl($returnUrl);
	        $aliPay->setNotifyUrl($notifyUrl);
	        $aliPay->setRsaPrivateKey($rsaPrivateKey);
	        $aliPay->setTotalFee($payAmount);
	        $aliPay->setOutTradeNo($outTradeNo);
	        $aliPay->setOrderName($orderName);
	        $payConfigs = $aliPay->doPay();
 
	        $queryStr = http_build_query($payConfigs);
	        if ($aliPay->isWeixin()) {
	            echo '<script type="text/javascript" src="/static/alipay/js/ap.js"></script>
	                  <script>
	                      var gotoUrl = \'https://openapi.alipay.com/gateway.do?'.$queryStr.'\';
	                      _AP.pay(gotoUrl);
	                  </script>';
	        } else {
	            header("Location:https://openapi.alipay.com/gateway.do?{$queryStr}");
	        }
	    }
	}

model:

	class AlipaywapModel extends BaseModel
	{
	    protected $appId;
	    protected $returnUrl;
	    protected $notifyUrl;
	    protected $charset;
	    protected $totalFee;
	    protected $outTradeNo;
	    protected $orderName;
	    //私钥值
	    protected $rsaPrivateKey;
	    public function __construct()
	    {
	        $this->charset = 'utf8';
	    }
	    public function setAppid($appid)
	    {
	        $this->appId = $appid;
	    }
	    public function setReturnUrl($returnUrl)
	    {
	        $this->returnUrl = $returnUrl;
	    }
	    public function setNotifyUrl($notifyUrl)
	    {
	        $this->notifyUrl = $notifyUrl;
	    }
	    public function setRsaPrivateKey($rsaPrivateKey)
	    {
	        $this->rsaPrivateKey = $rsaPrivateKey;
	    }
 
	    public function setTotalFee($payAmount)
	    {
	        $this->totalFee = $payAmount;
	    }
 
	    public function setOutTradeNo($outTradeNo)
	    {
	        $this->outTradeNo = $outTradeNo;
	    }
 
	    public function setOrderName($orderName)
	    {
	        $this->orderName = $orderName;
	    }
 
	    /**
	     * 发起订单
	     * @param float $totalFee 收款总费用 单位元
	     * @param string $outTradeNo 唯一的订单号
	     * @param string $orderName 订单名称
	     * @param string $notifyUrl 支付结果通知url 不要有问号
	     * @param string $timestamp 订单发起时间
	     * @return array
	     */
	    public function doPay()
	    {
	        //请求参数
	        $requestConfigs = array(
	            'out_trade_no'=>$this->outTradeNo,
	            'product_code'=>'QUICK_WAP_WAY',
	            'total_amount'=>$this->totalFee, //单位 元
	            'subject'=>$this->orderName,  //订单标题
	        );
	        $commonConfigs = array(
	            //公共参数
	            'app_id' => $this->appId,
	            'method' => 'alipay.trade.wap.pay',             //接口名称
	            'format' => 'JSON',
	            'return_url' => $this->returnUrl,
	            'charset'=>$this->charset,
	            'sign_type'=>'RSA2',
	            'timestamp'=>date('Y-m-d H:i:s'),
	            'version'=>'1.0',
	            'notify_url' => $this->notifyUrl,
	            'biz_content'=>json_encode($requestConfigs),
	        );
	        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
	        return $commonConfigs;
	    }
	    public function generateSign($params, $signType = "RSA") {
	        return $this->sign($this->getSignContent($params), $signType);
	    }
	    protected function sign($data, $signType = "RSA") {
	        $priKey=$this->rsaPrivateKey;
	        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
	            wordwrap($priKey, 64, "\n", true) .
	            "\n-----END RSA PRIVATE KEY-----";
	        ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
	        if ("RSA2" == $signType) {
	            openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
	        } else {
	            openssl_sign($data, $sign, $res);
	        }
	        $sign = base64_encode($sign);
	        return $sign;
	    }
	    /**
	     * 校验$value是否非空
	     *  if not set ,return true;
	     *    if is null , return true;
	     **/
	    protected function checkEmpty($value) {
	        if (!isset($value))
	            return true;
	        if ($value === null)
	            return true;
	        if (trim($value) === "")
	            return true;
	        return false;
	    }
	    public function getSignContent($params) {
	        ksort($params);
	        $stringToBeSigned = "";
	        $i = 0;
	        foreach ($params as $k => $v) {
	            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
	                // 转换成目标字符集
	                $v = $this->characet($v, $this->charset);
	                if ($i == 0) {
	                    $stringToBeSigned .= "$k" . "=" . "$v";
	                } else {
	                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
	                }
	                $i++;
	            }
	        }
	        unset ($k, $v);
	        return $stringToBeSigned;
	    }
	    /**
	     * 转换字符集编码
	     * @param $data
	     * @param $targetCharset
	     * @return string
	     */
	    function characet($data, $targetCharset) {
	        if (!empty($data)) {
	            $fileType = $this->charset;
	            if (strcasecmp($fileType, $targetCharset) != 0) {
	                $data = mb_convert_encoding($data, $targetCharset, $fileType);
	            }
	        }
	        return $data;
	    }
	    /**
	     * 是否微信打开
	     * @return boolean [description]
	     */
	    function isWeixin(){
	        if ( strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false ) {
	            return true;
	        }
	        return false;
	    }
	}