tillbell 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a61824e3e025f8e1614137ca1139ed6d76aff9d201a40960704824618926e05
4
- data.tar.gz: ac1bdc03f6ef383c121b02ff4b03c8d725f65e3b8184a3c058c65e0aa5c24a12
3
+ metadata.gz: 12dfa4c6d7a3864417897be9a747fefbe25e4b1507665cea18427357bde1d6e6
4
+ data.tar.gz: 30ad262de35244e9262452acce2734ee92245274bd66f6f74205f798d5e01695
5
5
  SHA512:
6
- metadata.gz: 0ca823bfe9a3749f16d0af8103f580cc19cc547eb3c1f9ff1c6f9a76d81de686342bfc3b1656cb245ad3fdf4d37a5febe3a4b82526aa8ba7fe1f4e06febae0a0
7
- data.tar.gz: 833739c0d7431f22a909c83e3b48e19365163554165d3c5b73b2ef538d8fd96cedd753de6311697f260feae454bb1b04e171c7f8fd524be83cb0ff8272625593
6
+ metadata.gz: 5d140d39754930be99c4c93de83e51b2947e609e3bff3b0629bad2a95421e806c83e14ce08df900bb78049e869f23802e6d0692506c012feaa58d36649a56de2
7
+ data.tar.gz: e8118449e7d79da14d3c6b7fc40b0e23aaf20c342343e8f1e77928b15ad4d3ed2a4a6bd14b3915ebb5aa9af1616e5fdc38dcbb1f2fc70f2c423eb7d399599257
data/CHANGELOG.md CHANGED
@@ -6,6 +6,23 @@ All notable changes to this project are documented in this file. The format foll
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.0] - 2026-10-30
10
+
11
+ ### Added
12
+
13
+ - `bin/rails tillbell:check` (`Tillbell::Check`): checks the package name, the service account
14
+ key, that the engine is mounted and the notification sender, without printing a secret.
15
+ `ONLINE=1` also asks Google (the key, its rights, the subscriptions of the Play Console);
16
+ `PLANS=premium:monthly,premium:yearly` checks the plans the app sells exist and are active.
17
+ - The install generator prints the credentials template and the next steps.
18
+
19
+ ### Changed
20
+
21
+ - README, "Google Play setup": rewritten for someone who has never touched Google Play's
22
+ consoles. What Google needs and why, a credentials template to fill in, a checklist, every
23
+ step in the Play Console and the Cloud Console, the check, a test purchase and a
24
+ troubleshooting table.
25
+
9
26
  ## [0.2.0] - 2026-10-30
10
27
 
11
28
  ### Added
data/README.md CHANGED
@@ -64,16 +64,18 @@ Assuming a Rails 8 app with a `User` model and Pay installed:
64
64
  end
65
65
  ```
66
66
 
67
- 3. Fill in `config/initializers/tillbell.rb`:
67
+ 3. Fill in `config/initializers/tillbell.rb`, and put the two values Google gives you in your credentials (the [Google Play setup](#google-play-setup) below has the template and every click):
68
68
 
69
69
  ```ruby
70
70
  Tillbell.configure do |config|
71
71
  config.google.package_name = "com.example.app"
72
72
  config.google.service_account_json = Rails.application.credentials.dig(:tillbell, :google_service_account)
73
- config.google.pubsub_service_account_email = "pubsub-push@my-project.iam.gserviceaccount.com"
73
+ config.google.pubsub_service_account_email = Rails.application.credentials.dig(:tillbell, :pubsub_service_account_email)
74
74
  end
75
75
  ```
76
76
 
77
+ Then `bin/rails tillbell:check` tells you what is still missing.
78
+
77
79
  4. Render a paywall:
78
80
 
79
81
  ```erb
@@ -92,7 +94,7 @@ Assuming a Rails 8 app with a `User` model and Pay installed:
92
94
  current_user.pay_subscriptions.active.any?
93
95
  ```
94
96
 
95
- Then add [tillbell-android](https://github.com/pmakerhq/tillbell-android) to your Hotwire Native app and follow the [Google Play setup](#google-play-setup).
97
+ Then add [tillbell-android](https://github.com/pmakerhq/tillbell-android) to your Hotwire Native app, and do the [Google Play setup](#google-play-setup) if you have not yet: it is the part that happens on Google's side.
96
98
 
97
99
  ## How it works
98
100
 
@@ -162,10 +164,12 @@ end
162
164
  Tillbell.configure do |config|
163
165
  config.google.package_name = "com.example.app"
164
166
  config.google.service_account_json = Rails.application.credentials.dig(:tillbell, :google_service_account)
165
- config.google.pubsub_service_account_email = "pubsub-push@my-project.iam.gserviceaccount.com"
167
+ config.google.pubsub_service_account_email = Rails.application.credentials.dig(:tillbell, :pubsub_service_account_email)
166
168
  end
167
169
  ```
