【问题标题】:how to known the app env is a sandbox when developing in app purchase in ios using flutter使用flutter在ios中购买应用程序时如何知道应用程序环境是沙箱
【发布时间】:2021-09-08 23:29:18
【问题描述】:

现在我正在使用 Flutter 开发 iOS 应用内购买。这是客户购买成功时我的验证码截图:

void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList, Context<PayState> ctx) {
  purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
    if (purchaseDetails.status == PurchaseStatus.pending) {
      RestLog.logger("PurchaseStatus pending..." + ctx.state.payModel.isAvailable.toString());
      _showPendingUI(ctx);
    } else {
      if (purchaseDetails.status == PurchaseStatus.error) {
        RestLog.logger("PurchaseStatus error");
        _handleError(purchaseDetails.error!, ctx);
      } else if (purchaseDetails.status == PurchaseStatus.purchased || purchaseDetails.status == PurchaseStatus.restored) {
        RestLog.logger("purchase successful trigger verify");
        PayVerifyModel payVerifyModel =
            PayVerifyModel(orderId: purchaseDetails.purchaseID, receipt: purchaseDetails.verificationData.serverVerificationData, isSandBox: true);
        Pay.verifyUserPay(payVerifyModel);
      }
      if (purchaseDetails.pendingCompletePurchase) {
        await InAppPurchase.instance.completePurchase(purchaseDetails);
      }
    }
  });
}

问题是我不知道支付环境是哪个环境,现在我正在测试所以我通过了 isSandBox 是真的。如何从当前运行的上下文中知道 isSandBox?我应该怎么做才能让它充满活力?

【问题讨论】:

  • stackoverflow.com/questions/49707028/… 解决了您的问题吗?
  • 不,我想知道 isSandBox 的状态。 @Muhtar
  • 为什么不使用发布模式和调试模式?我看不到重点抱歉
  • 这是苹果沙盒状态探测,有时在沙盒付费模式下也有发布模式。 @Muhtar

标签: flutter


【解决方案1】:

其实你不需要关心环境是沙箱还是生产环境,你总是可以先使用生产URL,如果在沙箱中生成recipt,服务器将返回21007,在收到21007代码后,你可以再次尝试验证在沙盒中:

private static JSONObject validateApplePay(BuySuccessRequest request, String url) {
        try {
            HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setAllowUserInteraction(false);
            PrintStream ps = new PrintStream(connection.getOutputStream());
            // Base64.Encoder encoder = Base64.getEncoder();
            // String base64String = encoder.encodeToString(request.getReceipt().getBytes(StandardCharsets.UTF_8));
            ps.print("{\"receipt-data\": \"" + request.getReceipt() + "\"}");
            ps.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str;
            StringBuffer sb = new StringBuffer();
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            // String encodeToString = Base64.getEncoder().encodeToString("Test".getBytes());
            br.close();
            String resultStr = sb.toString();
            JSONObject result = JSONObject.parseObject(resultStr);
            if (result != null && result.getInteger("status").intValue() == ApplePayVerifyResponseType.SANDBOX_SEND_TO_PRODUCTION.getKey().intValue()) {
                return validateApplePay(request, SAND_BOX_VERIFY_URL);
            }
            return result;
        } catch (Exception e) {
            log.error("Validate pay failed", e);
        }
        return null;
    }

这是苹果的advice

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 2012-11-29
    • 2012-08-04
    • 1970-01-01
    • 2023-03-12
    • 2017-01-27
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    相关资源
    最近更新 更多