菜鸟教程小白 发表于 2022-12-13 17:09:48

php - 验证交易收据时的状态 21002


                                            <p><p>我尝试查看所有其他问题,但似乎没有任何解决此问题的方法。我已经缩小范围,所以来自 <a href="https://sandbox.itunes.apple.com/verifyReceipt" rel="noreferrer noopener nofollow">https://sandbox.itunes.apple.com/verifyReceipt</a> 的响应只是 <code>{"status":21002}</code>。任何帮助将不胜感激,我已经复制了下面的相关代码。</p>

<pre><code>NSString *completeString = @&#34;http://www.mysite.com/verify.php&#34;;

NSURL *urlForValidation = ;

NSMutableURLRequest *validationRequest = [ initWithURL:urlForValidation];

;

NSString *strTest = ];

];

NSData *responseData = ;

NSString *response = [ initWithData: responseData encoding: NSUTF8StringEncoding];

NSLog(@&#34;%@&#34;, response);

- (NSString*) createEncodedString:(NSData*)data {
    static char table[] = &#34;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=&#34;;

    const int size = ((data.length + 2)/3)*4;
    uint8_t output;

    const uint8_t* input = (const uint8_t*);
    for (int i = 0; i &lt; data.length; i += 3)
    {
      int value = 0;
      for (int j = i; j &lt; (i + 3); j++)
      {
            value &lt;&lt;= 8;
            if (j &lt; data.length)
                value |= (0xFF &amp; input);
      }

      const int index = (i / 3) * 4;
      output =table[(value &gt;&gt; 18) &amp; 0x3F];
      output =table[(value &gt;&gt; 12) &amp; 0x3F];
      output = (i + 1) &lt; data.length ? table[(value &gt;&gt; 6)&amp; 0x3F] : &#39;=&#39;;
      output = (i + 2) &lt; data.length ? table[(value &gt;&gt; 0)&amp; 0x3F] : &#39;=&#39;;
    }

    return[ initWithBytes:output length:size encoding:NSASCIIStringEncoding];
}
</code></pre>

<p>最后这是使用的PHP代码:</p>

<pre><code>$url = &#39;https://sandbox.itunes.apple.com/verifyReceipt&#39;;

$receipt = &#34;{%s}&#34; % $_POST[&#34;receipt&#34;];

$purchase_encoded = base64_encode( $receipt );

$encodedData = json_encode( Array(
    &#39;receipt-data&#39; =&gt; $purchase_encoded
) );

//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);


//Decode response data using json_decode method to get an object.

echo $encodedResponse;

$response = json_decode( $encodedResponse );

echo $response;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>经过一番折腾,我终于弄明白了。我相信最大的错误在于我如何设置 HTTP 正文。最终代码如下,希望这可以帮助其他人!</p>

<p> Objective-C :</p>

<pre><code>NSString *completeString = @&#34;http://www.mysite.com/verify.php&#34;;

NSURL *urlForValidation = ;

NSMutableURLRequest *validationRequest = [ initWithURL:urlForValidation];

;

NSString *strTest = ];

];

NSData *responseData = ;

NSString *response = [ initWithData: responseData encoding: NSUTF8StringEncoding];

NSLog(@&#34;%@&#34;, response);
</code></pre>

<p>PHP:</p>

<pre><code>$url = &#39;https://sandbox.itunes.apple.com/verifyReceipt&#39;;

$encodedData = json_encode( Array(
    &#39;receipt-data&#39; =&gt; $_POST[&#34;receipt&#34;]
) );


//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);

$response = json_decode( $encodedResponse );

/*
echo &#34;Status Code:&#34;;

echo $response-&gt;{&#39;status&#39;};
*/

if ($response-&gt;{&#39;status&#39;} != 0) {
    echo &#34;FAIL&#34;;
} else {
    echo &#34;SUCCESS&#34;;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于php - 验证交易收据时的状态 21002,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12964728/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12964728/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - 验证交易收据时的状态 21002