stafox / huawei-iap
Huawei In-App Purchase (IAP) verification library
Installs: 9 768
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: >=7.2
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ~6.3|~7.0
Requires (Dev)
- phpunit/phpunit: ^7.0|^8.0
README
PHP library that can be used for verifying In-App Purchases for the Huawei's Order and Subscription services.
Requirements
- PHP >= 7.2
- ext-json
- ext-curl
Getting Started
The easiest way to work with this package is when it's installed as a Composer package inside your project. Composer isn't strictly required, but makes life a lot easier.
If you're not familiar with Composer, please see http://getcomposer.org/.
-
Add
huawei-iap
to your application's composer.json.{ ... "require": { "stafox/huawei-iap": "main" }, ... }
-
Run
php composer install
. -
If you haven't already, add the Composer autoload to your project's initialization file. (example)
require 'vendor/autoload.php';
Quick Usage Examples
Subscription validation
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();
Order (one-time purchase) validation
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 custom AuthorizationStorage
By default auth token stored in-memory. To reduce number of authorization requests you can extend AuthorizationStorage to store data for longer period of time.
For example:
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); } }
And then pass it into Validator.
use Huawei\IAP\Validator as HuaweiValidator; $redisAuthStorage = new RedisAuthorizationStorage($redisClient); $validator = new HuaweiValidator(); $validator->setAuthorizationStorage($redisAuthStorage);
Select store site
By default Germany store site will be used. It may be useful to use different store sites to reduce request time.
To do that you need to extend Validator
and override
createSubscriptionVerificationRequest(PurchaseData $purchaseData)
createOrderVerificationRequest(PurchaseData $purchaseData)
And extend PurchaseDate
to be able get country, for example.
Then pass needed country to Validator::getClient()
method.