Search code examples
javaspringin-app-purchaseresttemplatein-app-purchase-receipt

Apple purchase : verify receipt using Rest template


I have to implement an API which aims to verify a receipt through Apple server using Rest template. However, I receive always a status code 21002 from https://sandbox.itunes.apple.com/verifyReceipt

 public void verifyReceipt(Purchase purchase) {

    String uri = "https://sandbox.itunes.apple.com/verifyReceipt";
    String receipt = new String(Base64.getEncoder().encode(purchase.getReceipt().getBytes()));

    Map<String, String> params = new HashMap<>();
    params.put("receipt-data", receipt);
    params.put("password", "secret");

    Gson gson = new Gson();
    String json = gson.toJson(params);
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> request = new HttpEntity<>(json, headers);
    ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, request ,String.class);
....

}

Apple documentation : https://developer.apple.com/documentation/appstorereceipts/verifyreceipt

Any idea ?


Solution

  • What is the Purchase class? The receipt data that the client side(iOS App) send to our server side should already be base64 encoded.