支付宝wap支付链接:https://fanqie.tongfawuye.com/alipay/alipay_detail
支付宝PC支付链接:https://fanqie.tongfawuye.com/alipay/aliyun_pay?
支付宝沙箱APP安卓版下载二维码及测试账号:
资源下载路径:https://download.csdn.net/download/chenxiaomumu/21699071
采用PHPCI框架,实现手机网页及PC端网页调用支付支付。
application/controllers/Alipay.php
<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once(APPPATH."/libraries/alipay/AliPayConfig.php");
require_once(APPPATH."/libraries/alipay/AlipayTradeService.php");
require_once(APPPATH."/libraries/alipay/AlipayTradeWapPayContentBuilder.php");
require_once(APPPATH."/libraries/alipay/AlipayTradePagePayContentBuilder.php");
/**
* Class Alipay
*/
class Alipay extends MY_Controller {
function __construct()
{
parent::__construct(false);
$this->load->model('Pay_log_model');
}
/**
* 手机端网页支付
*/
public function alipay_detail(){
//$amount = $this->input->get('amount');
//$phone = $this->input->get('phone');
$amount = (float)0.01;
$phone = '13427790490';
if($amount < 0.01){
$data['error'] = '请输入有效的支付金额';
$this->load->view('alipay_detail', $data);
return;
}
//插入支付日志
$pay_log = array();
$pay_log['pay_user'] = $phone;
$pay_log['name'] = '支付宝wap订单支付';
$pay_log['amount'] = $amount;
$pay_log['pay_mode'] = 2;
$pay_log['add_time'] = time();
$pay_log['add_time_ymd'] = date("Y-m-d H:i:s");
$this->Pay_log_model->save($pay_log);
$log_id = $pay_log['log_id'];
try {
$data['alipay_data'] = $this->get_alipay_request_data($pay_log['name'], $log_id, $amount);
} catch (Exception $e) {
$data['alipay_data'] = '小伙子,出错了';
}
$this->load->view('alipay_detail',$data);
}
/**
* 支付宝移动端下单信息
* @param $name
* @param $log_id
* @param $amount
* @return false|mixed|SimpleXMLElement|string|提交表单HTML文本
* @throws Exception
*/
private function get_alipay_request_data($name,$log_id,$amount){
$payRequestBuilder = new AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($log_id);//订单描述(日志ID)
$payRequestBuilder->setSubject($name);//订单标题。注意:不可使用特殊字符,如 /,=,& 等。
$payRequestBuilder->setOutTradeNo(AliPayConfig::app_id.date("YmdHis").$log_id);//商户订单号,商户网站订单系统中唯一订单号,必填
$payRequestBuilder->setTotalAmount($amount);//订单总金额。单位为元,精确到小数点后两位,取值范围:[0.01,100000000]
$payRequestBuilder->setTimeExpress('5m');//(推荐使用,相对时间) 支付超时时间,5m 5分钟
$payResponse = new AlipayTradeService();
return $payResponse->wapPay($payRequestBuilder,AliPayConfig::return_url,AliPayConfig::notify_url);
}
/**
* PC端扫码支付
*/
public function aliyun_pay(){
//$amount = $this->input->get('amount');
//$phone = $this->input->get('phone');
$amount = (float)0.01;
$phone = '13427790490';
if($amount < 0.01){
$data['error'] = '请输入有效的支付金额';
$this->load->view('aliyun_pay', $data);
return;
}
//插入支付日志
$pay_log = array();
$pay_log['pay_user'] = $phone;
$pay_log['name'] = '支付宝PC订单支付';
$pay_log['amount'] = $amount;
$pay_log['pay_mode'] = 2;
$pay_log['add_time'] = time();
$pay_log['add_time_ymd'] = date("Y-m-d H:i:s");
$this->Pay_log_model->save($pay_log);
$log_id = $pay_log['log_id'];
try {
$data['alipay_data'] = $this->get_pc_alipay_request_data($pay_log['name'], $log_id, $amount);
} catch (Exception $e) {
$data['alipay_data'] = '小伙子,出错了';
}
$this->load->view('aliyun_pay',$data);
}
/**
* 支付宝PC端下单信息
* @param $name
* @param $log_id
* @param $amount
* @return false|mixed|SimpleXMLElement|string|提交表单HTML文本
* @throws Exception
*/
private function get_pc_alipay_request_data($name,$log_id,$amount){
$payRequestBuilder = new AlipayTradePagePayContentBuilder();
$payRequestBuilder->setBody($log_id);//订单描述(日志ID)
$payRequestBuilder->setSubject($name);//订单标题。注意:不可使用特殊字符,如 /,=,& 等。
$payRequestBuilder->setOutTradeNo(AliPayConfig::app_id.date("YmdHis").$log_id);//商户订单号,商户网站订单系统中唯一订单号,必填
$payRequestBuilder->setTotalAmount($amount);//订单总金额。单位为元,精确到小数点后两位,取值范围:[0.01,100000000]
$payRequestBuilder->setTimeExpress('5m');//(推荐使用,相对时间) 支付超时时间,5m 5分钟
$payResponse = new AlipayTradeService();
return $payResponse->pagePay($payRequestBuilder,AliPayConfig::return_url,AliPayConfig::notify_url);
}
/**
* 支付宝回调异步通知
*/
public function alipay_recharge_notify()
{
//log_message('error', "POST:" . print_r($_POST, true));
//计算得出通知验证结果
$alipaySevice = new AlipayTradeService();
$result = $alipaySevice->check($_POST);
if($result) {//验证成功
//逻辑处理
$log_id = $_POST['body'];
$out_trade_no = $_POST['out_trade_no'];
$total_amount = $_POST['total_amount'];
//交易状态
$trade_status = $_POST['trade_status'];
//logResult("mobile callback:res=".$out_trade_no.";login_id=".$_POST['subject'].";total_frme=".$_POST['total_fee'].";trade_status=".$trade_status);
//只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,支付宝才会认定为买家付款成功
if($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS')
{
//支付成功的后续操作
$pay_log = $this->Pay_log_model->get_info($log_id);
if(!empty($pay_log))
{
if($pay_log['amount'] == $total_amount && $pay_log['pay_status'] == 0)
{
//修改支付状态
$log_data = array(
'pay_status'=>1,
'trade_no'=>$out_trade_no,
'pay_user'=>$_POST['buyer_id'],
'pay_trans_id'=>$_POST['trade_no'],
'pay_code'=>$_POST['trade_status']
);
$this->Pay_log_model->save($log_data, $log_id);
//给支付宝返回成功
echo "success";exit;
}
}
}
}
//验证失败
echo "fail";
}
}
application/models/Pay_log_model.php(和微信支付使用了同一个model)
<?php
/**
* Class Pay_log_model
*/
class Pay_log_model extends MY_Model {
public $_table = 'pay_log';
public $primary_key = 'log_id';
function get_info($id, $select=null){
if(!empty($select)){
$this->db->select($select);
}
return $this->get($id);
}
function save(&$data, $id = 0) {
$data['last_time'] = time();
if ($id > 0) {
$this->db->set ( $data );
$this->db->where ( $this->primary_key, $id );
$this->db->update ( $this->_table );
$err = $this->db->error();
$data['db_code'] = $err['code'];
$data [$this->primary_key] = $id;
return $this->db->affected_rows ();
}
$data['add_time'] = time();
$this->db->insert ( $this->_table, $data );
$err = $this->db->error();
$data['db_code'] = $err['code'];
$data [$this->primary_key] = $this->db->insert_id ();
return $this->db->affected_rows ();
}
}
application/views/alipay_detail.php
<!DOCTYPE html>
<html>
<head>
<title>支付宝手机网站支付接口</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
<h1>支付宝手机网站支付接口快速通道(接口名:alipay.trade.wap.pay)</h1>
</header>
<div id="main">
<?php echo $alipay_data?>
<div id="foot">
<ul class="foot-ul">
<li>
支付宝版权所有 2015-2018 ALIPAY.COM
</li>
</ul>
</div>
</div>
</body>
</html>
application/views/aliyun_pay.php
<!DOCTYPE html>
<html>
<head>
<title>支付宝PC网站支付接口</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
<h1>支付宝PC网站支付接口快速通道(接口名:alipay.trade.page.pay)</h1>
</header>
<div id="main">
<?php echo $alipay_data?>
<div id="foot">
<ul class="foot-ul">
<li>
支付宝版权所有 2015-2018 ALIPAY.COM
</li>
</ul>
</div>
</div>
</body>
</html>
application/libraries/alipay/AliPayConfig.php
<?php
/**
* 配置账号信息
*/
class AliPayConfig
{
/**
* 应用ID,您的APPID
*/
const app_id = '';
/**
* 异步通知地址
*/
const notify_url = '';
/**
* 同步跳转
*/
const return_url = '';
/**
* 编码格式
*/
const charset = 'UTF-8';
/**
* 签名方式
*/
const sign_type = 'RSA2';
/**
* 支付宝网关
*/
const gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
//const gatewayUrl = 'https://openapi.alipay.com/gateway.do';
/**
* 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
*/
const alipay_public_key = '';
/**
* 商户私钥,您的原始格式RSA私钥
*/
const merchant_private_key = '';
}
?支付宝开放平台-手机网站支付中下载对应的demo只需要以下这几个文件即可:
找到支付宝开放平台沙箱环境
备注:
PHP微信支付对接文章地址:
https://blog.csdn.net/chenxiaomumu/article/details/119937721
|