PHP 接入支付宝证书方式签名以及验签

支付宝在 2019.10.25 日左右更新了新的 PHP SDK (v4.1.0)。 之前的 PHP SDK(v3.4.2) 仅支持公钥方式加签。这次更新之后 PHP SDK 也可以使用证书进行签名和验签了。

支付宝官方 SDK 下载地址

下载解压 并将文件夹中的 aop 文件夹 直接复制到项目中你想要放到文件夹里,例如 extend

文件夹 (为了测试 crt 证书文件也已经放到同目录下 cert 目录中了)



先修正 SDK 中的1个小错误:AopCertClient.php 中

478 行左右 有一个检查是否为空的方法



修改为

	if (method_exists($request, 'getApiVersion')){
$iv = $request->getApiVersion();
}else{
$iv = $this->apiVersion;
}

引入文件

	require_once EXTEND_PATH.'aop/AopCertClient.php';
require_once EXTEND_PATH.'aop/AopCertification.php';
require_once EXTEND_PATH.'aop/request/AlipayTradeQueryRequest.php';
require_once EXTEND_PATH.'aop/request/AlipayTradeWapPayRequest.php';
require_once EXTEND_PATH.'aop/request/AlipayOpenOperationOpenbizmockBizQueryRequest.php';

从 aop/test/ 目录下 找下 AopCertClientTest.php (因为是使用证书进行签名,如果是使用公钥进行签名,就应该参考 AopClientTest.php 文件,不做赘述)直接复制相关代码进行修改比如 我复制的就是

	//1、execute 使用
