zdk/applestore

Google Play收据验证器

v1.2 2021-01-13 11:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 03:14:09 UTC


README

Latest Version on Packagist Total Downloads

Google Play和App Store提供内购(IAP)服务。IAP可用于销售各种内容,包括订阅、新功能和服务等。购买事件和支付过程在移动应用程序(iOS和Android)上发生,并由应用程序处理,然后您的后端需要了解此购买事件以交付购买的产品或更新用户的订阅状态。

Laravel In-App purchase旨在帮助您解析和验证购买的产品,并处理订阅的不同状态,如新订阅、自动续订、取消、到期等。

目录

安装

通过composer安装包

composer require imdhemy/laravel-purchases

发布配置文件

php artisan vendor:publish --provider="Imdhemy\Purchases\PurchaseServiceProvider" --tag="config"

配置

发布的配置文件config/purchase.php看起来像

return [
    'routing' => [],

    'google_play_package_name' => env('GOOGLE_PLAY_PACKAGE_NAME', 'com.example.name'),

    'appstore_sandbox' => env('APPSTORE_SANDBOX', true),

    'appstore_password' => env('APPSTORE_PASSWORD', ''),

    'eventListeners' => [
        /**
         * --------------------------------------------------------
         * Google Play Events
         * --------------------------------------------------------
         */
        SubscriptionPurchased::class => [],
        SubscriptionRenewed::class => [],
        SubscriptionInGracePeriod::class => [],
        SubscriptionExpired::class => [],
        SubscriptionCanceled::class => [],
        SubscriptionPaused::class => [],
        SubscriptionRestarted::class => [],
        SubscriptionDeferred::class => [],
        SubscriptionRevoked::class => [],
        SubscriptionOnHold::class => [],
        SubscriptionRecovered::class => [],
        SubscriptionPauseScheduleChanged::class => [],
        SubscriptionPriceChangeConfirmed::class => [],

        /**
         * --------------------------------------------------------
         * Appstore Events
         * --------------------------------------------------------
         */
        Cancel::class => [],
        DidChangeRenewalPref::class => [],
        DidChangeRenewalStatus::class => [],
        DidFailToRenew::class => [],
        DidRecover::class => [],
        DidRenew::class => [],
        InitialBuy::class => [],
        InteractiveRenewal::class => [],
        PriceIncreaseConsent::class => [],
        Refund::class => [],
    ],
];

每个配置选项都在配置部分中有说明。

一、通用配置

通用配置不针对两个支持提供者(Google和Apple)中的任何一个。

一、1 路由

此包添加了两个POST端点以接收实时开发者通知App Store服务器通知

可以通过配置文件中的routing键配置这些路由。例如

[
    // ..
   'routing' => [
        'middleware' => 'api',
        'prefix' => 'my_prefix'
    ],
    // ..
];

一、2 事件监听器

您的应用程序应处理订阅生命周期的不同状态。每个状态更新都会触发一个指定的事件。您可以为每种情况创建一个事件监听器以更新您的后端。

use Imdhemy\Purchases\Events\GooglePlay\SubscriptionRenewed;

class AutoRenewSubscription 
{   
    /**
    * @param SubscriptionRenewed $event
    */
    public function handle(SubscriptionRenewed $event)
    {
        // do some stuff
    }   
}

将创建的监听器添加到相关的事件键。

    // ..
        SubscriptionRenewed::class => [AutoRenewSubscription::class],
    // ..

所有事件都扩展了\Imdhemy\Purchases\Events\PurchaseEvent抽象类,该类实现了\Imdhemy\Purchases\Contracts\PurchaseEventContract接口。有关更多信息,请参阅购买事件部分

二、Google Play配置

以下配置特定于Google Play

二、1 Google应用凭证

请求Google Play开发者API需要身份验证和作用域。要身份验证您的机器,请创建服务帐户,然后上传下载的JSON文件google-app-credentials.json到您的服务器,最后将GOOGLE_APPLICATION_CREDENTIALS键添加到您的.env文件中,并将其设置为JSON文件的路径。

  1. 在云控制台中,转到创建服务帐户密钥页面。
  2. Service account列表中选择New service account
  3. Service account name字段中输入一个名称。
  4. Role列表中选择Project > Owner
  5. 单击Create。一个包含您的密钥的JSON文件将下载到您的计算机。
  6. 将JSON文件上传到您的存储目录或任何受保护的目录。
  7. .env中的GOOGLE_APPLICATION_CREDENTIALS键设置为JSON文件路径。

二、2 Google Play包名

购买此订阅的应用程序的包名(例如,'com.some.thing')。

三、App Store配置

以下配置设置专门针对App Store。

三、1 App Store沙盒

配置键appstore_sandbox是一个布尔值,用于确定发送到App Store的请求是在沙箱中进行还是不是。

三、2 App Store密码

向App Store发送请求需要设置键appstore_password。这是您的应用的共享密钥,是一个十六进制字符串。

