# Android
Google Play Billing provides server push notifications that let you monitor state changes for Play-managed subscriptions. By enabling real-time developer notifications, you'll receive a purchase token directly from Cloud Pub/Sub anytime there is an update to an existing subscription.
To enable this capability:
- Setup Cloud Pub/Sub using your own Google Cloud Platform (GCP) project.
- Enable real-time developer notifications for your Android app.
# Setting up Google PubSub
Cloud Pub/Sub is a fully-managed real-time messaging service allowing you to send and receive messages between independent applications. It delivers low-latency, durable messaging that helps you quickly integrate systems hosted on the Google Cloud Platform and externally.
Google Play Billing uses the Cloud Pub/Sub to publish push notifications on topics to which you subscribe.
# Create a topic
To start receiving the notifications, you need to create a topic to which the Google Play Billing should publish the notifications. To create a topic:
- Read the instructions in Create the topic.
- Use the Google Cloud Platform Console to create the topic.
# Create a Pub/Sub subscription
To receive messages published to a topic, you must create a Pub/Sub subscription to that topic. To create a Pub/Sub subscription:
- Read the Cloud Pub/Sub Subscriber Guide to determine whether to configure the subscription as either a push subscription or pull subscription. A pull subscription requires your secure backend server to initiate requests to the Cloud Pub/Sub server to retrieve messages. A push subscription requires Cloud Pub/Sub to initiate requests to your secure backend server to deliver messages.
- Read the instructions in Add a subscription.
- Use the Google Cloud Platform Console to create the subscription.
# Grant publish rights on your topic
Cloud Pub/Sub requires that you grant Google Play Billing privileges to publish notifications to your topic using the following steps:
- Open the Google Cloud Console.
- Select your project and click Pub/Sub in the left-hand navigation.
- Find your topic and open the permissions details.
4. Add the service account google-play-developer-notifications@system.gserviceaccount.com
and grant it the role of Pub/Sub Publisher.
5. Save to complete topic set up.
# Enable real-time developer notifications for your app
To enable real-time developer notifications for your app:
- Open the Google Play Console.
- Select your Android app.
- Navigate to the Development tools > Services & APIs page.
- Scroll to the Real-time developer notifications section at the bottom of the page.
In the Topic name field, enter the full Cloud Pub/Sub topic name that you configured earlier. The topic name should be in the format of
projects/{project_id}/topics/{topic_name}
whereproject_id
is the unique identifier for your project andtopic_name
is the name of the topic created earlier.Click Send Test Message to send a test message. Performing a test publish helps ensure that everything is setup and configured properly. If the test publish succeeds, a message is displayed stating that the test publish was successful. If you have a subscriber running for this topic, it should receive this test message. If the publish fails, an error is shown. Ensure that the topic name is correct and that
google-play-developer-notifications@system.gserviceaccount.com
service account has Pub/Sub Publisher access to the topic.Click Update Topic
For more information about this documentation please visit this link, Add real-time developer notifications
# Handling Webhook Notification
This explains how we handle webhook event fired from Google PubSub to our webhook URL.
- We extracted the Base64 encoded data passed from Google PubSub
- We get the subscription notification data. This is how subscription notification looks like.
[
"version" => "1.0"
"notificationType" => 3
"purchaseToken" => "bgodibjhklpmpphnonpfhkkg.AO-J1OxmovNIgvVCrG9Vz4H-S4kEhBpR--4kWevSP04I1LGKhwSnA9AluwEpuEwQa05FQpdafJwd-cYQre9grTv8hEM6Zkye1X6jXjmp6IV7VxBXfk9cs66L6-E9vNJ2bCipI6x7-F7U"
"subscriptionId" => "concha_club_1"
]
2
3
4
5
6
- We then fetch the purchase token and subscription ID (Plan ID) so we can query who's user owns the webhook.
- Then we determine what is the notification type of the webhook based on the
notificationType
. Refer to this link for the list of all notification types. - We also validate the webhook payload against the Google Play to make sure it is authorized.
- After getting all the necessary information, the webhook will be now processed based on their corresponding action.
Here is the entire code snippet for handling the webhook.
$payload = $this->webhookCall->payload;
$data = json_decode(base64_decode(Arr::get($payload, 'message.data')), true);
try {
$subscriptionNotification = Arr::get($data, 'subscriptionNotification');
if (!$subscriptionNotification) {
return;
}
$this->plan = Plan::findBySlug(Arr::get($subscriptionNotification, 'subscriptionId'));
$this->invoice = Invoice::findByTransaction(Arr::get($subscriptionNotification, 'purchaseToken'));
PlayValidator::setPackageName(Arr::get($data, 'packageName'))
->setProductId(Arr::get($subscriptionNotification, 'subscriptionId'))
->setPurchaseToken(Arr::get($subscriptionNotification, 'purchaseToken'))
->validateSubscription();
$notificationTypeId = SubscriptionNotificationType::getDescription(
Arr::get($subscriptionNotification, 'notificationType')
);
$notificationType = Str::studly(
strtolower($notificationTypeId)
);
if ($notificationType && $this->plan && $this->invoice) {
// call class function base on notification type
call_user_func(array($this, "handle$notificationType"));
}
} catch (Exception $ex) {
return;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
← Getting Started IOS →