stafox/huawei-iap 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stafox/huawei-iap

最新稳定版本:1.0.0

Composer 安装命令:

composer require stafox/huawei-iap

包简介

Huawei In-App Purchase (IAP) verification library

README 文档

README

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality License

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/.

  1. Add huawei-iap to your application's composer.json.

     {
         ...
         "require": {
             "stafox/huawei-iap": "main"
         },
         ...
     }
    
  2. Run php composer install.

  3. 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.

统计信息

  • 总下载量: 13.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-29