点击蓝字关注我们!每天获取最新的编程小知识!
源 / php中文网 源 / www.php.cn
在前面的文章《PHP实现个人支付宝支付开发(四)》中,我们为大家介绍了在网站上安装使用支付宝接口文件的流程。
(视频版请点击本文末尾左下角:
下面我们继续结合之前的案例源码,给大家介绍PHP实现个人支付宝支付开发功能的相关内容。
codepay_config.php文件是码支付的配置文件。前文中的码支付ID和通信秘钥就在如下图中的位置。
前文中创建的数据表如下。 案例中index.html前台文件:<
html
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
meta
http-equiv
=
"Access-Control-Allow-Origin"
content
=
"*"
>
<
title
>信息
title
>
<
link
rel
=
"stylesheet"
href
=
"__INDEX__/layui/css/layui.css"
media
=
"all"
>
head
>
<
body
>
<
ul
class
=
"layui-nav"
style
=
"border-radius:0px;"
lay-filter
=
""
>
<
li
class
=
"layui-nav-item"
><
a
href
=
""
>最新活动
a
>
li
>
<
li
class
=
"layui-nav-item "
><
a
href
=
""
>产品
a
>
li
>
<
li
class
=
"layui-nav-item"
><
a
href
=
""
>大数据
a
>
li
>
<
li
class
=
"layui-nav-item"
><
a
href
=
""
>社区
a
>
li
>
ul
>
<
div
class
=
"layui-carousel"
id
=
"test1"
>
<
div
carousel-item>
<
div
>条目1
div
>
<
div
>条目2
div
>
<
div
>条目3
div
>
<
div
>条目4
div
>
<
div
>条目5
div
>
div
>
div
>
<
div
class
=
"layui-fluid"
>
<
div
class
=
"layui-row layui-col-space15"
>
<
div
class
=
"layui-col-md12"
>
<
div
class
=
"layui-card"
>
<
div
class
=
"layui-card-header"
>网站设置
div
>
<
div
class
=
"layui-card-body"
pad15>
<
div
class
=
"layui-form"
lay-filter
=
"text"
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>信息名
label
>
<
div
class
=
"layui-input-block"
>
<
input
type
=
"text"
name
=
"provenance_url"
id
=
"provenance_url"
placeholder
=
"信息名"
value
=
""
class
=
"layui-input"
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>信息描述
label
>
<
div
class
=
"layui-input-inline"
style
=
"width: 85%;"
>
<
textarea
name
=
"provenance_desc"
id
=
"provenance_desc"
lay-verify
=
"required"
placeholder
=
"请仔细填写描述"
class
=
"layui-textarea"
>
textarea
>
div
>
<
div
class
=
"layui-form-mid layui-word-aux"
>
<
span
class
=
"x-red"
style
=
"color: red"
>*必填
span
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>联系方式
label
>
<
div
class
=
"layui-input-inline"
>
<
input
type
=
"text"
name
=
"user_qq"
id
=
"user_qq"
lay-verify
=
"required"
placeholder
=
"要求填写QQ联系方式"
class
=
"layui-input"
>
div
>
<
div
class
=
"layui-form-mid layui-word-aux"
>
<
span
class
=
"x-red"
style
=
"color: red"
>*必填
span
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>信息图片
label
>
<
div
class
=
"layui-input-block"
>
<
button
type
=
"button"
class
=
"layui-btn"
id
=
"test2"
>图片上传
button
>
<
div
class
=
"layui-upload-list"
id
=
"thumbnail"
>
div
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
div
class
=
"layui-input-block"
>
<
button
class
=
"layui-btn"
lay-submit
lay-filter
=
"test"
>保存
button
>
div
>
div
>
div
>
div
>
div
>
div
>
div
>
div
>
<
div
class
=
"layui-fluid"
id
=
"order"
style
=
"display: none"
>
<
div
class
=
"layui-row layui-col-space15"
>
<
div
class
=
"layui-col-md12"
>
<
div
class
=
"layui-card"
>
<
div
class
=
"layui-card-header"
>确认订单
div
>
<
div
class
=
"layui-card-body"
pad15>
<
div
class
=
"layui-form"
lay-filter
=
"text"
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>信息名
label
>
<
div
class
=
"layui-input-block"
>
<
input
type
=
"text"
name
=
"provenance_url"
id
=
"url"
placeholder
=
"信息名"
value
=
""
class
=
"layui-input"
disabled
=
"true"
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>信息描述
label
>
<
div
class
=
"layui-input-inline"
>
<
textarea
name
=
"provenance_desc"
id
=
"desc"
lay-verify
=
"required"
placeholder
=
"请仔细填写描述"
class
=
"layui-textarea"
disabled
=
"true"
>
textarea
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>联系方式
label
>
<
div
class
=
"layui-input-inline"
>
<
input
type
=
"text"
name
=
"user_qq"
id
=
"qq"
lay-verify
=
"required"
placeholder
=
"要求填写QQ联系方式"
disabled
=
"true"
class
=
"layui-input"
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>金额
label
>
<
div
class
=
"layui-input-inline"
>
<
input
type
=
"text"
name
=
"price"
id
=
"price"
lay-verify
=
"required"
placeholder
=
"要求填写QQ联系方式"
value
=
""
disabled
=
"true"
class
=
"layui-input"
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
label
class
=
"layui-form-label"
>支付方式
label
>
<
div
class
=
"layui-input-block"
>
<
input
type
=
"radio"
name
=
"type"
value
=
"1"
title
=
"支付宝"
>
<
img
src
=
"static/index/images/Alipay.png"
>
<
input
type
=
"radio"
name
=
"type"
value
=
"2"
title
=
"微信"
>
<
img
src
=
"static/index/images/WeChat.png"
>
div
>
div
>
<
div
class
=
"layui-form-item"
>
<
div
class
=
"layui-input-block"
>
<
button
class
=
"layui-btn"
lay-submit
lay-filter
=
"pay"
>确定支付
button
>
div
>
div
>
div
>
div
>
div
>
div
>
div
>
div
>
<
script
src
=
"__INDEX__/layui/layui.js"
>
script
>
<
script
>
//注意:导航 依赖 element 模块,否则无法进行功能性操作
layui.use('element', function () {
var element = layui.element;
//…
});
layui.use('carousel', function () {
var carousel = layui.carousel;
//建造实例
carousel.render({
elem: '#test1'
, width: '100%' //设置容器宽度
, arrow: 'always' //始终显示箭头
//,anim: 'updown' //切换动画方式
});
});
layui.use(['form', 'upload'], function () {
var form = layui.form,
$ = layui.jquery,
upload = layui.upload;
upload.render({
elem: '#test2',
url: "index.php/index/upload",
multiple: true,
before: function (obj) {
//预读本地文件示例,不支持ie8
obj.preview(function (index, file, result) {
$('#thumbnail').append('<
img
src
=
"' + result + '"
alt
=
"' + file.name + '"
class
=
"layui-upload-img"
>')
});
},
done: function (res) {
//上传完毕
$('#thumbnail').append('<
input
type
=
"hidden"
name
=
"file[]"
id
=
"file"
value
=
"' + res.data + '"
/>')
}
});
form.on('submit(test)', function (data) {
console.log(data.field);
$.ajax({
url: "index.php/index/order",
dataType: "json",
data: data.field,
success: function (res) {
console.log(res)
if (res.code >= 1) {
layer.alert(res.msg, {icon: 5})
} else {
$('#url').val(res.data['provenance_url'])
$('#desc').val(res.data['provenance_desc'])
$('#qq').val(res.data['user_qq'])
$('#price').val(res.price)
layer.open({
type: 1 //此处以iframe举例
, title: '提交BUG'
, area: ['700px', '650px']
, maxmin: true
, btnAlign: 'c'
, shade: false
, content: $('#order')
});
}
}
});
})
form.on('submit(pay)',function (data) {
$.post('codepay/codepay.php',{
'type':data.field.type,
'price':data.field.price,
'user':'admin'
},function (data) {
console.log(data)
layer.open({
type: 1 //此处以iframe举例
, title: '提交BUG'
, area: ['700px', '650px']
, maxmin: true
, btnAlign: 'c'
, shade: false
, content: data
});
})
})
})
script
>
/**
* Created by PhpStorm.
* User: win7
* Date: 2019/1/2
* Time: 9:32
*/
namespace
app\index\controller;
use
think\Controller;
use
\think\facade\Request;
use
think\facade\Session;
class
Index
extends
Controller
{
public
function
index()
{
return
$this
->fetch();
}
public
function
order()
{
$data
= Request::param();
if
(
empty
(
$data
[
'file'
])){
return
json([
'code'
=>1,
'msg'
=>
'请添加车牌相关图片'
]);
}
$pic_count
=
count
(
$data
[
'file'
]);
if
(
$pic_count
> 1){
return
json([
'code'
=>0,
'msg'
=>
'订单生成'
,
'price'
=>15,
'data'
=>
$data
]);
}
else
{
return
json([
'code'
=>0,
'msg'
=>
'订单生成'
,
'price'
=>0.1,
'data'
=>
$data
]);
}
}
public
function
upload()
{
// 获取到上传的图片
$file
= request()->file(
'file'
);
// 移动到框架应用根目录/uploads/ 目录下
if
(
$info
=
$file
->validate([
'ext'
=>
'jpg,jpeg,png,gif'
])->move(
'upload'
)) {
//客户端要求返回的必须是JSON格式数据,默认没有加上上传目录,需要手工添加一下
return
json([1,
'上传成功!'
,
'data'
=> [
'/upload/'
.
$info
->getSaveName()]]);
}
else
{
//处理出错信息,其实客户端也会处理的,可省略
return
$file
->getError();
}
}
}
因为我们只写了一个小案例,这里的订单内容也就很简单,当上传的图片大于1时则需要付15元,小于1时,只需付0.1元。
本篇文章就是关于PHP实现个人支付宝支付开发的相关介绍,其实也非常简单易懂,在后续的文章中,我们再继续为大家讲解相关知识内容。
-END-
声明:本文选自「 php中文网 」,搜索「 phpcnnew 」即可关注!