thinkphp5网站中集成使用支付宝手机支付接口

Posted 菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp5网站中集成使用支付宝手机支付接口相关的知识,希望对你有一定的参考价值。

今天以thinkphp5中使用支付宝的手机支付接口为例。

一、创建基本页面pay/alipay_wap_submit.php(开始创建订单)

<!DOCTYPE html>
<html>
<head>
    <title>支付宝手机网站支付接口</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <meta http-equiv="pragma" content="no-cache" />
    <style>
        *{
            margin:0;
            padding:0;
        }
        ul,ol{
            list-style:none;
        }
        body{
            font-family: "Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
        }
        .hidden{
            display:none;
        }
        .new-btn-login-sp{
            padding: 1px;
            display: inline-block;
            width: 75%;
        }
        .new-btn-login {
            background-color: #02aaf1;
            color: #FFFFFF;
            font-weight: bold;
            border: none;
            width: 100%;
            height: 30px;
            border-radius: 5px;
            font-size: 16px;
        }
        #main{
            width:100%;
            margin:0 auto;
            font-size:14px;
        }
        .red-star{
            color:#f00;
            width:10px;
            display:inline-block;
        }
        .null-star{
            color:#fff;
        }
        .content{
            margin-top:5px;
        }
        .content dt{
            width:100px;
            display:inline-block;
            float: left;
            margin-left: 20px;
            color: #666;
            font-size: 13px;
            margin-top: 8px;
        }
        .content dd{
            margin-left:120px;
            margin-bottom:5px;
        }
        .content dd input {
            width: 85%;
            height: 28px;
            border: 0;
            -webkit-border-radius: 0;
            -webkit-appearance: none;
        }
        #foot{
            margin-top:10px;
            position: absolute;
            bottom: 15px;
            width: 100%;
        }
        .foot-ul{
            width: 100%;
        }
        .foot-ul li {
            width: 100%;
            text-align:center;
            color: #666;
        }
        .note-help {
            color: #999999;
            font-size: 12px;
            line-height: 130%;
            margin-top: 5px;
            width: 100%;
            display: block;
        }
        #btn-dd{
            margin: 20px;
            text-align: center;
        }
        .foot-ul{
            width: 100%;
        }
        .one_line{
            display: block;
            height: 1px;
            border: 0;
            border-top: 1px solid #eeeeee;
            width: 100%;
            margin-left: 20px;
        }
        .am-header {
            display: -webkit-box;
            display: -ms-flexbox;
            display: box;
            width: 100%;
            position: relative;
            padding: 7px 0;
            -webkit-box-sizing: border-box;
            -ms-box-sizing: border-box;
            box-sizing: border-box;
            background: #1D222D;
            height: 50px;
            text-align: center;
            -webkit-box-pack: center;
            -ms-flex-pack: center;
            box-pack: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            box-align: center;
        }
        .am-header h1 {
            -webkit-box-flex: 1;
            -ms-flex: 1;
            box-flex: 1;
            line-height: 18px;
            text-align: center;
            font-size: 18px;
            font-weight: 300;
            color: #fff;
        }
    </style>
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
    <h1>支付宝手机网站支付窗口</h1>
</header>
<div id="main">
    <form name=alipayment action="<?php echo url(\'alipay/alipay_wap_do\',[\'type\'    =>\'cz\'])?>" method=post>
        <div id="body" style="clear:left">
            <dl class="content">

                    <input id="WIDout_trade_no" name="WIDout_trade_no" type="hidden"/>


                    <input id="WIDsubject" name="WIDsubject"  type="hidden"/>

                <hr class="one_line">
                <dt>充值金额
                    :</dt>
                <dd>
                    <input id="WIDtotal_fee" name="WIDtotal_fee" required="required" oninvalid="setCustomValidity(\'请输入充值金额!\');" oninput="setCustomValidity(\'\');"/>
                </dd>

                    <input id="WIDshow_url" name="WIDshow_url"  type="hidden"/>


                    <input id="WIDbody" name="WIDbody"  type="hidden"/>

                <hr class="one_line">
                <dt></dt>
                <dd id="btn-dd">
                        <span class="new-btn-login-sp">
                            <button class="new-btn-login" type="submit" style="text-align:center;">确 认</button>
                        </span>
                    <span class="note-help">如果您点击“确认”按钮,即表示您同意该次的执行操作。</span>
                </dd>
            </dl>
        </div>
    </form>
    <div id="foot">
        <ul class="foot-ul">
            <li>
                支付宝版权所有 2015-2018 ALIPAY.COM
            </li>
        </ul>
    </div>
