Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
netcccyun committed Mar 30, 2023
1 parent 8bf61da commit f8ebe55
Show file tree
Hide file tree
Showing 27 changed files with 2,725 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.vscode
/build
/vendor
/composer.lock
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# alipay-sdk-php
支付宝开放平台第三方 PHP SDK,基于官方最新版本,支持公钥和公钥证书2种模式
# Alipay SDK for PHP
支付宝开放平台第三方 PHP SDK,基于官方最新版本,支持公钥和公钥证书2种模式。

### 功能特点

- 根据支付宝开放平台最新API开发,相比官方SDK,功能更完善,代码更简洁
- 支持Composer安装,无需加载多余组件,可应用于任何平台或框架
- 符合`PSR`标准,你可以各种方便的与你的框架集成
- 基本完善的PHPDoc,可以随心所欲添加本项目中没有的API接口

### 环境要求

PHP >= 7.1

### 使用方法

1. Composer 安装。

```bash
composer require cccyun/alipay-sdk
```

2. 创建配置文件 [`config.php`](./blob/main/examples/config.php),填写配置信息。

3. 引入配置文件,构造请求参数,调用AlipayTradeService中的方法发起请求,参考 [`examples/qrpay.php`](./blob/main/examples/qrpay.php)

4. 更多实例,请移步 [`examples`](examples/) 目录。

5. AlipayService实现类功能说明

| 类名 | 说明 |
| --------------------- | -------------------------------------------------------- |
| AlipayTradeService | 支付宝交易功能,基本上所有支付产品都用这个 |
| AlipayOauthService | 支付宝快捷登录功能,用于JS支付快捷登录以及第三方应用授权 |
| AlipaySettleService | 支付宝分账功能 |
| AlipayTransferService | 支付宝转账功能 |
| AlipayComplainService | 支付宝交易投诉处理 |
| AlipayCertifyService | 支付宝身份认证 |
| AlipayCertdocService | 支付宝实名证件信息比对验证 |
| AlipayBillService | 支付宝账单功能 |

6. 要对接的API在AlipayService实现类中没有,可根据支付宝官方的文档,使用AlipayService类中的aopExecute方法直接调用接口,参考 [`examples/other.php`](./blob/main/examples/other.php)



26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "cccyun/alipay-sdk",
"description": "支付宝开放平台第三方 PHP SDK,基于官方最新版本,支持公钥和公钥证书2种模式。",
"type": "library",
"keywords": [
"alipay",
"支付宝"
],
"license": "MIT",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.1"
},
"authors": [
{
"name": "caihong",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Alipay\\": "src/"
}
}
}
30 changes: 30 additions & 0 deletions examples/apppay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* 支付宝APP支付示例
*/

require __DIR__.'/../vendor/autoload.php';
@header('Content-Type: text/html; charset=UTF-8');
$hostInfo = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'];

//引入配置文件
$alipay_config = require('config.php');

//异步回调地址
$alipay_config['notify_url'] = $hostInfo.dirname($_SERVER['SCRIPT_NAME']).'/notify.php';

//构造业务参数bizContent
$bizContent = [
'out_trade_no' => date("YmdHis").rand(111,999), //商户订单号
'total_amount' => '0.15', //订单金额,单位为元
'subject' => 'sample subject', //商品的标题
];

//发起支付请求
try{
$aop = new \Alipay\AlipayTradeService($alipay_config);
$result = $aop->appPay($bizContent);
echo $result; //SDK请求串
}catch(Exception $e){
echo '支付宝下单失败!'.$e->getMessage();
}
76 changes: 76 additions & 0 deletions examples/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* 支付宝应用信息配置文件
* 参考文档:https://opendocs.alipay.com/common/02kipl
*/
$alipay_config = [

//=======【应用基本信息设置】=======
/**
* 支付宝开放平台应用ID
*/
'app_id' => '',

/**
* 支付宝公钥
* (使用公钥证书模式此项可留空)
*/
'alipay_public_key' => '',

/**
* 应用私钥
*/
'app_private_key' => '',

/**
* 服务商模式应用授权token
* 只有服务商模式的子商户需要填写
*/
//'app_auth_token' => '',

/**
* 互联网平台直付通子商户ID
* 只有互联网平台直付通的子商户需要填写
*/
//'smid' => '',


//=======【证书路径设置,使用公钥证书模式需填写】=======
/**
* 应用公钥证书文件路径
* (填写后会使用公钥证书模式,留空使用公钥模式)
*/
'app_cert_path' => '',

/**
* 支付宝公钥证书文件路径
* (填写后会使用公钥证书模式,留空使用公钥模式)
*/
'alipay_cert_path' => '',

/**
* 支付宝根证书文件路径
* (填写后会使用公钥证书模式,留空使用公钥模式)
*/
'root_cert_path' => '',


//=======【其他设置,一般无需修改】=======
/**
* 签名方式,默认为RSA2
*/
'sign_type' => "RSA2",

/**
* 编码格式
*/
'charset' => "UTF-8",

/**
* 支付宝网关
*/
'gateway_url' => "https://openapi.alipay.com/gateway.do",

];
return $alipay_config;
109 changes: 109 additions & 0 deletions examples/jspay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* 支付宝生活号支付(JS支付)示例
* 使用此支付类型前,需要在支付宝开放平台应用里面,配置授权回调域名
*/

