PHP实现支付宝即时到账功能

本文实例为大家分享了PHP支付宝即时到账功能的实现代码,供大家参考,具体内容如下

首先需要下载即时到账交易接口,传送门https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1

选择(create_direct_pay_by_user)

然后解压,选择MD5签名版本的文件

此文件夹里有个叫create_direct_pay_by_user-PHP-UTF-8的文件,我们就用这个文件~

接着打开文件后如图

并把文件夹lib里面的四个PHP文件重命名

alipay_core.function.php重命名为:Corefunction.php;

alipay_md5.function.php重命名为:Md5function.php;

alipay_notify.class.php重命名为:Notify.php;

alipay_submit.class.php重命名为:Submit.php;

最后在THINKPHP\Library\Vendor目录下新建文件夹命名为Alipay,把以上四个php文件复制进去如图所示

然后,打开Submit.php文件,把以下代码去掉;

require_once("alipay_core.function.php");
require_once("alipay_md5.function.php");

同样,打开Notify.php文件,把以下两段代码去掉;

require_once("alipay_core.function.php");
require_once("alipay_md5.function.php");

接下来可以写代码了(注意img文件夹里的图片地址需要修改)

HTML代码:(这个代码其实就是index里面的代码,复制如下)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>支付宝即时到账交易接口</title>
</head>

<style>
 html,body {
 width:100%;
 min-width:1200px;
 height:auto;
 padding:0;
 margin:0;
 font-family:"微软雅黑";
 background-color:#242736
 }
 .header {
 width:100%;
 margin:0 auto;
 height:230px;
 background-color:#fff
 }
 .container {
 width:100%;
 min-width:100px;
 height:auto
 }
 .black {
 background-color:#242736
 }
 .blue {
 background-color:#0ae
 }
 .qrcode {
 width:1200px;
 margin:0 auto;
 height:30px;
 background-color:#242736
 }
 .littlecode {
 width:16px;
 height:16px;
 margin-top:6px;
 cursor:pointer;
 float:right
 }
 .showqrs {
 top:30px;
 position:absolute;
 width:100px;
 margin-left:-65px;
 height:160px;
 display:none
 }
 .shtoparrow {
 width:0;
 height:0;
 margin-left:65px;
 border-left:8px solid transparent;
 border-right:8px solid transparent;
 border-bottom:8px solid #e7e8eb;
 margin-bottom:0;
 font-size:0;
 line-height:0
 }
 .guanzhuqr {
 text-align:center;
 background-color:#e7e8eb;
 border:1px solid #e7e8eb
 }
 .guanzhuqr img {
 margin-top:10px;
 width:80px
 }
 .shmsg {
 margin-left:10px;
 width:80px;
 height:16px;
 line-height:16px;
 font-size:12px;
 color:#242323;
 text-align:center
 }
 .nav {
 width:1200px;
 margin:0 auto;
 height:70px;
 }
 .open,.logo {
 display:block;
 float:left;
 height:40px;
 width:85px;
 margin-top:20px
 }
 .divier {
 display:block;
 float:left;
 margin-left:20px;
 margin-right:20px;
 margin-top:23px;
 width:1px;
 height:24px;
 background-color:#d3d3d3
 }
 .open {
 line-height:30px;
 font-size:20px;
 text-decoration:none;
 color:#1a1a1a
 }
 .navbar {
 float:right;
 width:200px;
 height:40px;
 margin-top:15px;
 list-style:none
 }
 .navbar li {
 float:left;
 width:100px;
 height:40px
 }
 .navbar li a {
 display:inline-block;
 width:100px;
 height:40px;
 line-height:40px;
 font-size:16px;
 color:#1a1a1a;
 text-decoration:none;
 text-align:center
 }
 .navbar li a:hover {
 color:#00AAEE
 }
 .title {
 width:1200px;
 margin:0 auto;
 height:80px;
 line-height:80px;
 font-size:20px;
 color:#FFF
 }
 .content {
 width:100%;
 min-width:1200px;
 height:660px;
 background-color:#fff;
 }
 .alipayform {
 width:800px;
 margin:0 auto;
 height:600px;
 border:1px solid #0ae
 }
 .element {
 width:600px;
 height:80px;
 margin-left:100px;
 font-size:20px
 }
 .etitle,.einput {
 float:left;
 height:26px
 }
 .etitle {
 width:150px;
 line-height:26px;
 text-align:right
 }
 .einput {
 width:200px;
 margin-left:20px
 }
 .einput input {
 width:398px;
 height:24px;
 border:1px solid #0ae;
 font-size:16px
 }
 .mark {
 margin-top: 10px;
 width:500px;
 height:30px;
 margin-left:80px;
 line-height:30px;
 font-size:12px;
 color:#999
 }
 .legend {
 margin-left:100px;
 font-size:24px
 }
 .alisubmit {
 width:400px;
 height:40px;
 border:0;
 background-color:#0ae;
 font-size:16px;
 color:#FFF;
 cursor:pointer;
 margin-left:170px
 }
 .footer {
 width:100%;
 height:120px;
 background-color:#242735
 }
 .footer-sub a,span {
 color:#808080;
 font-size:12px;
 text-decoration:none
 }
 .footer-sub a:hover {
 color:#00aeee
 }
 .footer-sub span {
 margin:0 3px
 }
 .footer-sub {
 padding-top:40px;
 height:20px;
 width:600px;
 margin:0 auto;
 text-align:center
 }
