Exemplo n.º 1
0
 public function isValid()
 {
     if ($this->device->isAndroid()) {
         $validator = new PlayValidator(['client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'refresh_token' => env('GOOGLE_REFRESH_TOKEN')]);
         $transaction_receipt = json_decode($this->transaction->receipt);
         try {
             $response = $validator->setPackageName($transaction_receipt->packageName)->setProductId($transaction_receipt->productId)->setPurchaseToken($transaction_receipt->purchaseToken)->validate();
         } catch (\Exception $e) {
             return false;
         }
         return true;
     } else {
         if ($this->device->isIos()) {
             $validator = new iTunesValidator(iTunesValidator::ENDPOINT_PRODUCTION);
             try {
                 $response = $validator->setReceiptData($this->transaction->transactionReceipt)->validate();
             } catch (Exception $e) {
                 return false;
             }
             return $response->isValid();
         }
     }
     return false;
 }
    $receipt = $args['receiptId'];
    $validator = new ITunesValidator($endpoint);
    try {
        $response = $validator->setReceiptData($receipt)->validate();
    } catch (\Exception $e) {
        return sendResponse($res, $e->getMessage(), 500);
    }
    if ($response->isValid()) {
        return sendResponse($res, 'This receipt is valid.', 200, ['receipt' => $response->getReceipt()]);
    } else {
        return sendResponse($res, 'This receipt is invalid.', 403, ['result_code' => $response->getResultCode()]);
    }
});
$app->get('/validate/google/product/{productId}/token/{purchaseToken}', function ($req, $res, $args) use($config) {
    $clientId = "{$config->google->clientId}";
    $clientSecret = "{$config->google->clientSecret}";
    $refreshToken = "{$config->google->refreshToken}";
    $packageName = "{$config->google->packageName}";
    $productId = $args['productId'];
    $purchaseToken = $args['purchaseToken'];
    try {
        $validator = new PlayValidator(['client_id' => $clientId, 'client_secret' => $clientSecret, 'refresh_token' => $refreshToken]);
        $response = $validator->setPackageName($packageName)->setProductId($productId)->setPurchaseToken($purchaseToken)->validate();
    } catch (\Exception $e) {
        $message = $e->getMessage();
        $status = preg_match('/\\(404\\)/', $message) ? 404 : 403;
        return sendResponse($res, $message, $status);
    }
    return sendResponse($res, 'This product is valid.');
});
$app->run();