销售产品

Google产品

您可以使用\Imdhemy\Purchases\Facades\Product外观来确认或获取Google Play的收据数据,如下所示

use \Imdhemy\Purchases\Facades\Product;
use \Imdhemy\GooglePlay\Products\ProductPurchase;

$itemId = 'product_id';
$token = 'purchase_token';

Product::googlePlay()->id($itemId)->token($token)->acknowledge();
// You can optionally submit a developer payload
Product::googlePlay()->id($itemId)->token($token)->acknowledge("your_developer_payload");

/** @var ProductPurchase $productReceipt */
$productReceipt = Product::googlePlay()->id($itemId)->token($token)->get();

每个键都有一个以get为前缀的获取方法,例如:getKind()来获取kind值。更多详细信息请查看

  1. Google开发者文档.
  2. PHP Google Play计费包.

App Store产品

您可以使用\Imdhemy\Purchases\Facades\Product向App Store发送verifyReceipt请求,如下所示

use Imdhemy\AppStore\Receipts\ReceiptResponse;
use \Imdhemy\Purchases\Facades\Product;

$receiptData = 'the_base64_encoded_receipt_data';
/** @var ReceiptResponse $receiptResponse */
$receiptResponse = Product::appStore()->receiptData($receiptData)->verifyReceipt();

通常每个键都有一个获取方法。

更多详细信息请查看

  1. App Store文档
  2. PHP App Store IAP包

销售订阅

Google Play订阅

您可以使用\Imdhemy\Purchases\Facades\Subscription外观来确认或获取Google Play的收据数据,如下所示

use Imdhemy\GooglePlay\Subscriptions\SubscriptionPurchase;
use Imdhemy\Purchases\Facades\Subscription;

$itemId = 'product_id';
$token = 'purchase_token';

Subscription::googlePlay()->id($itemId)->token($token)->acknowledge();
// You can optionally submit a developer payload
Subscription::googlePlay()->id($itemId)->token($token)->acknowledge("your_developer_payload");

/** @var SubscriptionPurchase $subscriptionReceipt */
$subscriptionReceipt = Subscription::googlePlay()->id($itemId)->token($token)->get();
// You can optionally override the package name
Subscription::googlePlay()->packageName('com.example.name')->id($itemId)->token($token)->get();

SubscriptionPurchase资源表示用户内购产品购买的状态。这是其JSON表示形式

更多详细信息请查看

  1. Google开发者文档.
  2. PHP Google Play计费包.

App Store订阅

您可以使用\Imdhemy\Purchases\Facades\Subscription向App Store发送verifyReceipt请求,如下所示

use Imdhemy\Purchases\Facades\Subscription;
// To verify a subscription receipt
$receiptData = 'the_base64_encoded_receipt_data';
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->verifyReceipt();

// If the subscription is an auto-renewable one, 
//call the renewable() method before the trigger method verifyReceipt()
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->renewable()->verifyReceipt();

// or you can omit the renewable() method and use the verifyRenewable() method instead
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->verifyRenewable();

更多详细信息请查看

  1. 使用App Store验证收据
  2. PHP App Store IAP包

购买事件

如配置部分所述,您的应用应该处理订阅生命周期的不同状态。每个状态更新都会触发一个指定的事件。您可以为每种情况创建一个事件监听器来更新您的后端。

所有触发的事件都实现了Imdhemy\Purchases\Contracts\PurchaseEventContract接口,这使得您可以通过getServerNotification()方法获取接收通知的标准表示。

检索到的通知是类型为\Imdhemy\Purchases\Contracts\ServerNotificationContract,这允许您使用getSubscription()方法获取订阅的标准表示。

订阅对象提供以下方法

  1. getExpiryTime()返回一个Time对象,告诉订阅的过期时间。
  2. getItemId()返回购买的订阅ID。
  3. getProvider()返回此订阅的提供者,可以是google_playapp_store
  4. getUniqueIdentifier()返回此订阅的唯一标识符。
  5. getProviderRepresentation()根据提供者返回SubscriptionPurchaseReceiptResponse

以下是一个自动续订订阅的示例

use Imdhemy\Purchases\Events\GooglePlay\SubscriptionRenewed;

class AutoRenewSubscription 
{   
    /**
    * @param SubscriptionRenewed $event
    */
    public function handle(SubscriptionRenewed $event)
    {   
       // The following data can be retrieved from the event
       $notification = $event->getServerNotification();
       $subscription = $notification->getSubscription();
       $uniqueIdentifier = $subscription->getUniqueIdentifier();
       $expirationTime = $subscription->getExpiryTime();
        
       // The following steps are up to you, this is only an imagined scenario.
       $user = $this->findUserBySubscriptionId($uniqueIdentifier);
       $user->getSubscription()->setExpirationTime($expirationTime);
       $user->save();        
        
       $this->notifyUserAboutUpdate($user);
    }   
}

测试

TODO: 添加测试示例。