</style>

<body>

<div class="header">
 <div class="container black">
 <div class="qrcode">
 <div class="littlecode">
 <img width="16px" src="img/little_qrcode.jpg" id="licode">
 <div class="showqrs" id="showqrs">
  <div class="shtoparrow"></div>
  <div class="guanzhuqr">
  <img src="img/guanzhu_qrcode.png" width="80">
  <div class="shmsg" style="margin-top:5px;">
  请扫码关注
  </div>
  <div class="shmsg" style="margin-bottom:5px;">
  接收重要信息
  </div>
  </div>
 </div>
 </div>
 </div>
 </div>
 <div class="container">
 <div class="nav">
 <a href="https://www.alipay.com/" class="logo"><img src="img/alipay_logo.png" height="30px"></a>
 <span class="divier"></span>
 <a href="http://open.alipay.com/platform/home.htm" class="open" target="_blank">开放平台</a>
 <ul class="navbar">
 <li><a href="https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在线文档</a></li>
 <li><a href="https://cschannel.alipay.com/portal.htm?sourceId=213" target="_blank">技术支持</a></li>
 </ul>
 </div>
 </div>
 <div class="container blue">
 <div class="title">支付宝即时到账(create_direct_pay_by_user)</div>
 </div>
</div>
<div class="content">
 <form action="{:U('home/Pay/alipayapi')}" class="alipayform" method="post" target="_blank">
 <div class="element" style="margin-top:60px;">
 <div class="legend">支付宝即时到账交易接口快速通道 </div>
 </div>
 <div class="element">
 <div class="etitle">商户订单号:</div>
 <div class="einput"><input type="text" name="WIDout_trade_no" id="out_trade_no"></div>
 <br>
 <div class="mark">注意:商户订单号(out_trade_no).必填(建议是英文字母和数字,不能含有特殊字符)</div>
 </div>

 <div class="element">
 <div class="etitle">商品名称:</div>
 <div class="einput"><input type="text" name="WIDsubject" value="test商品123"></div>
 <br>
 <div class="mark">注意:产品名称(subject),必填(建议中文,英文,数字,不能含有特殊字符)</div>
 </div>
 <div class="element">
 <div class="etitle">付款金额:</div>
 <div class="einput"><input type="text" name="WIDtotal_fee" value="0.01"></div>
 <br>
 <div class="mark">注意:付款金额(total_fee),必填(格式如:1.00,请精确到分)</div>
 </div>
 <div class="element">
 <div class="etitle">商品描述:</div>
 <div class="einput"><input type="text" name="WIDbody" value="即时到账测试"></div>
 <br>
 <div class="mark">注意:商品描述(body),选填(建议中文,英文,数字,不能含有特殊字符)</div>
 </div>
 <div class="element">
 <input type="submit" class="alisubmit" value ="确认支付">
 </div>
 </form>
</div>
<div class="footer">
 <p class="footer-sub">
 <a href="http://ab.alipay.com/i/index.htm" target="_blank">关于支付宝</a><span>|</span>
 <a href="https://e.alipay.com/index.htm" target="_blank">商家中心</a><span>|</span>
 <a href="https://job.alibaba.com/zhaopin/index.htm" target="_blank">诚征英才</a><span>|</span>
 <a href="http://ab.alipay.com/i/lianxi.htm" target="_blank">联系我们</a><span>|</span>
 <a href="#" id="international" target="_blank">International Business</a><span>|</span>
 <a href="http://ab.alipay.com/i/jieshao.htm#en" target="_blank">About Alipay</a>
 <br>
 <span>支付宝版权所有</span>
 <span class="footer-date">2004-2016</span>
 <span><a href="http://fun.alipay.com/certificate/jyxkz.htm" target="_blank">ICP证:沪B2-20150087</a></span>
 </p>

