4

I am trying to find a way to check the user subscription status on Apple Store from the backend (PHP/Laravel). We know the user's UUID already and we have subscriptions setup on Apple. The user access the App after subscribing for the service through Apple for a trial period, what I need to check is if the user actually subscribed to the service after the trial period is done or not.

My question is how do you communicate with Apple Store to get user's subscription status using PHP/Laravel as the backend?

To clarify, the application allows users to subscribe to a service via web and mobile app. Via web is easy to get the user's subscription status since we are using Stripe for that. However, on the app, users subscribe to the service through Apple Store. So again, how do you communicate with Apple Store to get user's subscription status using PHP/Laravel as the backend?

Kal
  • 948
  • 17
  • 30
  • The Apple store doesn't have some sort of API to query like you would stripe to determine if the user is subscribed? A quick google search returns this but I am not an iOS dev https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/Subscriptions.html – Orlando P. Jun 07 '18 at 21:16
  • This question might help also. https://stackoverflow.com/questions/5120177/how-to-check-in-app-purchase-auto-renewable-subscription-is-valid – Orlando P. Jun 07 '18 at 21:18
  • Maybe this will help you: https://github.com/aporat/store-receipt-validator – zen Jun 11 '18 at 09:32

1 Answers1

2

You can interrogate the App Store for this information from a trusted server.

The endpoint for the sandbox environment is https://sandbox.itunes.apple.com/verifyReceipt
and for production it's https://buy.itunes.apple.com/verifyReceipt

You need to send the following as a JSON payload:

receipt-data If you don't have it on your server, it can retrieved by calling the appStoreReceiptURL method of NSBundle. Read the entire contents of that file and send it to your server.

Password (For auto-renewable subscriptions only, it'll be your app's shared secret)

exclude-old-transactions Only used for iOS7 style app receipts that contain auto-renewable or non-renewing subscriptions. If value is true, response includes only the latest renewal transaction for any subscriptions.

It'll then return a payload containing the receipt status and some other additional information.

Check this article at Apple

Edit

use cURL to call the App Store endpoint (linked above). Here's a rough example, you'll need to modify it for your particular environment and to fill in the required variables.

$service_url = [one of the two above];
$curl = curl_init($service_url);
$curl_post_data = array(
        'receipt-data' => $receiptData,
        'password' => $password, //Only required for certain types of subscription
        'exclude-old-transactions' => $excludeoldtransactions //Depends on your use case, check Apple link
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_dump($decoded->response);

http://php.net/manual/en/book.curl.php

Community
  • 1
  • 1
Will Jones
  • 1,861
  • 13
  • 24
  • Can you please show the code to make these calls `receipt-data` and `appStoreReceiptURL` in PHP please. – Kal Jun 11 '18 at 18:48
  • where do you get the receipt `$receiptData`? – Kal Jun 11 '18 at 23:12
  • You need to send that data over to your server from the iOS device. Persist that in your database and then retrieve it in this script. So you'll need a second script that your iOS app calls in order to get that information into your database. – Will Jones Jun 12 '18 at 11:16