Skip to content

Instantly share code, notes, and snippets.

@maldechavda
Created July 14, 2023 08:28
Show Gist options
  • Save maldechavda/41a65aacf8996dd7485b874dcfac4836 to your computer and use it in GitHub Desktop.
Save maldechavda/41a65aacf8996dd7485b874dcfac4836 to your computer and use it in GitHub Desktop.
Verify IOS inapp purchase
?php
namespace App\Http\Controllers\Api;
use App\Enums\PurchaseType;
use App\Http\Requests\PurchaseRequest;
use App\Http\Resources\UserResource;
use App\Models\App;
use App\Models\Subscription;
use App\Models\User;
use Carbon\Carbon;
use App\Http\Controllers\Controller;
use App\Http\Requests\AutoRenewRequest;
class PurchaseController extends Controller
{
public function checkAutoRenew(AutoRenewRequest $request)
{
$user = User::with('app', 'subscription')->find($request->get('userId'));
$receipt = json_encode([
"receipt-data" => $request->get('receiptInfo'),
"password" => $user->app->generatedInApp,
]);
if ($request->get('type') == 'sandbox') {
$url = "https://sandbox.itunes.apple.com/verifyReceipt";
} else {
$url = "https://buy.itunes.apple.com/verifyReceipt";
}
//send receipt to Apple
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$receipt);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
//getting answer from Apple
$object = json_decode($buffer);
$array = json_decode($buffer, true);
$status = $object->{'status'};
if ($status == 0) {
$inAppData = array_get($array, 'latest_receipt_info');
if (!$inAppData || ! is_array($inAppData)) {
return response()->json([
'success' => false,
]);
}
$inAppData = array_reverse($inAppData);
//$searchKey = array_search($request->get('package_name'), array_column($inAppData, 'product_id'));
$latestData = $inAppData[0];
if (substr($latestData['expires_date'], 0, 19) < Carbon::now()) {
$user->fill([
'purchase_date' => substr($latestData['original_purchase_date'], 0, 19),
'expiry_date' => substr($latestData['expires_date'], 0, 19),
'is_purchased' => false
])->save();
Subscription::createOrUpdate([
'user_id' => $user->getKey(),
'app_id' => $user->app->getKey(),
'original_transaction_id' => array_get($latestData, 'original_transaction_id'),
'product_id' => array_get($latestData, 'product_id'),
'purchase_at' => Carbon::parse(array_get($latestData, 'original_purchase_date')),
'expiry_at' => Carbon::parse(array_get($latestData, 'expires_date')),
]);
$user->fresh();
} else {
$user->fill([
'purchase_date' => substr($latestData['original_purchase_date'], 0, 19),
'expiry_date' => substr($latestData['expires_date'], 0, 19),
'is_purchased' => true
])->save();
Subscription::createOrUpdate([
'user_id' => $user->getKey(),
'app_id' => $user->app->getKey(),
'original_transaction_id' => array_get($latestData, 'original_transaction_id'),
'product_id' => array_get($latestData, 'product_id'),
'purchase_at' => Carbon::parse(array_get($latestData, 'original_purchase_date')),
'expiry_at' => Carbon::parse(array_get($latestData, 'expires_date')),
]);
$user->fresh();
}
return new UserResource($user);
}
return response()->json([
'success' => false,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment