葫芦七朵花 發表於 2019-9-27 15:51:00

PHP后端处理苹果内购对接

<h2><strong>苹果内购流程:</strong></h2>
<p>iOS App上次苹果商店审核对于虚拟金币类必须要用苹果支付,不能使用第三方支付,苹果支付还要3/7分成,呵呵...</p>
<p><img src="https://img2018.cnblogs.com/blog/1087123/201909/1087123-20190927153511453-98743553.jpg"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px">1、前六步有IOS端处理,最终获取购买凭证</span></p>
<p><span style="font-size: 18px">2、POST请求,发送购买凭证receipt-data到服务端接口</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">    /*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 苹果内购
   * @param receipt-data 购买凭证(必传)
   * @param is_test 是否沙盒数据(选填,1是 0否,默认否)
   * @return json
   *</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> actionsApple_pay(){

      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!framework::post('is_test')) {    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">沙盒购买地址</span>
            <span style="color: rgba(128, 0, 128, 1)">$url</span> = "https://sandbox.itunes.apple.com/verifyReceipt"<span style="color: rgba(0, 0, 0, 1)">;
      }</span><span style="color: rgba(0, 0, 255, 1)">else</span>{<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">正式购买地址</span>
            <span style="color: rgba(128, 0, 128, 1)">$url</span> = "https://buy.itunes.apple.com/verifyReceipt"<span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 0, 128, 1)">$receipt_data</span> = framework::post('receipt-data'<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 验证参数</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 128, 128, 1)">strlen</span>(<span style="color: rgba(128, 0, 128, 1)">$receipt_data</span>) &lt; 20<span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(128, 0, 128, 1)">$result</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
                </span>'status'=&gt;<span style="color: rgba(0, 0, 255, 1)">false</span>,
                'message'=&gt;'非法参数'<span style="color: rgba(0, 0, 0, 1)">
            );
            </span><span style="color: rgba(0, 0, 255, 1)">echo</span> json_encode(<span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      </span><span style="color: rgba(128, 0, 128, 1)">$post_data</span> = json_encode(<span style="color: rgba(0, 0, 255, 1)">array</span>("receipt-data" =&gt; <span style="color: rgba(128, 0, 128, 1)">$receipt_data</span><span style="color: rgba(0, 0, 0, 1)">));
      </span><span style="color: rgba(128, 0, 128, 1)">$response</span> = https::curlHttps(<span style="color: rgba(128, 0, 128, 1)">$url</span>, <span style="color: rgba(128, 0, 128, 1)">$post_data</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(128, 0, 128, 1)">$res</span> = json_decode(<span style="color: rgba(128, 0, 128, 1)">$response</span>, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);

      </span><span style="color: rgba(128, 0, 128, 1)">$err_msg</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
            </span>'21000' =&gt; 'App Store不能读取你提供的JSON对象',
            '21002' =&gt; 'receipt-data域的数据有问题',
            '21003' =&gt; 'receipt无法通过验证',
            '21004' =&gt; '提供的shared secret不匹配你账号中的shared secret',
            '21005' =&gt; 'receipt服务器当前不可用',
            '21006' =&gt; 'receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送',
            '21007' =&gt; 'receipt是Sandbox receipt,但却发送至生产系统的验证服务',
            '21008' =&gt; 'receipt是生产receipt,但却发送至Sandbox环境的验证服务'<span style="color: rgba(0, 0, 0, 1)">
      );

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断是否购买成功   </span>
      <span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 128, 128, 1)">intval</span>(<span style="color: rgba(128, 0, 128, 1)">$res</span>['status']) === 0<span style="color: rgba(0, 0, 0, 1)">){   
            </span><span style="color: rgba(128, 0, 128, 1)">$result</span> = ['status'=&gt;<span style="color: rgba(0, 0, 255, 1)">true</span>, 'message'=&gt;'正式购买成功'<span style="color: rgba(0, 0, 0, 1)">];   
      }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{   
            </span><span style="color: rgba(128, 0, 128, 1)">$result</span> = ['status'=&gt;<span style="color: rgba(0, 0, 255, 1)">false</span>, 'message' =&gt; '购买失败 status:'.<span style="color: rgba(128, 0, 128, 1)">$res</span>['status'].' - '.@<span style="color: rgba(128, 0, 128, 1)">$err_msg</span>[<span style="color: rgba(128, 0, 128, 1)">$res</span>['status'<span style="color: rgba(0, 0, 0, 1)">]] ];
            framework</span>::logWrite('苹果支付失败 ---res---'.json_encode(<span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">));
      }

      </span><span style="color: rgba(0, 0, 255, 1)">echo</span> json_encode(<span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>注</strong>:<span style="font-size: 14px">测试阶段返回 21007,默认为沙盒数据,传is_test参数即可查看响应结果</span></span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/sanplit/p/apply_pay.html
頁: [1]
查看完整版本: PHP后端处理苹果内购对接