bestys-mobile/google-play-billing 问题修复 & 功能扩展

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

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

bestys-mobile/google-play-billing

Composer 安装命令:

composer require bestys-mobile/google-play-billing

包简介

Google Play Billing

README 文档

README

Latest Version on Packagist Total Downloads

PHP Google Play Billing provides the required implementation to add Google Play's billing system to your php project.

Google Play's billing system overview

Google Play's billing system is a service that enables you to sell digital products and content in your Android app.

You can use Google Play's billing system to sell the following types of digital content:

  • One-time products: A one-time product is content that users can purchase with a single, non-recurring charge to the user's form of payment. One-time products can be either consumable or non-consumable:

    • A consumable product is one that a user consumes to receive in-app content, such as in-game currency. When a user consumes the product, your app dispenses the associated content, and the user can then purchase the item again.
    • A non-consumable product is a product that is purchased only once to provide a permanent benefit. Examples include premium upgrades and level packs.
  • Subscriptions: A subscription is a product that provides access to content on a recurring basis . Subscriptions renew automatically until they're canceled. Examples of subscriptions include access to online magazines and music streaming services.

Installation

Install the package via composer:

composer require imdhemy/google-play-billing

Configuration

Requests to the Google Play Developer API, requires authentication and scopes. To authenticate your machine create a service account, then set the environment variable GOOGLE_APPLICATION_CREDENTIALS

  1. In the Cloud Console, go to the Create service account key page.
  2. From the Service account list, select New service account.
  3. In the Service account name field, enter a name.
  4. From the Role list, select Project > Owner.
  5. Click Create. A JSON file that contains your key downloads to your computer.
  6. You can set the environment variable by:
$path = 'path/to/google-app-credentials.json';
putenv(sprintf("GOOGLE_APPLICATION_CREDENTIALS=%s", $path));

Sell products

With any type of your products, you can use the REST api to:

  1. acknowledge: Acknowledges a purchase of an inapp item.
  2. get: Checks the purchase and consumption status of an inapp item.

Acknowledge a product

To acknowledge a product, you need a Client, which can be created by ClientFactory using the android publisher scope , then create an instance of the Bestys\Products\Product class, finally trigger the acknowledge() method.

use Bestys\GooglePlay\ClientFactory;
use Bestys\GooglePlay\Products\Product;

$client = ClientFactory::create([ClientFactory::SCOPE_ANDROID_PUBLISHER]);
$product = new Product($client, 'com.example.package.name', 'productId', 'Purchase_Token');
$product->acknowledge();

Get the consumption state of a product

To check the purchase and consumption status of an inapp item, use the same steps used to acknowledge a product , but trigger the get() method not the acknowledge one.

use Bestys\GooglePlay\ClientFactory;
use Bestys\GooglePlay\Products\Product;

$client = ClientFactory::create([ClientFactory::SCOPE_ANDROID_PUBLISHER]);
$product = new Product($client, 'com.example.package.name', 'productId', 'Purchase_Token');
$resource = $product->get();

The get() method returns a Bestys\GooglePlay\Products\ProductPurchase object, which contains the following methods:

Method Description
getKind ...
getPurchaseTime ...
getPurchaseState ...
getConsumptionState ...
getDeveloperPayload ...
getOrderId ...
getAcknowledgementState ...
getPurchaseToken ...
getProductId ...
getQuantity ...
getObfuscatedExternalAccountId ...
getObfuscatedExternalProfileId ...
getRegionCode ...

Sell subscriptions

This section describes how to handle subscription lifecycle events, such as renewals and expiration.

Life of a purchase

Here's a typical purchase flow for a one-time purchase or a subscription.

  • Show the user what they can buy.
  • Launch the purchase flow for the user to accept the purchase.
  • Verify the purchase on your server.
  • Give content to the user, and acknowledge delivery of the content. Optionally, mark the item as consumed so that the user can buy the item again.

Subscriptions automatically renew until they are canceled. A subscription can go through the following states:

  • Active: User is in good standing and has access to the subscription.
  • Cancelled: User has cancelled but still has access until expiration.
  • In grace period: User experienced a payment issue, but still has access while Google is retrying the payment method.
  • On hold: User experienced a payment issue, and no longer has access while Google is retrying the payment method.
  • Paused: User paused their access, and does not have access until they resume.
  • Expired: User has cancelled and lost access to the subscription. The user is considered churned at expiration.

Handling the subscription lifecycle

A subscription can go through various state changes throughout its lifecycle and your app needs to respond to each change . To check the subscriber's state, your app can query using the Purchases.subscriptions:get (provided by this package) in the Google Play Developer API.

