支付宝证书签名 PHP SDK

2021年09月16日 阅读数:1
这篇文章主要向大家介绍支付宝证书签名 PHP SDK,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

<div id="content_views" class="markdown_views prism-atom-one-light"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <p></p><div class="toc"><h3><a name="t0"></a>PHP 接入支付宝证书方式签名以及验签</h3></div><p></p> <p>支付宝在 2019.10.25 日左右更新了新的 PHP SDK (v4.1.0)。 以前的 PHP SDK(v3.4.2) 仅支持公钥方式加签。此次更新以后 PHP SDK 也能够使用证书进行签名和验签了。</p> <p><a href="https://docs.open.alipay.com/54/103419/" rel="nofollow">支付宝官方 SDK 下载地址</a></p> <p>下载解压 并将文件夹中的 aop 文件夹 直接复制到项目中你想要放到文件夹里,例如 extend<br> 文件夹 (为了测试 crt 证书文件也已经放到同目录下 cert 目录中了)<br> <img src="https://img-blog.csdnimg.cn/20191025191726611.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzZDQ2NTAzODcxNw==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br> 先修正 SDK 中的1个小错误:AopCertClient.php 中<br> 478 行左右 有一个检查是否为空的方法<br> <img src="https://img-blog.csdnimg.cn/20191025194517119.png" alt="在这里插入图片描述"><br> 修改成</p> <pre class="prettyprint"><code class="prism language-php has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">method_exists</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'getApiVersion'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token variable">$iv</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getApiVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token variable">$iv</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">apiVersion</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <div class="hljs-button {2}" data-title="复制"></div></code></pre> <p>引入文件</p> <pre class="prettyprint"><code class="prism language-php has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"> <span class="token keyword">require_once</span> <span class="token constant">EXTEND_PATH</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'aop/AopCertClient.php'</span><span class="token punctuation">;</span> <span class="token keyword">require_once</span> <span class="token constant">EXTEND_PATH</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'aop/AopCertification.php'</span><span class="token punctuation">;</span> <span class="token keyword">require_once</span> <span class="token constant">EXTEND_PATH</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'aop/request/AlipayTradeQueryRequest.php'</span><span class="token punctuation">;</span> <span class="token keyword">require_once</span> <span class="token constant">EXTEND_PATH</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'aop/request/AlipayTradeWapPayRequest.php'</span><span class="token punctuation">;</span> <span class="token keyword">require_once</span> <span class="token constant">EXTEND_PATH</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'aop/request/AlipayOpenOperationOpenbizmockBizQueryRequest.php'</span><span class="token punctuation">;</span> <div class="hljs-button {2}" data-title="复制"></div></code></pre> <p>从 aop/test/ 目录下 找下 AopCertClientTest.php (由于是使用证书进行签名,若是是使用公钥进行签名,就应该参考 AopClientTest.php 文件,不作赘述)直接复制相关代码进行修改好比 我复制的就是</p> <pre class="prettyprint"><code class="prism language-php has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"> <span class="token comment">//一、execute 使用</span> <span class="token variable">$aop</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AopCertClient</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$appCertPath</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt"</span><span class="token punctuation">;</span> <span class="token variable">$alipayCertPath</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt"</span><span class="token punctuation">;</span> <span class="token variable">$rootCertPath</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt"</span><span class="token punctuation">;</span>php

<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>

<div class="hljs-button {2}" data-title="复制"></div></code></pre> <p>由于我要用的是 单笔转帐到支付宝帐户的接口。<br> 因此 上面的代码要修改两个地方。</p> <pre class="prettyprint"><code class="prism language-php has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"> <span class="token variable">$request</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AlipayFundTransToaccountTransferRequest</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 官方示例 切记要本身修改</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_biz_no\":\"3142321423432\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"payee_type\":\"ALIPAY_LOGONID\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"payee_account\":\"abc@sina.com\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"amount\":\"12.23\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"payer_show_name\":\"上海交通卡退款\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"payee_real_name\":\"张三\","</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">"\"remark\":\"转帐备注\""</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">" }"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <div class="hljs-button {2}" data-title="复制"></div></code></pre> <p>修改完成以后,就能够跑代码进行测试了。</p>web

                                </div>

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