$aop = new AopCertClient ();
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">gatewayUrl</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'https://openapi.alipay.com/gateway.do'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">appId</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'你的appid'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">rsaPrivateKey</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'你的应用私钥'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">alipayrsaPublicKey</span> <span class="token operator">=</span> <span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getPublicKey</span><span class="token punctuation">(</span><span class="token variable">$alipayCertPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//调用getPublicKey从支付宝公钥证书中提取公钥</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">apiVersion</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'1.0'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">signType</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'RSA2'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">postCharset</span><span class="token operator">=</span><span class="token single-quoted-string string">'utf-8'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">format</span><span class="token operator">=</span><span class="token single-quoted-string string">'json'</span><span class="token punctuation">;</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">isCheckAlipayPublicCert</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token comment">//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">appCertSN</span> <span class="token operator">=</span> <span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getCertSN</span><span class="token punctuation">(</span><span class="token variable">$appCertPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//调用getCertSN获取证书序列号</span>
<span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">alipayRootCertSN</span> <span class="token operator">=</span> <span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getRootCertSN</span><span class="token punctuation">(</span><span class="token variable">$rootCertPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//调用getRootCertSN获取支付宝根证书序列号</span> <span class="token variable">$request</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AlipayTradeQueryRequest</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">setBizContent</span><span class="token punctuation">(</span><span class="token double-quoted-string string">"{"</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">"\"out_trade_no\":\"20150320010101001\","</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">"\"trade_no\":\"2014112611001004680 073956707\","</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">"\"org_pid\":\"2088101117952222\","</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">" \"query_options\":["</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">" \"TRADE_SETTE_INFO\""</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">" ]"</span> <span class="token punctuation">.</span>
<span class="token double-quoted-string string">" }"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$aop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">execute</span> <span class="token punctuation">(</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$result</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

因为我要用的是 单笔转账到支付宝账户的接口。

所以 上面的代码要修改两个地方。

	$request = new AlipayFundTransToaccountTransferRequest ();
// 官方示例 切记要自己修改
$request->setBizContent("{" .
"\"out_biz_no\":\"3142321423432\"," .
"\"payee_type\":\"ALIPAY_LOGONID\"," .
"\"payee_account\":\"abc@sina.com\"," .
"\"amount\":\"12.23\"," .
"\"payer_show_name\":\"上海交通卡退款\"," .
"\"payee_real_name\":\"张三\"," .
"\"remark\":\"转账备注\"" .
" }");

修改完成之后,就可以跑代码进行测试了。

                                </div>

原文:https://blog.csdn.net/csd465038717/article/details/102748277

支付宝证书签名 PHP SDK的更多相关文章

  1. OpenSSL与公钥私钥证书签名的千丝万缕

    导语 人对任何事物的认识都是阶段性的,从无知到知晓,从懵懂到半知半解,从误解到将信将疑,从晕头转向到下定决心吃透. 介绍 OpenSSL是一个强大的命令行工具,它可以用来处理许多种跟PKI(Publi ...

  2. SHA256withRSA证书签名,私钥签名/公钥验签

    证书签名 package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  3. [加密]openssl之数字证书签名,CA认证原理及详细操作

    转自:http://blog.sina.com.cn/s/blog_cfee55a70102wn3h.html 1 公钥密码体系(Public-key Cryptography) 公钥密码体系,又称非 ...

  4. APP获取证书签名指纹

    Android: public static String getSignatureSHA1(Context context) { String sign = null; try { // 通过包管理 ...

  5. [Apple开发者帐户帮助]三、创建证书(7)创建证书签名请求

    Mac上的Keychain Access允许您创建证书签名请求(CSR). 启动位于的Keychain Access /Applications/Utilities. 选择Keychain Acces ...

  6. 集成支付宝钱包支付iOS SDK的方法与经验

    流程 摘自第一个文档<支付宝钱包支付接口开发包2.0标准版.pdf> 图中的“商户客户端”就是我们的iOS客户端需要做的事情: 调用支付宝支付接口 处理支付宝返回的支付结果 在调用支付宝支 ...

  7. ios--集成支付宝钱包支付iOS SDK的方法与经验

    文/胖花花(简书作者)原文链接:http://www.jianshu.com/p/fe56e122663e著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 没想到,支付宝的SDK是我目前 ...

  8. 集成支付宝钱包支付ios SDK的方法和经验

    没想到,支付宝的SDK是我目前用过的所有第三方SDK中最难用的一个了. 下载 首先,你要想找到这个SDK,都得费点功夫.现在的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 “请点此下载集成开 ...

  9. ClickOnce证书签名

    打开Microsoft .NET Framework 的SDK命令提示,按以下步骤操作: 1.创建一个自我签署的X.509证书(.cer)和一个.pvk私钥文件,用到makecert工具,命令如下: ...

随机推荐

  1. 16/7/11_PHP-图形图像操作

    GD库简介 GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新的图片. PHP除了能进行文本处理以外,通过GD ...

  2. poj2376Cleaning Shifts (贪心求解)

    描述 大表哥分配 N (1 <= N <= 25,000) 只中的一些奶牛在牛棚附近做些清洁. 他总是要让至少一只牛做清洁.他把一天分成T段(1 <= T <= 1,000,0 ...

  3. HDU 6583 Typewriter 题解

    ——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...

  4. Go-Mutex互斥量

    先来看一段go1.12.5中Mutex的源码: // Copyright 2009 The Go Authors. All rights reserved. // Use of this source ...

  5. Git002--安装

    Git--安装 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00 ...

  6. python3 -tensorflow-dataset 之map函数 lambda隐函数的应用

    ###隐函数lanbda及map函数的应用实例 ''' lambda 参数1,参数2,参数3,...:表达式 ''' f = lambda x,y :x+y print(f(1,2)) ###输出3 ...

  7. Object.watch

    /*  * object.watch polyfill  *  * 2012-04-03  *  * By Eli Grey, http://eligrey.com  * Public Domain. ...

  8. [POJ3612] Telephone Wire(暴力dp+剪枝)

    [POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...

  9. k8s<------docker

  10. 全栈开发系列学习2——django项目搭建

    项目代码:http://yunpan.cn/cHajgT4HvgHqx (提取码:8350) 配置项目: 1. 首先确保你的机器安装了python和pip,这两种安装比较简单,这里就不说了. 2. 在 ...