State Is returned expiryTimeMillis paymentState autoRenewing
Active Yes Future 1 (Payment Received) True
Cancelled Yes Future 1 (Payment Received) False
In grace period Yes Future 0 (Payment Pending) True
On hold Yes Past 0 (Payment Pending) True
Paused Yes Past 1 (Payment Received) True
Expired Yes Past 1 (Payment Received) False

The following methods are available to be used on a subscription:

  1. acknowledge: Acknowledges a subscription purchase.
  2. get: Checks whether a user's subscription purchase is valid and returns its expiry time.
  3. cancel: Cancels a user's subscription purchase.
  4. defer: Defers a user's subscription purchase until a specified future expiration time.
  5. refund: Refunds a user's subscription purchase, but the subscription remains valid until its expiration time and it will continue to recur.
  6. revoke: Refunds and immediately revokes a user's subscription purchase.

N.B. The stroked method are not implemented yet. help us with your contributions 😅.

use Bestys\GooglePlay\ClientFactory;
use Bestys\GooglePlay\Subscriptions\Subscription;

$client = ClientFactory::create([ClientFactory::SCOPE_ANDROID_PUBLISHER]);
$subscription = new Subscription($client, 'com.example.package.name', 'subscriptionId', 'Purchase_Token');
$subscription->acknowledge();
$resource = $subscription->get(); // Bestys\GooglePlay\Subscriptions\SubscriptionPurchase

The Following methods are available in Bestys\GooglePlay\Subscriptions\SubscriptionPurchase Object

Method Description
getKind ...
isAutoRenewing ...
getPriceCurrencyCode ...
getPriceAmountMicros ...
getCountryCode ...
getDeveloperPayload ...
getOrderId ...
getLinkedPurchaseToken ...
getEmailAddress ...
getGivenName ...
getProfileId ...
getExternalAccountId ...
getObfuscatedExternalAccountId ...
getObfuscatedExternalProfileId ...
getStartTime ...
getExpiryTime ...
getAutoResumeTime ...
getIntroductoryPriceInfo ...
getPriceChange ...
getCancellation ...
getPromotionType ...
getAcknowledgementState ...

Your app should listen for state changes using Real-time developer notifications to ensure state is kept in-sync. A SubscriptionNotification is sent for events affecting subscription state such as renewals and cancellations. You need to call the developer API after receiving a Real-time developer notifications to get the complete status and update your own backend state. These notifications tell you only that the subscription state changed. They do not give you complete information about overall subscription status.

Note: Due to quota restrictions, it is not recommended to check state by polling the Google Play Developer API at regular intervals instead of leveraging Real-time developer notifications.

Your app needs to handle the state changes that are described in the following table:

State Notification Value
New subscriptions SUBSCRIPTION_PURCHASED 4
Renewals SUBSCRIPTION_RENEWED 2
Expiration SUBSCRIPTION_EXPIRED 13
Cancellations SUBSCRIPTION_CANCELLED
Revocations SUBSCRIPTION_REVOKED 3
Account hold SUBSCRIPTION_ON_HOLD 5
Grace period SUBSCRIPTION_IN_GRACE_PERIOD 6
Paused subscriptions SUBSCRIPTION_PAUSED 10
Restorations ... ...
Upgrades, downgrades, and re-sign-ups ... ...

After receiving a real-time developer notification, you can parse its contents as follows:

use Bestys\GooglePlay\DeveloperNotifications\DeveloperNotification;
$data = 'the_received_base_64_encoded_string';
$developerNotification = DeveloperNotification::parse($data); // Bestys\GooglePlay\DeveloperNotifications\DeveloperNotification
$subscriptionNotification = $developerNotification->getSubscriptionNotification(); // Bestys\GooglePlay\DeveloperNotifications\SubscriptionNotification

The Following methods are available in Bestys\GooglePlay\DeveloperNotifications\DeveloperNotification

Method Description
parse ...
getType ...
getVersion ...
getPackageName ...
getEventTime ...
getSubscriptionNotification ...
getOneTimeProductNotification ...
getTestNotification ...

The Following methods are available in Bestys\GooglePlay\DeveloperNotifications\SubscriptionNotification

Method Description
getVersion ...
getNotificationType ...
getPurchaseToken ...
getSubscriptionId ...

bestys-mobile/google-play-billing 适用场景与选型建议

bestys-mobile/google-play-billing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bestys-mobile/google-play-billing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bestys-mobile/google-play-billing 我们能提供哪些服务?
定制开发 / 二次开发

基于 bestys-mobile/google-play-billing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-01-06