</div>
</body>
<script language="javascript">
    function GetDateNow() {
        var vNow = new Date();
        var sNow = "";
        sNow += String(vNow.getFullYear());
        sNow += String(vNow.getMonth() + 1);
        sNow += String(vNow.getDate());
        sNow += String(vNow.getHours());
        sNow += String(vNow.getMinutes());
        sNow += String(vNow.getSeconds());
        sNow += String(vNow.getMilliseconds());
        document.getElementById("WIDout_trade_no").value =  sNow;
        document.getElementById("WIDsubject").value = "支付宝手机充值";
        document.getElementById("WIDtotal_fee").value = "";
    }
    GetDateNow();
</script>



</html>

 

 

二、配置基本参数

/*支付宝wap支付参数*/
    \'alipay_wap_config\' =>[
        \'partner\'           =>\'208862*****17685\',
        \'seller_id\'         =>\'20886*****17685\',//和上面一样
        \'key\'       =>\'j60fqq5a*********vsi1w4iz9t\',
        \'notify_url\'        =>\'http://\'.$_SERVER[\'HTTP_HOST\'].\'/index/alipay/alipay_wap_notify\',
        \'return_url\'        =>\'http://\'.$_SERVER[\'HTTP_HOST\'].\'/index/alipay/alipay_wap_return\',
        \'sign_type\'         =>strtoupper(\'MD5\'),
        \'input_charset\'         =>strtolower(\'utf-8\'),
        \'cacert\'            =>getcwd().\'\\\\cacert.pem\',//需要放在根目录
        \'transport\'         =>\'http\',
        \'payment_type\'      =>\'1\',
        \'service\'           =>\'alipay.wap.create.direct.pay.by.user\'
    ],

三、控制器处理请求:

 //表单提交支付
    public function alipay_wap_do($type)
    {
        if(!$type){
            throw new \\think\\exception\\HttpException(403, \'非法请求\');
        }
 
        $WIDtotal_fee = $_POST[\'WIDtotal_fee\'];
        $alipay_config = Config::get(\'alipay_wap_config\');
 
        switch ($type){
            case \'cz\':
                if (\'\' == $WIDtotal_fee) {
                    return alert(\'请输入充值金额\', url(\'pay/alipay_wap_submit\'));
                } elseif (!preg_match(\'/(^[1-9]([0-9]+)?(\\.[0-9]{1,2})?$)|
                (^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)/\', $WIDtotal_fee)) {
                    return alert(\'请输入正确格式的金额数量\', url(\'pay/alipay_wap_submit\'));
                }
                Session::set(\'dawn_alipay_pay_action\',\'cz\');
                break;
            case \'hk\':
                Session::set(\'dawn_alipay_pay_action\',\'hk\');
                break;
        }
 
 
 
        //p($alipay_config);die;
 
        //商户订单号,商户网站订单系统中

以上是关于thinkphp5网站中集成使用支付宝手机支付接口的主要内容,如果未能解决你的问题,请参考以下文章

ThinkPHP5 支付宝 电脑与手机支付扩展库

ThinkPHP5 支付宝支付扩展库(超简单,超好用~)

ThinkPHP5 支付宝支付扩展库(超简单,超好用~)

商城系统支付方式如何配置?支付方式有哪些

支付宝手机网站支付开发指引

C#版支付宝手机网站支付接口API之(转)