imdhemy-simpleclick-dustin/laravel-purchases

用于Google Play的Laravel发票验证器

0.10.3 2021-04-03 15:29 UTC

README

Laravel In-app Purchase cover

Packagist PHP Version Support Latest Version on Packagist Total Downloads GitHub last commit

✅ App Store ✅ Google Play

Laravel In-App purchase

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_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. 服务帐户列表中,选择新建服务帐户
  3. 服务帐户名称字段中,输入一个名称。
  4. 角色列表中,选择项目 > 所有者
  5. 点击创建。一个包含您的密钥的JSON文件将下载到您的计算机上。
  6. 将 JSON 文件上传到您的存储目录或任何受保护的目录。
  7. .envGOOGLE_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 发送一个 verifyReceipt 请求到 App Store,如下所示:

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 发送一个 verifyReceipt 请求到 App Store,如下所示:

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);
    }   
}

测试

待办事项:添加测试示例。