当前位置: 首页 > 工具软件 > alipay > 使用案例 >

支付宝(Alipay)支付,超详细使用教程讲解!

卓雅达
2023-12-01

1、支付宝支付按钮调用

<script>
//支付宝支付
	$('.alipayClass').bind("click",function(){
		var imoney = $('#iMoney').val();
		$.ajax({
            type:"POST",
            url:"__APP__/aliPay/aliPayDo",//创建支付订单
            data:{money:money},
            dataType:'text',
            success:function(result){
            	var data=eval(result);
            	//成功
                if (data[0] == 1) {
                    window.location.href="__APP__?c=aliPay&a=alipay&paymentId="+data[1];
                };
                //失败
                if (data[0] == 0) {                	
					// $(".motify").show();
			  //       $(".motify-inner").text(data[1]);
			  //       $('.motify').delay(1000).hide(0);
                	opLayer('open',"网络故障充值失败!");
                	$("#goodcover").delay(1000).hide(0);
                	$(".Wtankdiv").delay(1000).hide(0);			  
			        return;	
                };
            }
        });  
	});
</script>

 

alipayDo生成订单页面

<?php>
//生成订单操作
    public function alipayDo(){
        if ($this->account) {
            $imoney = I("post.imoney");
            if (floatval($imoney)) {    
                //插入支付表
                $data['payment_id'] = date(ymdHis).$this->user_id.'w';//支付单号
                $data['money'] = floatval($imoney);//需要支付的金额
                $data['cur_money'] = 0;//支付货币金额
                $data['user_id'] = $this->user_id;
                $data['user_name'] = $this->account;
                $data['pay_type'] = 'excharge';
                $data['bank'] = 'xxx';//收款银行
                $data['pay_account'] = $this->user_id;//支付账号
                $data['created_time'] = time();
                $result = $this-M('create_payment')->data($data)->add();
                if ($result) {
                    echo json_encode(array(1,$data['payment_id']));
                }else{
                    echo json_encode(array(0,'订单生成失败,请重试!'));
                }
            }else{
                echo json_encode(array(0,'获取金额失败,请重试!'));
            }
        }else {
            echo json_encode(array(0,'手机号码信息不正确!'));
        }
    }
<?php>

支付宝支付页面

<?php
//支付宝支付
    public function alipay(){
    	header("Content-type:text/html;charset=utf-8");
        //导入支付宝支付 在ThinkPHP核心框架中
        vendor('Alipay.wshop.lib.alipay_submit','','.class.php');
        vendor('Alipay.wshop.alipay','','.config.php'); 

        $paymentId=trim(I('get.paymentId'));
        $paymentInfo = $this->M('create_payments')->where('payment_id = '.$paymentId)->find();
        //$type = I('type');
        if ($paymentInfo['pay_type'] == 'online') {
            $total_fee = round($paymentInfo['cash_fee'],2);
            $subject = "现金支付";
            $body = "现金支付";
        }else{
            $total_fee = round($paymentInfo['money'],2);
            $subject = "积分充值";
            $body = "积分充值";
        }

        $alipayConfig = alipayConfig();
        $parameter = array(
            "service"       => $alipayConfig['service'],
            "partner"       => $alipayConfig['partner'],
            "seller_id"  => $alipayConfig['seller_id'],
            "payment_type"  => $alipayConfig['payment_type'],
            "notify_url"    => $alipayConfig['notify_url'],
            "return_url"    => $alipayConfig['return_url'],
            "_input_charset"    => trim(strtolower($alipayConfig['input_charset'])),
            "out_trade_no"  => $paymentId,
            "subject"   => $subject,
            "total_fee" => $total_fee, //sprintf("%.2f", $paymentInfo['money']), 0.01, //
            "show_url"  => __APP__."/Pay/myCenter",
            "body"  => $body,
        );
        $alipaySubmit = new \AlipaySubmit($alipayConfig);  
        $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");

        echo $html_text;
        
    }

支付宝同步支付返回结果

<?php
 //支付宝同步支付返回结果
    public function aliPayReturnUrl(){
        vendor('Alipay.wshop.lib.alipay_notify','','.class.php');
        vendor('Alipay.wshop.alipay','','.config.php');        
        //计算得出通知验证结果
        $alipayConfig = alipayConfig();
        $alipayNotify = new \AlipayNotify($alipayConfig);
        $verify_result = $alipayNotify->verifyReturn();
        if($verify_result) {
            //验证成功                  
            //商户订单号
            $out_trade_no = $_GET['out_trade_no'];
            //支付宝交易号
            $trade_no = $_GET['trade_no'];
            //交易状态
            $trade_status = $_GET['trade_status'];
            $paymentInfo = $this->M('create_payments')->where('payment_id = '.$out_trade_no)->find();
            if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                $this->assign('res','success');
                $logdata =array(
                    'type'=>'expense',
                    'user_id'=>$paymentInfo['user_id'],
                    'operator'=>$paymentInfo['user_name'],
                    'message'=>'支付成功',
                    'logtime'=>time()
                );
            }else{
                $this->assign('res','fail');
                $logdata =array(
                    'type'=>'expense',
                    'user_id'=>$paymentInfo['user_id'],
                    'operator'=>$paymentInfo['user_name'],
                    'message'=>'支付失败!',
                    'logtime'=>time()
                );
            }
        }else {
            //验证失败
            //如要调试,请看alipay_notify.php页面的verifyReturn函数
            $logdata =array(
                'type'=>'expense',
                'user_id'=>$paymentInfo['user_id'],
                'operator'=>$paymentInfo['user_name'],
                'message'=>'验证失败!',
                'logtime'=>time()
            );
            //$this->userDataDepositLogModel->data($logdata)->add();
            $this->assign('res','fail');
        }
        //日志表        
        //$this->display('payResult');
        //日志表
        $this->userDataDepositLogModel->data($logdata)->add();
        $this->assign('paymentInfo',$paymentInfo);
        if ($paymentInfo['pay_type'] == 'recharge') {
            $this->display('Pay/payMyResult');
        }else{
            $this->display('Pay/onlineMyPayResult');
        }
    }

特别说明:第三方支付类库,存放在ThinkPHP下的Library下Vendor!具体可以我的本博客资源中下载使用!

 类似资料: