支付宝证书签名 PHP SDK

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

posted @ 2019-12-24 13:55  橱窗外的小孩  阅读(579)  评论(0编辑  收藏