require __DIR__.'/../vendor/autoload.php';
@header('Content-Type: text/html; charset=UTF-8');
$hostInfo = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'];

//引入配置文件
$alipay_config = require('config.php');

//支付宝快捷登录并获取openid
$redirect_uri = $hostInfo.$_SERVER['REQUEST_URI'];
try{
$oauth = new \Alipay\AlipayOauthService($alipay_config);
if(isset($_GET['auth_code'])){
$result = $oauth->getToken($_GET['auth_code']);
$openid = $result['user_id'];
}else{
$oauth->oauth($redirect_uri);
}
}catch(Exception $e){
echo '支付宝快捷登录失败!'.$e->getMessage();
exit;
}

//异步回调地址
$alipay_config['notify_url'] = $hostInfo.dirname($_SERVER['SCRIPT_NAME']).'/notify.php';

//构造业务参数bizContent
$bizContent = [
'out_trade_no' => date("YmdHis").rand(111,999), //商户订单号
'total_amount' => '0.15', //订单金额,单位为元
'subject' => 'sample subject', //商品的标题
];

//发起支付请求
try{
$aop = new \Alipay\AlipayTradeService($alipay_config);
$result = $aop->qrPay($bizContent);
$alipay_trade_no = $result['trade_no'];
}catch(Exception $e){
echo '支付宝下单失败!'.$e->getMessage();
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>支付宝支付</title>
<link href="//cdn.staticfile.org/ionic/1.3.2/css/ionic.min.css" rel="stylesheet" />
</head>
<body>
<div class="bar bar-header bar-light" align-title="center">
<h1 class="title">支付宝支付</h1>
</div>
<div class="has-header" style="padding: 5px;position: absolute;width: 100%;">
<div class="text-center" style="color: #a09ee5;">
<i class="icon ion-information-circled" style="font-size: 80px;"></i><br>
<span>正在跳转...</span>
<script>
document.body.addEventListener('touchmove', function (event) {
event.preventDefault();
},{ passive: false });

var tradeNO = '<?php echo $alipay_trade_no?>';

function Alipayready(callback) {
// 如果jsbridge已经注入则直接调用
if (window.AlipayJSBridge) {
callback && callback();
} else {
// 如果没有注入则监听注入的事件
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
function AlipayJsPay() {
Alipayready(function(){
AlipayJSBridge.call("tradePay",{
tradeNO: tradeNO
}, function(result){
var msg = "";
if(result.resultCode == "9000"){
msg = "支付成功";
//跳转到支付成功页面
}else if(result.resultCode == "8000"){
msg = "正在处理中";
}else if(result.resultCode == "4000"){
msg = "订单支付失败";
}else if(result.resultCode == "6002"){
msg = "网络连接出错";
}
if (msg!="") {
alert(msg);
}
});
});
}
window.onload = AlipayJsPay();
</script>
</div>
</div>
</body>
</html>
40 changes: 40 additions & 0 deletions examples/notify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* 支付宝异步回调页面示例
*/

require __DIR__.'/../vendor/autoload.php';

$alipay_config = require('config.php');
$aop = new \Alipay\AlipayTradeService($alipay_config);

if($aop->check($_POST)) {//验证成功
//商户订单号
$out_trade_no = $_POST['out_trade_no'];

//支付宝交易号
$trade_no = $_POST['trade_no'];

//买家支付宝用户ID
$buyer_id = $_POST['buyer_id'];

//交易金额
$total_amount = $_POST['total_amount'];

if($_POST['trade_status'] == 'TRADE_FINISHED') {
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序


//互联网平台直付通-确认结算
//$aop->settle_confirm($trade_no, $total_amount);
}

//验证成功返回
echo 'success';
}else{
//验证失败
echo 'fail';
}
26 changes: 26 additions & 0 deletions examples/other.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* 支付宝调用其他接口示例
* 使用\Alipay\AlipayService中的aopExecute方法调用自定义接口
*/

$alipay_config = require('config.php');
$aop = new \Alipay\AlipayService($alipay_config);

//接口名称
$apiName = 'ant.merchant.expand.indirect.zft.consult';

//请求参数的集合
$bizContent = [
'external_id' => '',
'name' => '',
'alias_name' => ''
];

try{
$result = $aop->aopExecute($apiName, $bizContent);
print_r($result);
}catch(Exception $e){
echo '错误信息:'.$e->getMessage();
}

Loading

0 comments on commit f8ebe55

Please sign in to comment.