Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iTunes Subscription Status in PHP

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?

like image 541
Kal Avatar asked Oct 14 '25 05:10

Kal


1 Answers

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

like image 77
Will Jones Avatar answered Oct 16 '25 18:10

Will Jones