1

Now I am developing an in app purchase of iOS using flutter. This is my verify code snipping when client purchasing success:

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);
      }
    }
  });
}

the problem is I do not know which env of pay env, now I am test so I pass the isSandBox is true. How to know the isSandBox from current running context? what should I do to make it dynamic?

Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

0

actually you did not need to care the env is sandbox or production, you can always using production url first, the server will return 21007 if the recipt is generate in sandbox, after recieved the 21007 code, you could try again to verify in sandbox:

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

this is the advice of apple.

Dolphin
  • 29,069
  • 61
  • 260
  • 539