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; //商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = $_POST['WIDout_trade_no']; //订单名称,必填
$subject = $_POST['WIDsubject']; //付款金额,必填
$total_fee = $_POST['WIDtotal_fee']; //收银台页面上,商品展示的超链接,必填
$show_url = $_POST['WIDshow_url']; //商品描述,可空
$body = $_POST['WIDbody']; /************************************************************/ //构造要请求的参数数组,无需改动
$parameter = array(
"service" => $alipay_config['service'],
"partner" => $alipay_config['partner'],
"seller_id" => $alipay_config['seller_id'],
"payment_type" => $alipay_config['payment_type'],
"notify_url" => $alipay_config['notify_url'],
"return_url" => $alipay_config['return_url'],
"_input_charset" => trim(strtolower($alipay_config['input_charset'])),
"out_trade_no" => $out_trade_no,
"subject" => $subject,
"total_fee" => $total_fee,
"show_url" => $show_url,
//"app_pay" => "Y",//启用此参数能唤起钱包APP支付宝
"body" => $body,
//其他业务参数根据在线开发文档,添加参数.
文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.2Z6TSk&treeId=60&articleId=103693&docType=1
//如"参数名" => "参数值" 注:上一个参数末尾需要“,”逗号。 ); //建立请求
$alipaySubmit = new \Alipay_wap\AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认");
echo $html_text;
} public function alipay_wap_notify()
{
$alipay_config = Config::get('alipay_wap_config');
//计算得出通知验证结果
$alipayNotify = new \Alipay_wap\AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify(); if ($verify_result) {//验证成功
/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //商户订单号 $out_trade_no = $_POST['out_trade_no']; //支付宝交易号 $trade_no = $_POST['trade_no']; //交易状态
$trade_status = $_POST['trade_status']; if ($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序 //注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
} else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序 //注意:
//付款完成后,支付宝系统发送该交易状态通知 //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
} //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— echo "success"; //请不要修改或删除 ////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
} else {
//验证失败
echo "fail"; //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
}
} public function alipay_wap_return()
{
$alipay_config = Config::get('alipay_wap_config');
//计算得出通知验证结果
$alipayNotify = new \Alipay_wap\AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
if ($verify_result) {//验证成功
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表 //商户订单号 $out_trade_no = $_GET['out_trade_no']; //支付宝交易号 $trade_no = $_GET['trade_no']; //交易状态
$trade_status = $_GET['trade_status']; if ($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
} else {
echo "trade_status=" . $_GET['trade_status'];
} /*echo "验证成功<br />";*/
//p($_GET);die; //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— /**验证成功,请加入自己逻辑**/
}
/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
} else {
//验证失败
//如要调试,请看alipay_notify.php页面的verifyReturn函数
echo "验证失败";
}
}
thinkphp5网站中集成使用支付宝手机支付接口的更多相关文章
- [译]MVC网站教程(四):MVC4网站中集成jqGrid表格插件(系列完结)
目录 1. 介绍 2. 软件环境 3. 在运行示例代码之前(源代码 + 示例登陆帐号) 4. jqGrid和AJAX 5. GridSettings 6. ...
- java web项目(spring项目)中集成webservice ,实现对外开放接口
什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...
- 网站中集成jquery.imgareaselect实现图片的本地预览和选择截取
imgAreaSelect 是由 Michal Wojciechowski开发的一款非常好用的jquery插件,实现了图片的截取功能.其文档和Demo也是很详尽的.大家可以到http://odynie ...
- 网站如何集成百度UEditor编辑器
在平时的网站维护使用过程中,富文本编辑器是网站必不可少的元素之一.现在市面上各种编辑器功能设计参差不齐,自己做了几个网站都是用蝉知建站系统做的,而蝉知默认内置的编辑器是KindEditor,功能简单, ...
- 第四百零三节,python网站在线支付,支付宝接口集成与远程调试,
第四百零三节,python网站在线支付,支付宝接口集成与远程调试, windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS ...
- 如何通过图片在 HTTPS 网站中获取 HTTP 接口数据
<script> (function() { var Decode=function(b){var e;e=[];var a=b.width,c=b.height,d=document.c ...
- 黑科技!仅需 3 行代码,就能将 Gitter 集成到个人网站中,实现一个 IM 即时通讯聊天室功能?
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- TODO:macOS上ThinkPHP5和Semantic-UI集成
TODO:macOS上ThinkPHP5和Semantic-UI集成 1. 全局安装 (on OSX via homebrew)Composer 是 homebrew-php 项目的一部分 2. 把X ...
- 支付宝Payto接口的C#.net实现方法
例一: using System; using System.Data; using System.Configuration; using System.Collections; using Sys ...
随机推荐
- 20181104_C#线程之Thread_ThreadPool_使用Thread实现回到和带参数的回调
C# .net Framework多线程演变路径: 1.0 1.1 时代使用Thread 2.0 时代使用ThreadPool 3.0 时代使用Task 4.0 时代使用 ...
- python学习 (三十一) python中的class
1 python的类: Python类都继承自object. __init__: 构造函数,如果不写,有一个默认的. __init__: 这个构造函数只能有一个,Python中不能有多个构造函数. ...
- node 中的定时器, nextTick()和setImmediate()的使用
1.node中使用定时器的问题在于,它并非精确的.譬如setTimeout()设定一个任务在10ms后执行,但是在9ms后,有一个任务占用了5ms,再次轮到定时器时,已经耽误了4ms. 好了node中 ...
- 【UVALive】3695 Distant Galaxy(......)
题目 传送门:QWQ 分析 好喵啊~~~~ 不会做 正解看蓝书P53吧 代码 #include <cstdio> #include <algorithm> using name ...
- css引用方式
网页中引用CSS样式 内联样式 行内样式表 外部样式表 链接式 导入式 内嵌方式 style标签 <!doctype html> <html> <head> < ...
- 前端学习---JavaScript
JavaScript基本知识 JavaScript是一门独立的语言,像我们学习php,python等需要安装apache,python3.6,那我们学习JavaScript只需要我们电脑有一个浏览器即 ...
- Linux系统构架 - HA集群配置
一.安装 HA 即(high available)高可用,又被叫做双机热备,用于关键性业务. 简单理解就是,有两台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至 ...
- WPF Shader 正片叠底
新建文本文档拷贝以下代码 //正片叠底sampler2D implicitInput : register(s0);sampler2D oldInput : register(s1);float4 m ...
- 【HDU2825】Wireless Password【AC自动机,状态压缩DP】
题意 题目给出m(m<=10)个单词,每个单词的长度不超过10且仅由小写字母组成,给出一个正整数n(n<=25)和正整数k,问有多少方法可以组成长度为n的文本且最少包含k个给出的单词. 分 ...
- linux 安装网络监控插件indicator-sysmonitor
1.添加源 sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor 2.更新源 sudo apt-get update 3.安装 su ...