</div>
</body>
<script>
 var even = document.getElementById("licode");
 var showqrs = document.getElementById("showqrs");
 even.onmouseover = function(){
 showqrs.style.display = "block";
 }
 even.onmouseleave = function(){
 showqrs.style.display = "none";
 }

 var out_trade_no = document.getElementById("out_trade_no");

 //设定时间格式化函数
 Date.prototype.format = function (format) {
 var args = {
 "M+": this.getMonth() + 1,
 "d+": this.getDate(),
 "h+": this.getHours(),
 "m+": this.getMinutes(),
 "s+": this.getSeconds(),
 };
 if (/(y+)/.test(format))
 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
 for (var i in args) {
 var n = args[i];
 if (new RegExp("(" + i + ")").test(format))
 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
 }
 return format;
 };

 out_trade_no.value = 'test'+ new Date().format("yyyyMMddhhmmss");
</script>
</html>

PHP控制代码(其实就是把alipay.config和alipayapi代码集合在一起)

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/7 0007
 * Time: 下午 3:30
 */

namespace Home\Controller;
use Think\Controller;
use Vendor\Alidayu;

class PayController extends Controller
{
 public function alipayapi() {
 Vendor('Alipay.Corefunction');
 Vendor('Alipay.Md5function');
 Vendor('Alipay.Notify');
 Vendor('Alipay.Submit');

/***************************请求参数******************************************/
 //商户订单号,商户网站订单系统中唯一订单号,必填
 $out_trade_no = $_POST['WIDout_trade_no'];

 //订单名称,必填
 $subject = $_POST['WIDsubject'];

 //付款金额,必填
 $total_fee = $_POST['WIDtotal_fee'];

 //商品描述,可空
 $body = $_POST['WIDbody'];

/*******************************构造配置来自alipay.config******************************************/
 //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
 $alipay_config['partner'] = '*******需要申请ID*****';//商户ID

//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
 $alipay_config['seller_id'] = $alipay_config['partner'];

// MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
 $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY

// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类2088421713316394自定义参数,必须外网可以正常访问
 $alipay_config['notify_url'] = "http://商户网址/create_direct_pay_by_user-PHP-UTF-8/notify_url.php";

// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
 $alipay_config['return_url'] = "http://商户网址/create_direct_pay_by_user-PHP-UTF-8/return_url.php";

//签名方式
 $alipay_config['sign_type'] = strtoupper('MD5');

//字符编码格式 目前支持 gbk 或 utf-8
 $alipay_config['input_charset']= strtolower('utf-8');

//ca证书路径地址,用于curl中ssl校验
//请保证cacert.pem文件在当前文件夹目录中
 $alipay_config['cacert'] = getcwd().'\\cacert.pem';

//访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
 $alipay_config['transport'] = 'http';

// 支付类型 ,无需修改
 $alipay_config['payment_type'] = "1";

// 产品类型,无需修改
 $alipay_config['service'] = "create_direct_pay_by_user";

//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

//↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

// 防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数
 $alipay_config['anti_phishing_key'] = "";

// 客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1
 $alipay_config['exter_invoke_ip'] = "";

//↑↑↑↑↑↑↑↑↑↑请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

/****************************************************************************/
//构造要请求的参数数组,无需改动
 $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'],

 "anti_phishing_key"=>$alipay_config['anti_phishing_key'],
 "exter_invoke_ip"=>$alipay_config['exter_invoke_ip'],
 "out_trade_no" => $out_trade_no,
 "subject" => $subject,
 "total_fee" => $total_fee,
 "body" => $body,
 "_input_charset" => trim(strtolower($alipay_config['input_charset']))
 //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1
 //如"参数名"=>"参数值"

 );

//建立请求
 $alipaySubmit = new \AlipaySubmit($alipay_config);
 $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
 echo $html_text;

 }

}

然后结果如图

确认支付后可能会出现支付宝错误代码 ILLEGAL_PARTNER

那是因为没有配置

$alipay_config['partner'] = '*******需要申请ID*****';//商户ID
//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
$alipay_config['seller_id'] = $alipay_config['partner'];
// MD5密钥,安全检验码,由数字和字母组成的32位字符串,
//查看地址:https://b.alipay.com/order/pidAndKey.htm

$alipay_config['key'] = '******需要申请KEY*********'; //商户KEY

