PHP code example of stafox / huawei-iap

1. Go to this page and download the library: Download stafox/huawei-iap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stafox / huawei-iap example snippets



use Huawei\IAP\AuthorizationCredentials;
use Huawei\IAP\Validator as HuaweiValidator;

$validator = new HuaweiValidator();

$appId = 123456789; // Your application ID
$appKey = 'XXXYYYZZZ'; // Your app key

$authCredentials = new AuthorizationCredentials($appId, $appKey);

$type = HuaweiValidator::TYPE_SUBSCRIPTION;
$subscriptionId = 'AAABBBCCC';
$productId = 'com.your.app.subscription';
$purchaseToken = 'purchaseTokenHere';

$purchaseData = new PurchaseData($type, $subscriptionId, $purchaseToken, $productId);

$subscriptionResponse = $validator->validate($authCredentials, $purchaseData);

$isSubscriptionValid = $subscriptionResponse->isSubValid();
$expirationDateMs = $subscriptionResponse->getExpirationDate();


use Huawei\IAP\AuthorizationCredentials;
use Huawei\IAP\Validator as HuaweiValidator;

$validator = new HuaweiValidator();

$appId = 123456789; // Your application ID
$appKey = 'XXXYYYZZZ'; // Your app key

$authCredentials = new AuthorizationCredentials($appId, $appKey);

$type = HuaweiValidator::TYPE_ORDER;
$productId = 'com.your.app.subscription';
$purchaseToken = 'purchaseTokenHere';

$purchaseData = new PurchaseData($type, null, $purchaseToken, $productId);

$orderResponse = $validator->validate($authCredentials, $purchaseData);

$pruchaseKind = $orderResponse->getKind();
$orderId = $orderResponse->getOrderId();
$consumptionState = $orderResponse->getConsumptionState();

use Huawei\IAP\AuthorizationStorage;
use Redis;

class RedisAuthorizationStorage extends AuthorizationStorage
{
    private $redisClient;

    public function __construct(Redis $redisClient)
    {
        $this->redisClient = $redisClient;
    }

    public function fetch(AuthorizationCredentials $credentials): ?string
    {
        $key = $this->transformCredentialsToKey($credentials);

        $at = $this->redisClient->get($key);
        return $at === false ? null : $at;
    }

    public function save(AuthorizationCredentials $credentials, string $accessToken): void
    {
        $key = $this->transformCredentialsToKey($credentials);

        $this->redisClient->set($key, $accessToken);
    }
}

use Huawei\IAP\Validator as HuaweiValidator;


$redisAuthStorage = new RedisAuthorizationStorage($redisClient);

$validator = new HuaweiValidator();
$validator->setAuthorizationStorage($redisAuthStorage);