IAP内置购买服务器端

  1. <?php  
  2.     //服务器二次验证代码  
  3.     function getReceiptData($receipt$isSandbox = false)     
  4.     {     
  5.         if ($isSandbox) {     
  6.             $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';     
  7.         }     
  8.         else {     
  9.             $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';     
  10.         }     
  11.       
  12.         $postData = json_encode(     
  13.             array('receipt-data' => $receipt)     
  14.         );     
  15.       
  16.         $ch = curl_init($endpoint);     
  17.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
  18.         curl_setopt($ch, CURLOPT_POST, true);     
  19.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);     
  20.        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  //这两行一定要加,不加会报SSL 错误  
  21.         curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);   
  22.   
  23.   
  24.         $response = curl_exec($ch);     
  25.         $errno    = curl_errno($ch);     
  26.         $errmsg   = curl_error($ch);     
  27.         curl_close($ch);     
  28.     //判断时候出错,抛出异常  
  29.         if ($errno != 0) {     
  30.             throw new Exception($errmsg$errno);     
  31.         }     
  32.                   
  33.         $data = json_decode($response);     
  34.     //判断返回的数据是否是对象  
  35.         if (!is_object($data)) {     
  36.             throw new Exception('Invalid response data');     
  37.         }     
  38.     //判断购买时候成功  
  39.         if (!isset($data->status) || $data->status != 0) {     
  40.             throw new Exception('Invalid receipt');     
  41.         }     
  42.       
  43.     //返回产品的信息             
  44.         return array(     
  45.             'quantity'       =>  $data->receipt->quantity,     
  46.             'product_id'     =>  $data->receipt->product_id,     
  47.             'transaction_id' =>  $data->receipt->transaction_id,     
  48.             'purchase_date'  =>  $data->receipt->purchase_date,     
  49.             'app_item_id'    =>  $data->receipt->app_item_id,     
  50.             'bid'            =>  $data->receipt->bid,     
  51.             'bvrs'           =>  $data->receipt->bvrs     
  52.         );     
  53.     }     
  54.       
  55.     //获取 App 发送过来的数据,设置时候是沙盒状态  
  56.         $receipt   = $_GET['data'];     
  57.         $isSandbox = true;     
  58.     //开始执行验证  
  59.     try  
  60.      {  
  61.          $info = getReceiptData($receipt$isSandbox);     
  62.          // 通过product_id 来判断是下载哪个资源  
  63.          switch($info['product_id']){  
  64.             case 'com.application.xxxxx.xxxx':  
  65.                 Header("Location:xxxx.zip");  
  66.             break;             
  67.         }  
  68.      }  
  69.     //捕获异常  
  70.     catch(Exception $e)  
  71.     {  
  72.         echo 'Message: ' .$e->getMessage();  
  73.     }  
  74. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity IAP中,恢复购买流程需要以下几个步骤: 1. 在代码中实现恢复购买功能。在Unity中,你可以使用IAPManager类在代码中实现恢复购买功能。以下是一个示例: ```csharp using UnityEngine; using UnityEngine.Purchasing; public class IAPManager : MonoBehaviour, IStoreListener { private IStoreController m_Controller; void Start() { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("TestProduct", ProductType.Consumable); UnityPurchasing.Initialize(this, builder); } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { m_Controller = controller; } public void OnInitializeFailed(InitializationFailureReason error) { Debug.Log("IAP initialization failed: " + error); } public void OnPurchaseFailed(Product item, PurchaseFailureReason error) { Debug.Log("IAP purchase failed: " + error); } public void OnPurchaseComplete(Product item, PurchaseEventArgs args) { Debug.Log("IAP purchase complete: " + item.definition.id); } public void Purchase(string productId) { if (m_Controller != null) { var product = m_Controller.products.WithID(productId); if (product != null && product.availableToPurchase) { m_Controller.InitiatePurchase(product); } else { Debug.Log("IAP product not available: " + productId); } } } public void RestorePurchases() { if (m_Controller != null) { m_Controller.RestoreTransactions(); } } } ``` 在上面的代码中,我们添加了一个RestorePurchases方法,该方法调用m_Controller.RestoreTransactions()方法来恢复购买。 2. 在iOS项目中配置恢复购买功能。要在iOS上启用恢复购买功能,你需要在Apple开发者中心中创建一个新的IAP项目,并将其添加到Xcode项目中。在Xcode中,你需要设置应用程序的IAP权限,并添加IAP产品的标识符。 3. 在Unity中测试恢复购买功能。在Unity中,你可以使用IAPManager中的RestorePurchases方法测试恢复购买功能。在调用RestorePurchases方法之前,请确保你已经在Unity IAP控制台中创建了一个有效的产品,并在iOS项目中正确配置了IAP。 以上就是Unity IAP中恢复购买流程的实现步骤。请注意,在实际发布应用程序之前,请确保你已经按照苹果的要求正确实现了恢复购买功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值