ID和KEY需要公司申请,个人是没法申请的。
谢谢观看~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • PHP后台微信支付和支付宝支付开发

    关于支付的流程之类的就不做解释,大家可以自行搜索! 微信支付 项目前提:本人用的是tp框架,PHP语言 下载到微信平台提供的微信支付接口文件,放在了tp第三方类库vendor,命名为WxpayAPI WxpayAPI/lib/WxPay.Api.php 接口访问类; WxpayAPI/lib/WxPay.Config.php 配置账号信息; WxpayAPI/lib/WxPay.Data.php 数据对象基础类; WxpayAPI/lib/WxPay.Exception.php 微信支付API异

  • ThinkPHP实现支付宝接口功能实例

    本文实例讲述了ThinkPHP实现支付宝接口功能的方法.分享给大家供大家参考.具体分析如下: 最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能,这里我用的是即时到帐的接口,具体实现的步骤如下: 一.下载支付宝接口包 下载地址:https://b.alipay.com/order/productDetail.htm?productId=2012111200373124&tabId=4#ps-tabinfo-hash 具体如何下载,我就不在罗嗦了-- 二.重新整理接口包文件,

  • PHP微信支付开发实例

    PHP微信支付开发过程,分享给大家,供大家参考,具体内容如下 1.开发环境 Thinkphp 3.2.3 微信:服务号,已认证 开发域名:http://test.paywechat.com (自定义的域名,外网不可访问) 2.需要相关文件和权限 微信支付需申请开通 微信公众平台开发者文档:http://mp.weixin.qq.com/wiki/home/index.html 微信支付开发者文档:https://pay.weixin.qq.com/wiki/doc/api/index.html

  • 微信支付PHP SDK之微信公众号支付代码详解

    这里假设你已经申请完微信支付 1. 微信后台配置  如图 我们先进行测试,所以先把测试授权目录和 测试白名单添加上.测试授权目录是你要发起微信请求的哪个文件所在的目录. 例如jsapi 发起请求一般是jsapi.php所在目录 为测试目录,测试白名单即开发人员的微信号. 正式的支付授权目录不能和测试的一样否则会报错.不填写或者填错授权目录以及测试白名单都会报错. 报错样例: NaNsystem:access_denied 不在测试白名单 2. 配置 lib/WxPay.Config.php文件

  • php支付宝接口用法分析

    本文实例讲述了php支付宝接口用法.分享给大家供大家参考.具体分析如下: 现在流行的网站支持平台,支付宝当仁不让的老大了,现在我们就来告诉你如何使用支付宝api来做第三方支付,把支付宝放到自己网站来,代码如下: alipay_config.php配置程序如下: 复制代码 代码如下: <?php */ //alipay_config.php 配置程序 $interfaceurl = "https://www.alipay.com/payto:"; $sitename  = &quo

  • PHP 微信支付类 demo

    一切尽在代码中,代码附有注释,欢迎大家参考. <?php class WxpayService { protected $mchid; protected $appid; protected $key; public function __construct($mchid, $appid, $key) { $this->mchid = $mchid; // 微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送 $this->appid = $appid; //公众号AP

  • php进行支付宝开发中return_url和notify_url的区别分析

    本文实例分析了php进行支付宝开发中return_url和notify_url的区别.分享给大家供大家参考.具体分析如下: 在支付宝处理业务中return_url,notify_url是返回些什么状态呢,我们要根据它来做一些处理就必须了解return_url,notify_url的区别,下面我就来给大家介绍介绍. 问题描述: 我在处理支付宝业务中出现过这样的问题,付费完成后,在支付宝跳转到商家指定页面时,订单状态已经更新,通过调试发现是支付宝先通知notify_url,完成了订单状态. 支付宝r

  • 支付宝 接口开发帮助(asp,php,asp.net,jsp)

    推荐两文:1.支付宝购买流程 2.支付宝卖家流程 支付宝接口提供最新的ASP.ASP.NET.PHP.JSP等目前网络上最流行的源码包文件,程序员可以下载后按本文介绍更改参数和布置即可!而本文拿ASP.NET(C#)详细介绍. ------------------------------------------------------------------------------------- 摘自:支付宝官方 点击下载 实物商品交易服务集成技术文档和服务介绍 按照文档中的提示进行集成操作.

  • php支付宝在线支付接口开发教程

    1.什么是第三方支付 所谓第三方支付,就是一些和各大银行签约.并具备一定实力和信誉保障的第三方独立机构提供的交易支持平台.在通过第三方支付平台的交易中,买方选购商品后,使用第三方平台提供的账户进行货款支付,由第三方通知卖家货款到达. 目前提供第三方支付的机构很多,常见的有支付宝.财付通.快钱.网银在线.易宝支付.云网等各大支付平台.网站如果需要实现第三方支付首先应该向第三方支付平台申请一个账号并签署协议,协议生效后第三方支付平台将为其开通在线支付功能,通过程序将接口集成到网站中. 为什么要使用第

  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布.必须到你通过程序测试提交订单.发货通知等数据到微信的系统中,才能申请发布. 然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到二级三级目录下去,这对于使用MVC框架来说,是个小问题. 使用MVC,在开发环境,url往往是native url格式,不能保证一定是带有路径/的形式 所以,比较可靠的方式,是创建实体的子文件夹,这样的化,在系统整体url方面,需要进行一些改造. 另外,最后一步,js调用时,对于

随机推荐