168
170
 
171
+ The values marked "credentials" below come from Google: the [Google Play setup](#google-play-setup) says where to find each one, and gives the template to fill in. `bin/rails tillbell:check` verifies them.
172
+
169
173
  | Setting | Default | Description |
170
174
  |---|---|---|
171
175
  | `google.package_name` | (required) | Application id of the Android app. |
@@ -337,14 +341,183 @@ Events go through Pay's webhook delegator, so `Pay::Webhooks.delegator.subscribe
337
341
 
338
342
  ## Google Play setup
339
343
 
340
- 1. **Service account.** In Google Cloud, create a service account and a JSON key, and enable the *Google Play Android Developer API*. In the Play Console, under *Users and permissions*, invite the service account with *View financial data* and *Manage orders and subscriptions*. Store the key in your credentials as `tillbell.google_service_account`.
341
- 2. **Pub/Sub topic.** Create a topic, and grant `google-play-developer-notifications@system.gserviceaccount.com` the *Pub/Sub Publisher* role on it. In the Play Console, under *Monetization setup*, enter the topic name for real-time developer notifications.
342
- 3. **Push subscription.** Create a push subscription on the topic:
343
- - endpoint: `https://your-app.example.com/tillbell/notifications/google_play`;
344
- - *Enable authentication* turned on, with a service account of your choice. Set its e-mail as `google.pubsub_service_account_email`;
345
- - audience: leave it empty (it defaults to the endpoint URL), or set the same value as `google.pubsub_audience`.
346
- 4. **Test.** Click *Send test notification* in the Play Console. The endpoint answers 200 and logs `[Tillbell] Google Play notification test_notification`.
347
- 5. **License testers.** Add your testers in the Play Console, and install the app from a Play testing track to buy with test cards.
344
+ Tillbell needs two things from Google: **permission to read and acknowledge your customers' purchases**, and **a way to hear about renewals, cancellations and refunds**. Setting that up takes about 30 minutes of clicking in two Google websites:
345
+
346
+ | Website | What lives there | Called here |
347
+ |---|---|---|
348
+ | [Google Play Console](https://play.google.com/console) | Your app, your subscriptions, who may access them | **Play Console** |
349
+ | [Google Cloud Console](https://console.cloud.google.com) | Service accounts (the identities your server uses) and Pub/Sub (the mailbox Google drops notifications in) | **Cloud Console** |
350
+
351
+ You end up with **two values to give your app**: the JSON key of a service account, and the e-mail address of a second service account.
352
+
353
+ > [!NOTE]
354
+ > Google's menus move now and then. The names below are the ones to look for; if one has changed, search the console for it.
355
+
356
+ ### Before you start
357
+
358
+ - A Google Play developer account, with your app created in the Play Console.
359
+ - A payments profile (merchant account) linked to the developer account: Google asks for it when you create a paid product.
360
+ - **A build of your app that includes the Google Play Billing Library, published to a testing track** (the Internal testing track is enough). Google does not let you create subscriptions until it has seen one. [tillbell-android](https://github.com/pmakerhq/tillbell-android) brings the Billing Library, so upload a build of your Hotwire Native app that includes it.
361
+ - Your Rails app deployed on a public HTTPS address (Google must be able to call it).
362
+
363
+ ### The template to fill in
364
+
365
+ Put the two values in your Rails credentials (`bin/rails credentials:edit`, or `bin/rails credentials:edit --environment production`), under the `tillbell` key. The generated initializer reads them from there. Replace everything between `<` and `>`:
366
+
367
+ ```yaml
368
+ tillbell:
369
+ # Step 2: the whole JSON key file of the service account that calls Google.
370
+ # Keep the "|" and the two-space indentation, or the key is unreadable.
371
+ google_service_account: |
372
+ {
373
+ "type": "service_account",
374
+ "project_id": "<your-cloud-project>",
375
+ "private_key_id": "...",
376
+ "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
377
+ "client_email": "<tillbell-api>@<your-cloud-project>.iam.gserviceaccount.com",
378
+ "client_id": "...",
379
+ "token_uri": "https://oauth2.googleapis.com/token"
380
+ }
381
+ # Step 4: the e-mail address of the service account Pub/Sub signs its notifications with.
382
+ pubsub_service_account_email: <tillbell-push>@<your-cloud-project>.iam.gserviceaccount.com
383
+ ```
384
+
385
+ Two more settings live in the initializer, not in the credentials: `config.google.package_name` (the application id of your Android app, for example `com.example.app`) and, only if a proxy changes the address Rails sees, `config.google.pubsub_audience` (see [Configuration](#configuration)).
386
+
387
+ `rails generate tillbell:install` prints this template when it finishes.
388
+
389
+ ### The checklist
390
+
391
+ Copy it into your tracker and tick it as you go. Each step is explained below.
392
+
393
+ ```markdown
394
+ - [ ] 0. Developer account, app created, payments profile, a build with the Billing Library on a testing track
395
+ - [ ] 1. Subscriptions created in the Play Console, base plan active (note the product and base plan ids)
396
+ - [ ] 2. Service account "tillbell-api" created, API enabled, JSON key downloaded
397
+ - [ ] 3. That service account invited in the Play Console with the two permissions (then wait, up to a day)
398
+ - [ ] 4. Service account "tillbell-push" created (no role, no key), e-mail copied
399
+ - [ ] 5. Pub/Sub topic created, Google Play allowed to publish to it
400
+ - [ ] 6. Push subscription created: your URL, authentication on, "tillbell-push"
401
+ - [ ] 7. Credentials filled in and deployed, `bin/rails tillbell:check` passes
402
+ - [ ] 8. Topic connected in the Play Console, test message sent and received
403
+ - [ ] 9. License testers added, test purchase made
404
+ ```
405
+
406
+ ### 1. Create your subscriptions (Play Console)
407
+
408
+ *Monetize with Play* → *Products* → *Subscriptions* → *Create subscription*.
409
+
410
+ - The **product ID** names the subscription (`premium`). You pass it as `product_id:` to `plan_option`.
411
+ - Each subscription has one or more **base plans** (`monthly`, `yearly`), with their own ID, billing period and price. You pass it as `base_plan_id:`. A base plan must be **Active** to be sold.
412
+ - Neither ID can be changed later, so pick them carefully.
413
+
414
+ ### 2. Create the service account that calls Google (Cloud Console)
415
+
416
+ A *service account* is an identity for a program instead of a person. Your server uses this one to ask Google "what is the state of this purchase?" and to acknowledge it.
417
+
418
+ 1. Pick (or create) a Cloud project. Any project works: what ties the service account to your app is the invitation of step 3.
419
+ 2. *APIs & Services* → *Library* → search **Google Play Android Developer API** → *Enable*.
420
+ 3. *IAM & Admin* → *Service Accounts* → *Create service account*. Name it `tillbell-api`. Give it **no role** here.
421
+ 4. Open it → *Keys* → *Add key* → *Create new key* → **JSON**. A file downloads. **Google shows it only once**: that file is the `google_service_account` of the template. Treat it like a password, and never commit it.
422
+
423
+ Its e-mail address (`client_email` in the file) is needed in the next step. If *Create new key* is greyed out or refused, an organization policy blocks service account keys: see [If something is off](#if-something-is-off).
424
+
425
+ ### 3. Let that service account into the Play Console
426
+
427
+ The Cloud Console knows the account exists. The Play Console must also allow it.
428
+
429
+ *Users and permissions* → *Invite new users* → paste the service account's e-mail address, then open the **App permissions** tab → *Add app* → your app, and tick:
430
+
431
+ - **View financial data** (the account-level tab calls it *View financial data, orders and cancellation survey responses*);
432
+ - **Manage orders and subscriptions**.
433
+
434
+ Send the invitation. A service account does not need to accept it, but Google can take **up to a day** to apply the permissions, so a `401` or `403` in step 7 right after this step is normal.
435
+
436
+ ### 4. Create the account that signs notifications (Cloud Console)
437
+
438
+ When Google tells your server about a renewal, your server must know the call really comes from Google's Pub/Sub. Pub/Sub proves it by signing each call with a service account of your choice. You create that one now, and give your app its e-mail address so it can recognize the signature.
439
+
440
+ *IAM & Admin* → *Service Accounts* → *Create service account*, named `tillbell-push`. It needs **no role and no key**: it is only a name Pub/Sub signs with. Copy its e-mail address: that is `pubsub_service_account_email` of the template.
441
+
442
+ ### 5. Create the Pub/Sub topic (Cloud Console)
443
+
444
+ A *topic* is the mailbox Google Play drops notifications in.
445
+
446
+ 1. *Pub/Sub* → *Topics* → *Create topic* (enable the Pub/Sub API if asked). Name it `play-notifications`. Note its full name: `projects/<your-cloud-project>/topics/play-notifications`.
447
+ 2. Open the topic → *Permissions* → *Add principal*: `google-play-developer-notifications@system.gserviceaccount.com`, role **Pub/Sub Publisher**. That is Google Play itself, allowed to write in your mailbox.
448
+
449
+ ### 6. Make the topic call your server (Cloud Console)
450
+
451
+ A *push subscription* forwards each message to a URL.
452
+
453
+ Open the topic → *Create subscription*:
454
+
455
+ - **Delivery type**: *Push*.
456
+ - **Endpoint URL**: `https://<your-domain>/tillbell/notifications/google_play` (the path follows where you mounted the engine). It must be public HTTPS, with **no query string and no trailing slash**: the audience below is compared to it exactly. Behind a proxy that terminates TLS, Rails must know the request was HTTPS (`X-Forwarded-Proto`).
457
+ - **Enable authentication**: ticked, with the service account of step 4 (`tillbell-push`).
458
+ - **Audience**: leave empty. It then defaults to the endpoint URL, which is what Tillbell expects.
459
+
460
+ Two permissions can get in the way:
461
+
462
+ - If Google refuses to create the subscription with authentication, your own Google account needs the **Service Account User** role on `tillbell-push`.
463
+ - Pub/Sub's own service agent (`service-<project-number>@gcp-sa-pubsub.iam.gserviceaccount.com`) must be allowed to create tokens for `tillbell-push`: give it the **Service Account Token Creator** role on that account. Google's console steps say to check it, and projects that enabled Pub/Sub long ago need it explicitly. Without it, pushes fail silently.
464
+
465
+ ### 7. Fill in the credentials, deploy and check
466
+
467
+ Put the two values in your credentials (see the template), set `config.google.package_name`, and **deploy**: the next step needs your server to be able to accept Google's notifications. Then run the check where the credentials are available. The simplest is on the server; locally, `RAILS_ENV=production` boots your production environment, which may need its other environment variables (database, `SECRET_KEY_BASE`) and your production key.
468
+
469
+ ```sh
470
+ bin/rails tillbell:check # reads your configuration, asks nobody
471
+ ONLINE=1 PLANS=premium:monthly,premium:yearly bin/rails tillbell:check # also asks Google
472
+ ```
473
+
474
+ `PLANS` lists the `product_id:base_plan_id` pairs your app sells; each must exist in the Play Console with an active base plan. Without `PLANS`, `ONLINE=1` lists the subscriptions Google knows. With `ONLINE=1` the check also asks Google to read a purchase that cannot exist: "unknown purchase" means the service account may read purchases, a permission error means it may not. It changes nothing on Google and never prints a secret.
475
+
476
+ What it cannot try is acknowledging a purchase, which needs a real one: step 9 does. A healthy run:
477
+
478
+ ```
479
+ OK Android package name: com.example.app
480
+ OK Service account key (config.google.service_account_json): tillbell-api@my-project.iam.gserviceaccount.com
481
+ OK Engine mounted: /tillbell/purchases and /tillbell/notifications/google_play
482
+ OK Notification sender (config.google.pubsub_service_account_email): tillbell-push@my-project.iam.gserviceaccount.com
483
+ OK Notification audience: default: the notification URL as Rails sees it
484
+ OK Reading purchases: allowed (Google answered that the test token is unknown, as expected)
485
+ OK Subscription premium (base plan monthly): active
486
+ OK Subscription premium (base plan yearly): active
487
+
488
+ Everything is in place.
489
+ ```
490
+
491
+ Add `ONLINE=1` to your deploy checklist: it is the quickest way to learn that a key was revoked.
492
+
493
+ ### 8. Connect Google Play to the topic (Play Console)
494
+
495
+ *Monetize with Play* → *Monetization setup* → *Real-time developer notifications*: tick **Enable real-time notifications**, enter the full topic name from step 5, choose the notification content that covers **subscriptions and all voided purchases** (Tillbell handles both), save, then click **Send test message**.
496
+
497
+ The Play Console's success message only means the message reached **your topic**. Whether it reached your server is shown by your logs: `[Tillbell] Google Play notification test_notification`, and a 200 answer. A 401 there is the sender or the audience (see below). Without these notifications your app still sells subscriptions, but renewals and cancellations are never seen, and a purchase your server did not acknowledge is refunded by Google after three days.
498
+
499
+ ### 9. Test a purchase
500
+
501
+ 1. Play Console → *Settings* → *License testing*: add the Google accounts of your testers. They buy with test payment methods (pick the test card in the purchase sheet) and are not charged. Test subscriptions renew much faster than real ones, so you can watch a renewal and a cancellation: a monthly subscription renews every 5 minutes, a yearly one every 30, at most 6 times.
502
+ 2. Install your app on a device signed in with a tester account. License testers can install a build straight from their computer as long as the package name matches, or from the *Internal testing* opt-in link (a release on a testing track can take a few hours to reach testers).
503
+ 3. Open the paywall, buy, and watch `Pay::GooglePlay::Subscription` appear in your database. Then check that Google did not mark the purchase as unacknowledged: it should stay in your database as active after a few minutes.
504
+
505
+ ### If something is off
506
+
507
+ `bin/rails tillbell:check` names the cause of most problems. The rest:
508
+
509
+ | What you see | Likely cause | What to do |
510
+ |---|---|---|
511
+ | `not JSON`, or `private_key is missing or unreadable` | The key was pasted with a wrong indentation, or cut | Paste the whole key file again, with the `\|` and two spaces of indentation |
512
+ | `ONLINE=1`: *not authorized (401)* or *not allowed (403)* | Step 3 is missing or not applied yet, the permissions are not the two above, or (403) the API is not enabled in the project of the key | Check the invitation and the API (step 2.2), and wait up to a day |
513
+ | `ONLINE=1`: *not found (404)* | Wrong package name, or no such product in the Play Console | Compare with the Play Console |
514
+ | `ONLINE=1`: *key refused* | The key was deleted or rotated, or belongs to another project | Create a new JSON key and replace it |
515
+ | *Create new key* is greyed out or refused | An organization policy blocks service account keys (common on organizations created recently) | An organization administrator must allow key creation for this project |
516
+ | `Google was not asked` | An offline check failed | Fix the failures above it, then run again |
517
+ | The test message answers 401 in your logs | `pubsub_service_account_email` is not the account of step 4, or the audience differs | Compare the e-mails; if a proxy changes the URL Rails sees, set `config.google.pubsub_audience` to the same value as the subscription's *Audience* |
518
+ | Nothing arrives at your server | The endpoint URL is wrong, not public HTTPS, or the topic is not the one in the Play Console; or Pub/Sub's service agent cannot create tokens (step 6) | Check steps 5 to 8; the subscription's *Unacked messages* graph in the Cloud Console shows whether Google writes |
519
+ | The buy button stays disabled | The app does not register the `tillbell` component, or this is a browser | Use the app with [tillbell-android](https://github.com/pmakerhq/tillbell-android) |
520
+ | A purchase works, then Google refunds it after three days | Your server did not acknowledge it: the purchase endpoint failed | Look at your logs for the purchase request; run `bin/rails tillbell:check` |
348
521
 
349
522
  ## Security
350
523
 
@@ -358,6 +531,8 @@ Found a vulnerability? Please report it privately, as described in [SECURITY.md]
358
531
 
359
532
  ## Troubleshooting
360
533
 
534
+ Problems on Google's side (credentials, permissions, notifications) have their own table at the end of the [Google Play setup](#google-play-setup), and `bin/rails tillbell:check` names the cause of most of them.
535
+
361
536
  **The paywall buttons stay disabled.** The page is not running inside an app that registers the `tillbell` bridge component (a browser, or an app without [tillbell-android](https://github.com/pmakerhq/tillbell-android)). This is expected: the fallback is shown instead.
362
537
 
363
538
  **`POST /tillbell/purchases` answers 401.** `current_owner` returned `nil`: the request is not signed in, or `current_owner` does not match your authentication.
@@ -7,7 +7,7 @@ module Tillbell
7
7
  class InstallGenerator < Rails::Generators::Base
8
8
  source_root File.expand_path("templates", __dir__)
9
9
 
10
- desc "Creates the Tillbell initializer, mounts the engine and copies the paywall Stimulus controller."
10
+ desc "Creates the Tillbell initializer, mounts the engine, copies the paywall Stimulus controller and prints the Google Play credentials template."
11
11
 
12
12
  def create_initializer
13
13
  copy_file "initializer.rb", "config/initializers/tillbell.rb"
@@ -29,6 +29,11 @@ module Tillbell
29
29
  "npm install @hotwired/hotwire-native-bridge", :yellow
30
30
  end
31
31
  end
32
+
33
+ # The part that happens on Google's side, with the credentials to fill in.
34
+ def show_next_steps
35
+ readme "next_steps.txt"
36
+ end
32
37
  end
33
38
  end
34
39
  end
@@ -1,5 +1,9 @@
1
1
  # Tillbell: Google Play subscriptions stored as Pay subscriptions.
2
2
  # See https://github.com/pmakerhq/tillbell-rails#configuration
3
+ #
4
+ # The two values below come from Google: the README's "Google Play setup" has the credentials
5
+ # template and every click. Once they are in your credentials, check everything with
6
+ # bin/rails tillbell:check (ONLINE=1 also asks Google)
3
7
  Tillbell.configure do |config|
4
8
  # Application id of the Android app.
5
9
  config.google.package_name = "com.example.app"
@@ -0,0 +1,25 @@
1
+
2
+ Tillbell is installed. What is left happens on Google's side.
3
+
4
+ 1. Put the two values Google gives you in your credentials
5
+ (bin/rails credentials:edit, or --environment production):
6
+
7
+ tillbell:
8
+ # The whole JSON key file of the service account that calls Google.
9
+ # Keep the "|" and the two-space indentation.
10
+ google_service_account: |
11
+ { "type": "service_account", "client_email": "<tillbell-api>@<project>.iam.gserviceaccount.com", ... }
12
+ # The service account Pub/Sub signs its notifications with.
13
+ pubsub_service_account_email: <tillbell-push>@<project>.iam.gserviceaccount.com
14
+
15
+ 2. Set config.google.package_name in config/initializers/tillbell.rb.
16
+
17
+ 3. Follow "Google Play setup" for every click (Play Console, service accounts, Pub/Sub). Allow
18
+ up to a day for Google to apply the permissions you grant:
19
+ https://github.com/pmakerhq/tillbell-rails#google-play-setup
20
+
21
+ 4. Check it:
22
+
23
+ bin/rails tillbell:check
24
+ ONLINE=1 PLANS=premium:monthly bin/rails tillbell:check # also asks Google
25
+
@@ -0,0 +1,17 @@
1
+ namespace :tillbell do
2
+ desc "Check the Tillbell configuration. ONLINE=1 also asks Google; PLANS=premium:monthly,premium:yearly checks those plans exist."
3
+ task check: :environment do
4
+ icons = { ok: "OK ", warn: "WARN", fail: "FAIL" }
5
+ online = !%w[0 false no].include?(ENV["ONLINE"].to_s.strip.downcase) && ENV["ONLINE"].present?
6
+ plans = ENV["PLANS"].to_s.split(",").map(&:strip).reject(&:empty?)
7
+ check = Tillbell::Check.new(online: online, plans: plans)
8
+
9
+ check.call.each do |result|
10
+ puts "#{icons.fetch(result.status)} #{result.label}#{": #{result.detail}" if result.detail}"
11
+ end
12
+ puts "\nGoogle was not asked: run again with ONLINE=1 to check the key, its rights and your subscriptions." unless online
13
+
14
+ abort "\nSomething will not work: see the README, \"Google Play setup\"." unless check.ok?
15
+ puts "\nEverything is in place."
16
+ end
17
+ end
@@ -0,0 +1,234 @@
1
+ require "openssl"
2
+
3
+ module Tillbell
4
+ # Checks that the configuration Tillbell needs is complete, and optionally that Google
5
+ # accepts it, so an app knows whether purchases and notifications can work before a
6
+ # customer tries. Run by +bin/rails tillbell:check+ (see the README, "Google Play setup").
7
+ #
8
+ # It changes nothing and never shows a secret: only the key names and the e-mail address
9
+ # of the service accounts.
10
+ #
11
+ # check = Tillbell::Check.new(online: true, plans: [ "premium:monthly", "premium:yearly" ])
12
+ # check.call.each { |result| puts "#{result.status} #{result.label} #{result.detail}" }
13
+ # check.ok? # false when something will not work
14
+ class Check
15
+ # One line of the report. +status+ is +:ok+, +:warn+ (worth a look) or +:fail+ (it will
16
+ # not work).
17
+ Result = Data.define(:status, :label, :detail)
18
+
19
+ # The shape of a Google Cloud service account e-mail address.
20
+ SERVICE_ACCOUNT_EMAIL = /\A[^@\s]+@[^@\s]+\.iam\.gserviceaccount\.com\z/
21
+
22
+ # [online] Also ask Google (Android Publisher API): the key, its rights and the
23
+ # subscriptions of the Play Console.
24
+ # [plans] Plans the app sells, as +"product_id:base_plan_id"+ strings or
25
+ # +{ product_id:, base_plan_id: }+ hashes. With +online+, each must exist in the
26
+ # Play Console with an active base plan. Without plans, +online+ lists what the
27
+ # Play Console has.
28
+ def initialize(online: false, plans: [])
29
+ @online = online
30
+ @plans = Array(plans).map { |plan| normalize(plan) }
31
+ @results = []
32
+ end
33
+
34
+ # Runs every check and returns the results, in the order to read them.
35
+ def call
36
+ @results = []
37
+ check_package_name
38
+ key_email = check_service_account_json
39
+ check_engine_mount
40
+ check_pubsub(key_email)
41
+ if @online && ok?
42
+ check_online
43
+ elsif @online
44
+ add :warn, "Google was not asked", "fix the failures above first, then run again"
45
+ end
46
+ @results
47
+ end
48
+
49
+ # True when nothing failed (warnings are fine).
50
+ def ok? = @results.none? { |result| result.status == :fail }
51
+
52
+ private
53
+
54
+ def google = Tillbell.google
55
+
56
+ def add(status, label, detail = nil)
57
+ @results << Result.new(status, label, detail)
58
+ nil
59
+ end
60
+
61
+ def normalize(plan)
62
+ return plan.symbolize_keys.slice(:product_id, :base_plan_id) if plan.respond_to?(:symbolize_keys)
63
+
64
+ product_id, base_plan_id = plan.to_s.split(":", 2)
65
+ { product_id: product_id, base_plan_id: base_plan_id.presence }
66
+ end
67
+
68
+ def check_package_name
69
+ if google.package_name.present?
70
+ add :ok, "Android package name", google.package_name
71
+ else
72
+ add :fail, "Android package name", "config.google.package_name is not set"
73
+ end
74
+ end
75
+
76
+ # Returns the e-mail address of the service account of the key, when it could be read.
77
+ def check_service_account_json
78
+ label = "Service account key (config.google.service_account_json)"
79
+ raw = google.service_account_json
80
+ return add(:fail, label, "not set: put the key file in your credentials") if raw.blank?
81
+
82
+ key = raw.is_a?(String) ? JSON.parse(raw) : raw
83
+ return add(:fail, label, "not a key file: expected a JSON object") unless key.respond_to?(:to_h) && !key.is_a?(Array)
84
+
85
+ key = key.to_h.stringify_keys
86
+ problems = []
87
+ problems << "\"type\" is not service_account" unless key["type"] == "service_account"
88
+ problems << "\"client_email\" is missing" if key["client_email"].blank?
89
+ problems << "\"private_key\" is missing or unreadable" unless private_key?(key["private_key"])
90
+ return add(:fail, label, problems.join(", ")) if problems.any?
91
+
92
+ add :ok, label, key["client_email"]
93
+ key["client_email"]
94
+ rescue JSON::ParserError
95
+ add :fail, label, "not JSON: paste the whole key file as Google gave it (mind the YAML indentation)"
96
+ end
97
+
98
+ # A key that parses: a mangled one (lost line breaks, truncated) is caught here, offline.
99
+ def private_key?(pem)
100
+ OpenSSL::PKey.read(pem.to_s)
101
+ true
102
+ rescue OpenSSL::PKey::PKeyError
103
+ false
104
+ end
105
+
106
+ def check_engine_mount
107
+ mount = Rails.application.routes.routes.find { |route| route.app.try(:app) == Tillbell::Engine }
108
+ if mount
109
+ add :ok, "Engine mounted", "#{mount.path.spec}/purchases and #{mount.path.spec}/notifications/google_play"
110
+ else
111
+ add :fail, "Engine mounted", "add `mount Tillbell::Engine, at: \"/tillbell\"` to config/routes.rb"
112
+ end
113
+ end
114
+
115
+ def check_pubsub(key_email)
116
+ label = "Notification sender (config.google.pubsub_service_account_email)"
117
+ email = google.pubsub_service_account_email
118
+ if email.blank?
119
+ add :fail, label, "not set: every notification is refused with 401, so renewals and cancellations are never synced"
120
+ elsif !SERVICE_ACCOUNT_EMAIL.match?(email)
121
+ add :fail, label, "#{email} does not look like a service account (name@project.iam.gserviceaccount.com)"
122
+ elsif email == key_email
123
+ add :warn, label, "#{email} also calls the Google Play API: prefer a dedicated account with no role"
124
+ else
125
+ add :ok, label, email
126
+ end
127
+
128
+ add :ok, "Notification audience", google.pubsub_audience.presence || "default: the notification URL as Rails sees it"
129
+ end
130
+
131
+ # Only reads: nothing is created, changed or acknowledged on Google.
132
+ def check_online
133
+ service = Tillbell::GooglePlay.service
134
+ return unless check_purchase_access(service)
135
+
136
+ @plans.any? ? @plans.each { |plan| check_plan(service, plan) } : list_subscriptions(service)
137
+ rescue StandardError => error
138
+ add :fail, "Google Play API access", describe(error)
139
+ end
140
+
141
+ # What the gem does all day is read a purchase, so ask for one that cannot exist. Google
142
+ # answers "unknown purchase" when the account may read purchases, and a permission error
143
+ # when it may not. Acknowledging and cancelling ("Manage orders and subscriptions")
144
+ # cannot be tried without a real purchase: step 9 of the README does. Returns whether
145
+ # reading is allowed.
146
+ def check_purchase_access(service)
147
+ service.get_purchase_subscriptionsv2(google.package_name, "tillbell-check")
148
+ add :ok, "Reading purchases", "allowed"
149
+ true
150
+ rescue Google::Apis::ClientError => error
151
+ if Tillbell::GooglePlay::INVALID_TOKEN_STATUSES.include?(error.status_code)
152
+ add :ok, "Reading purchases", "allowed (Google answered that the test token is unknown, as expected)"
153
+ true
154
+ else
155
+ add :fail, "Reading purchases", describe(error)
156
+ false
157
+ end
158
+ rescue Google::Apis::AuthorizationError, Signet::AuthorizationError => error
159
+ add :fail, "Reading purchases", describe(error)
160
+ false
161
+ end
162
+
163
+ def check_plan(service, plan)
164
+ label = "Subscription #{plan[:product_id]}#{" (base plan #{plan[:base_plan_id]})" if plan[:base_plan_id]}"
165
+ subscription = service.get_monetization_subscription(google.package_name, plan[:product_id])
166
+ base_plans = Array(subscription.base_plans)
167
+ base_plan = if plan[:base_plan_id]
168
+ base_plans.find { |candidate| candidate.base_plan_id == plan[:base_plan_id] }
169
+ else
170
+ base_plans.find { |candidate| candidate.state == "ACTIVE" } || base_plans.first
171
+ end
172
+
173
+ if base_plan.nil?
174
+ add :fail, label, "the product exists but not this base plan (it has: #{base_plans.map(&:base_plan_id).join(', ').presence || 'none'})"
175
+ elsif base_plan.state == "ACTIVE"
176
+ add :ok, label, "active"
177
+ else
178
+ add :fail, label, "base plan is #{base_plan.state}: activate it in the Play Console"
179
+ end
180
+ rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => error
181
+ add :fail, label, describe(error, catalog: true)
182
+ end
183
+
184
+ def list_subscriptions(service)
185
+ subscriptions = []
186
+ token = nil
187
+ loop do
188
+ page = service.list_monetization_subscriptions(google.package_name, page_size: 1000, page_token: token)
189
+ subscriptions.concat(Array(page.subscriptions))
190
+ token = page.next_page_token.presence
191
+ break unless token
192
+ end
193
+ return add(:warn, "Subscriptions in the Play Console", "none found: create them under Monetize with Play, Products, Subscriptions") if subscriptions.empty?
194
+
195
+ subscriptions.each do |subscription|
196
+ plans = Array(subscription.base_plans).map { |base_plan| "#{base_plan.base_plan_id} (#{base_plan.state.to_s.downcase})" }
197
+ add :ok, "Subscription #{subscription.product_id}", plans.join(", ")
198
+ end
199
+ rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => error
200
+ add :fail, "Subscriptions in the Play Console", describe(error, catalog: true)
201
+ end
202
+
203
+ # What to do about an error from Google. +catalog+: the error comes from reading the product
204
+ # catalog, which may need one more permission than reading purchases.
205
+ def describe(error, catalog: false)
206
+ case error
207
+ when Signet::AuthorizationError
208
+ "key refused by Google (revoked, deleted, or from another project): create a new JSON key"
209
+ when Google::Apis::AuthorizationError
210
+ "not authorized (401): invite the service account in the Play Console with the \"View financial data\" and " \
211
+ "\"Manage orders and subscriptions\" permissions, and allow up to a day for Google to apply them#{catalog_hint(catalog)}"
212
+ when Google::Apis::ClientError
213
+ client_error(error, catalog)
214
+ else
215
+ "#{error.class}: #{error.message}"
216
+ end
217
+ end
218
+
219
+ def client_error(error, catalog)
220
+ case error.status_code
221
+ when 403
222
+ "not allowed (403): enable the Google Play Android Developer API in the Cloud project of the key, and invite the " \
223
+ "service account in the Play Console with the \"View financial data\" and \"Manage orders and subscriptions\" " \
224
+ "permissions (Google can take up to a day to apply them)#{catalog_hint(catalog)}"
225
+ when 404 then "not found (404): wrong package name, or no such product in the Play Console"
226
+ else "Google answered #{error.status_code}: #{error.message}"
227
+ end
228
+ end
229
+
230
+ def catalog_hint(catalog)
231
+ catalog ? "; reading the product catalog may also need \"View app information (read-only)\"" : ""
232
+ end
233
+ end
234
+ end
@@ -1,3 +1,3 @@
1
1
  module Tillbell
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/tillbell.rb CHANGED
@@ -2,6 +2,7 @@ require "pay"
2
2
  require "tillbell/version"
3
3
  require "tillbell/engine"
4
4
  require "tillbell/google_play"
5
+ require "tillbell/check"
5
6
 
6
7
  # Tillbell adds Google Play subscriptions to Rails apps that use the Pay gem.
7
8
  #
@@ -13,8 +14,8 @@ require "tillbell/google_play"
13
14
  #
14
15
  # Tillbell.configure do |config|
15
16
  # config.google.package_name = "com.example.app"
16
- # config.google.service_account_json = Rails.application.credentials.dig(:tillbell, :google)
17
- # config.google.pubsub_service_account_email = "pubsub-push@example.iam.gserviceaccount.com"
17
+ # config.google.service_account_json = Rails.application.credentials.dig(:tillbell, :google_service_account)
18
+ # config.google.pubsub_service_account_email = Rails.application.credentials.dig(:tillbell, :pubsub_service_account_email)
18
19
  #
19
20
  # config.on(:subscription_created) { |pay_subscription| ... }
20
21
  # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tillbell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Filstroff
@@ -108,8 +108,11 @@ files:
108
108
  - contract/restore.request.json
109
109
  - lib/generators/tillbell/install/install_generator.rb
110
110
  - lib/generators/tillbell/install/templates/initializer.rb
111
+ - lib/generators/tillbell/install/templates/next_steps.txt
111
112
  - lib/generators/tillbell/install/templates/tillbell_controller.js
113
+ - lib/tasks/tillbell.rake
112
114
  - lib/tillbell.rb
115
+ - lib/tillbell/check.rb
113
116
  - lib/tillbell/engine.rb
114
117
  - lib/tillbell/google_play.rb
115
118
  - lib/tillbell/version.rb