tillbell 0.5.1 → 0.7.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 +4 -4
- data/CHANGELOG.md +60 -0
- data/README.md +88 -9
- data/app/controllers/tillbell/app_store_notifications_controller.rb +35 -0
- data/app/controllers/tillbell/purchases_controller.rb +27 -5
- data/app/helpers/tillbell/paywall_builder.rb +40 -11
- data/app/helpers/tillbell/paywall_helper.rb +10 -0
- data/app/models/pay/app_store/customer.rb +49 -0
- data/app/models/pay/app_store/subscription.rb +273 -0
- data/app/models/pay/google_play/subscription.rb +11 -2
- data/config/routes.rb +3 -0
- data/contract/examples/purchase.reply.pending_approval.json +4 -0
- data/contract/purchase.reply.json +31 -16
- data/lib/generators/tillbell/install/templates/initializer.rb +13 -1
- data/lib/generators/tillbell/install/templates/tillbell_controller.js +65 -10
- data/lib/tillbell/app_store.rb +178 -0
- data/lib/tillbell/check.rb +2 -1
- data/lib/tillbell/engine.rb +56 -5
- data/lib/tillbell/owner.rb +44 -4
- data/lib/tillbell/test_helpers.rb +98 -0
- data/lib/tillbell/version.rb +1 -1
- data/lib/tillbell.rb +75 -16
- metadata +27 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 40023bb1bf7c5e6140bea7065efc6477edc7e38a0f0936ddbb1d9f36cea664f1
|
|
4
|
+
data.tar.gz: 23fcd6c419b6cbbcd98fe18fe23bfc0d1dc1f551c1bf2fcb0aff13a1c9d4b6c9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '096b0bf3173c8fec91188e04d1b0ccec039b063334609019d7be2695dca8046493fbbd14f5531981c497f9a2510c8a84790c3df934e0c265b4d434be870f4901'
|
|
7
|
+
data.tar.gz: 7ae176e370fbdceefd46e35d004b5f3b44a85e80c93d5e2225a0b758ccf36cf9fcd5d4ad8ab8cd6e75d63463663104637fce2068d9814ac3e58315efc984e905
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,66 @@ All notable changes to this project are documented in this file. The format foll
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.7.0] - 2026-11-14
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Helpers that work for both stores, so an app needs no `is_a?` on the processor classes:
|
|
14
|
+
`store?` and `store_name` ("Google Play", "App Store") on Pay subscriptions (`store?` on
|
|
15
|
+
customers too), `renewing?` (gives access and has no end date) and `sandbox?` (an App Store
|
|
16
|
+
sandbox purchase or a Google Play license tester's; always false for other processors).
|
|
17
|
+
- `Tillbell::Owner#store_subscription`, `#store_on_hold?` and `#owned_store_subscription`, the
|
|
18
|
+
Google Play helpers extended to the App Store, and `#subscriptions_to_cancel_in_store`, the App
|
|
19
|
+
Store subscriptions still renewing that only the customer can stop.
|
|
20
|
+
- `Tillbell.store(user_agent)` and the `tillbell_store` view helper: `:app_store` in the Hotwire
|
|
21
|
+
Native iOS app, `:google_play` in the Android one, nil in a browser.
|
|
22
|
+
- `Pay::AppStore::Subscription#replacement?` and `#replaced?`, always false, and
|
|
23
|
+
`Tillbell::AppStore::REFUND_URL`.
|
|
24
|
+
- `config.apple.allow_xcode_transactions` accepts Xcode StoreKit Testing transactions, unsigned
|
|
25
|
+
by Apple, in the development and test environments only.
|
|
26
|
+
- `config.apple.stale_after`: an App Store subscription stops giving access that long after the
|
|
27
|
+
end of a period no notification renewed (`stale?`).
|
|
28
|
+
- `price_amount` and `price_currency` on App Store subscriptions.
|
|
29
|
+
- `app_store_product_id` on plans (the Google Play `product_id` by default, unless several plans
|
|
30
|
+
share that product; an App Store-only plan has no `product_id`), `paywall.plan_option(plan:)`
|
|
31
|
+
with the ids of the app's store, and `paywall.plan_change` from an App Store subscription.
|
|
32
|
+
- The `subscription_reactivated` event, when a customer takes a cancellation back before the end
|
|
33
|
+
of the period.
|
|
34
|
+
- `require "tillbell/test_helpers"`: `Tillbell::TestHelpers` creates store subscriptions in a
|
|
35
|
+
host app's tests.
|
|
36
|
+
- The paywall controller keeps the last store prices on the device and shows them while the
|
|
37
|
+
store answers. Copy the new `tillbell_controller.js` from the generator template into your app.
|
|
38
|
+
- Bridge contract: the iOS component can be limited to the hosts of the app's pages
|
|
39
|
+
(`allowedHosts` in tillbell-ios); a message from another host gets an `error` reply. On
|
|
40
|
+
Android, Hotwire Native's `originTrustPolicy` already does it.
|
|
41
|
+
|
|
42
|
+
### Changed
|
|
43
|
+
|
|
44
|
+
- App Store `sandbox?` is also true for Xcode StoreKit Testing purchases.
|
|
45
|
+
- `tillbell:check` skips plans without a Google Play `product_id`.
|
|
46
|
+
|
|
47
|
+
## [0.6.0] - 2026-11-11
|
|
48
|
+
|
|
49
|
+
### Added
|
|
50
|
+
|
|
51
|
+
- App Store subscriptions, for the iOS app of [tillbell-ios](https://github.com/pmakerhq/tillbell-ios):
|
|
52
|
+
`Pay::AppStore::Customer` and `Pay::AppStore::Subscription`, set up with
|
|
53
|
+
`config.apple.bundle_id`. `POST /tillbell/purchases` accepts the signed StoreKit 2
|
|
54
|
+
transaction the iOS app sends as its purchase token, verifies Apple's signature without
|
|
55
|
+
calling Apple and checks its `appAccountToken`, the same account id as Google Play.
|
|
56
|
+
- `POST /tillbell/notifications/app_store` receives App Store Server Notifications (version 2),
|
|
57
|
+
verified and processed through Pay like Google Play notifications: renewals, billing retries,
|
|
58
|
+
grace periods, expiries and refunds keep the subscription current.
|
|
59
|
+
- `app_store?` and the `app_store` scope on Pay customers and subscriptions. `grants_access?`
|
|
60
|
+
gives no access to an App Store subscription in billing retry after the grace period.
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
|
|
64
|
+
- Bridge contract: a `pending` purchase reply may come without `purchaseToken`, for an App Store
|
|
65
|
+
purchase waiting for approval (Ask to Buy). The paywall controller then shows the pending
|
|
66
|
+
message. Copy the new `tillbell_controller.js` from the generator template into your app.
|
|
67
|
+
- The bridge contract documents how tillbell-ios maps it to StoreKit 2.
|
|
68
|
+
|
|
9
69
|
## [0.5.1] - 2026-11-11
|
|
10
70
|
|
|
11
71
|
### Changed
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
**Self-hosted in-app subscriptions for Rails + Hotwire Native apps, built on [Pay](https://github.com/pay-rails/pay).**
|
|
7
7
|
|
|
8
|
-
Your Android app sells subscriptions through Google Play. Your Rails app verifies them, acknowledges them and keeps them in sync. No third-party service sits between the store and your server, and no extra table is added:
|
|
8
|
+
Your Android app sells subscriptions through Google Play, your iOS app through the App Store. Your Rails app verifies them, acknowledges them and keeps them in sync. No third-party service sits between the store and your server, and no extra table is added: store subscriptions are regular `Pay::Subscription` records, right next to your Stripe ones.
|
|
9
9
|
|
|
10
10
|
```ruby
|
|
11
11
|
user.pay_subscriptions.active # Stripe and Google Play together
|
|
@@ -14,7 +14,7 @@ subscription.active? # same rules as for Stripe
|
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
> [!NOTE]
|
|
17
|
-
> **Status: v0.
|
|
17
|
+
> **Status: v0.6.** Google Play is verified with real purchases. App Store support (StoreKit 2, with [tillbell-ios](https://github.com/pmakerhq/tillbell-ios)) is new: it is unit tested but not yet verified with real purchases.
|
|
18
18
|
|
|
19
19
|
## Table of contents
|
|
20
20
|
|
|
@@ -29,6 +29,7 @@ subscription.active? # same rules as for Stripe
|
|
|
29
29
|
- [Subscriptions and statuses](#subscriptions-and-statuses)
|
|
30
30
|
- [Events](#events)
|
|
31
31
|
- [Google Play setup](#google-play-setup)
|
|
32
|
+
- [App Store setup](#app-store-setup)
|
|
32
33
|
- [Security](#security)
|
|
33
34
|
- [Troubleshooting](#troubleshooting)
|
|
34
35
|
- [The Tillbell family](#the-tillbell-family)
|
|
@@ -53,7 +54,7 @@ Assuming a Rails 8 app with a `User` model and Pay installed:
|
|
|
53
54
|
|
|
54
55
|
```sh
|
|
55
56
|
bundle add pay
|
|
56
|
-
bundle add tillbell
|
|
57
|
+
bundle add tillbell
|
|
57
58
|
bin/rails generate tillbell:install
|
|
58
59
|
```
|
|
59
60
|
|
|
@@ -128,6 +129,7 @@ The page and the native app talk through a [Hotwire Native bridge component](htt
|
|
|
128
129
|
| Pay | 12+, installed and migrated |
|
|
129
130
|
| Active Job | A queue backend in production (Solid Queue, Sidekiq, ...) |
|
|
130
131
|
| Android app | Hotwire Native with [tillbell-android](https://github.com/pmakerhq/tillbell-android) |
|
|
132
|
+
| iOS app (optional) | Hotwire Native with [tillbell-ios](https://github.com/pmakerhq/tillbell-ios) |
|
|
131
133
|
|
|
132
134
|
## Installation
|
|
133
135
|
|
|
@@ -204,7 +206,7 @@ The values marked "credentials" below come from Google: the [Google Play setup](
|
|
|
204
206
|
| `google.pubsub_audience` | endpoint URL | Audience of the Pub/Sub OIDC token. Set it when a proxy changes the URL Rails sees. |
|
|
205
207
|
| `current_owner` | `-> { current_user }` | Returns who is buying. Evaluated in the controller and in the view, so any helper available there works (`Current.user`, `current_account`, ...). |
|
|
206
208
|
| `parent_controller` | `"ApplicationController"` | Controller the purchase endpoint inherits from, to reuse your authentication, CSRF protection and error handling. |
|
|
207
|
-
| `plans` | `[]` | The plans you sell, lowest first: `[{ name: :solo, product_id: "app_solo", base_plan_id: "monthly" }, ...]`. `tillbell:check` verifies
|
|
209
|
+
| `plans` | `[]` | The plans you sell, lowest first: `[{ name: :solo, product_id: "app_solo", base_plan_id: "monthly" }, ...]`. Add `app_store_product_id:` when the App Store product has another id than the Google Play one, or when several plans share a Google Play product (each App Store product is one duration, so they get no default); an App Store-only plan has no `product_id`. `tillbell:check` verifies the Google Play ones, `subscription.plan` finds the one a subscription is on, and `Tillbell.replacement_mode` and `paywall.plan_change` use their order to tell an upgrade from a downgrade. |
|
|
208
210
|
| `subscription_name` | `->(product_id:, base_plan_id:) { Pay.default_product_name }` | Pay subscription `name` of a Google Play product, as used by `subscribed?(name:)`. |
|
|
209
211
|
|
|
210
212
|
## The paywall
|
|
@@ -230,7 +232,7 @@ The markup is unstyled and every builder method accepts HTML attributes (`class:
|
|
|
230
232
|
|
|
231
233
|
| Builder method | Renders |
|
|
232
234
|
|---|---|
|
|
233
|
-
| `plan_option(product_id
|
|
235
|
+
| `plan_option(product_id: nil, base_plan_id: nil, plan: nil, checked: false, replacing: nil, replacement_mode: nil)` | A radio button wrapped in a label with the block's content. `plan_option(plan: :family)` takes a plan of `config.plans` instead of ids, with the ids of the store of the app showing the page; a plan that store does not sell renders nothing. |
|
|
234
236
|
| `price(placeholder = "")` | Where the price localized by Google Play is written. Use it inside `plan_option`. |
|
|
235
237
|
| `submit(text)` | Starts the purchase of the checked plan. |
|
|
236
238
|
| `restore(text)` | Records again the purchases Google Play knows for the device, e.g. after a reinstall. |
|
|
@@ -338,7 +340,7 @@ Tillbell.plan(:family) # by name, or Tillbell.pla
|
|
|
338
340
|
Tillbell.replacement_mode(from: :solo, to: :family) # => "CHARGE_PRORATED_PRICE" (upgrade); a downgrade gives "DEFERRED"
|
|
339
341
|
```
|
|
340
342
|
|
|
341
|
-
A plan change from the settings page is then one option in a paywall:
|
|
343
|
+
A plan change from the settings page is then one option in a paywall. `from` may be a Google Play or an App Store subscription: on the App Store the option is the target product alone, and Apple replaces the subscription of the same group by itself.
|
|
342
344
|
|
|
343
345
|
```erb
|
|
344
346
|
<%= tillbell_paywall(success_url: settings_path) do |paywall| %>
|
|
@@ -357,6 +359,9 @@ A plan change from the settings page is then one option in a paywall:
|
|
|
357
359
|
|---|---|
|
|
358
360
|
| `google_play_subscription` | The Google Play subscription, whatever the default processor. |
|
|
359
361
|
| `google_play_on_hold?` | True when Google suspended the subscription for a failed payment: send the customer to `manage_url` rather than to a second subscription, which would bill twice if Google's retry succeeds. |
|
|
362
|
+
| `store_subscription`, `store_on_hold?` | The same for either store: the most relevant Google Play or App Store subscription, and whether the store suspended it. |
|
|
363
|
+
| `owned_store_subscription` | The store subscription the store still counts as the customer's, with or without access (`owned?`): offer its `manage_url`, not a second purchase on the web, which would bill twice. |
|
|
364
|
+
| `subscriptions_to_cancel_in_store` | App Store subscriptions that still renew. Only the customer can stop them, so show their `manage_url` before closing the account. |
|
|
360
365
|
| `current_subscription` | The subscription to show and decide access from: the one that renews, then one that is canceled but running, then one whose payment is retried or that is paused; the most recent breaks a tie. |
|
|
361
366
|
| `billing_subscriptions` | What still bills or resumes by itself (`subscription.billing?`), Google Play first. |
|
|
362
367
|
| `cancel_billing_subscriptions!` | Cancels them: call it before closing or deleting an account. |
|
|
@@ -369,7 +374,15 @@ def subscribed? = !!current_user.current_subscription&.grants_access?
|
|
|
369
374
|
|
|
370
375
|
A customer can have a Stripe subscription and a Google Play one (a web customer who later subscribes in the app, or the reverse). `Pay::GooglePlay::Customer` is never the owner's default payment processor, so `owner.payment_processor` does not see Google Play subscriptions: look at `owner.pay_subscriptions`, or at the Google Play customer (`owner.pay_customers.google_play.first`), to decide who has access.
|
|
371
376
|
|
|
372
|
-
Pay only knows its own processors, so Tillbell adds `google_play?` to customers and subscriptions, and
|
|
377
|
+
Pay only knows its own processors, so Tillbell adds `google_play?` and `app_store?` to customers and subscriptions, and matching scopes next to Pay's `stripe` one (`Pay::Subscription.google_play`). Code that does not care which store adds these, on every Pay subscription:
|
|
378
|
+
|
|
379
|
+
| Method | Returns |
|
|
380
|
+
|---|---|
|
|
381
|
+
| `store?`, `store_name` | Whether it was bought in a store, and which one: `"Google Play"`, `"App Store"`, or nil for another processor. |
|
|
382
|
+
| `renewing?` | Gives access and will renew by itself (not canceled with an end date to come). |
|
|
383
|
+
| `sandbox?` | A test purchase that costs nothing: an App Store sandbox purchase, or a Google Play license tester's. False for other processors. Keep them out of revenue reports and conversion events. |
|
|
384
|
+
|
|
385
|
+
`Tillbell.store(request.user_agent)` (`tillbell_store` in views) tells which store the Hotwire Native app showing the page buys from: `:app_store`, `:google_play`, or nil in a browser. Unlike `tillbell_native?` it does not need the `tillbell` component, so it also answers for app versions that cannot buy yet, which is what store rules need, e.g. no link to a web checkout in the iOS app.
|
|
373
386
|
|
|
374
387
|
`subscription.cancel` stops the renewal through the Google Play Developer API; the customer keeps access until the end of the paid period. Google can only end a subscription immediately by refunding it, which Tillbell never does on its own, so `cancel_now!` (which Pay calls when an owner is destroyed) also just stops the renewal. Google answers 410 for a purchase token that expired more than 60 days ago: a subscription still stored as active (a missed notification) is then recorded as ended, with a `subscription_expired` event, instead of failing the deletion of its owner. Any other error is raised, a 404 from a wrong package name for example: the subscription may still be billing. A subscription ended or already canceled is left alone, and one that waits for a deferred plan change is cancelled like any other: whether it is renewing comes from Google's subscription state, not from the entitlement about to end. `swap`, `pause` and `resume` raise `Tillbell::Error`: plan changes go through the paywall, and the rest happens in the Play Store.
|
|
375
388
|
|
|
@@ -392,6 +405,7 @@ Tillbell.configure do |config|
|
|
|
392
405
|
config.on(:payment_recovered) { |subscription| } # the payment went through after a failure
|
|
393
406
|
config.on(:plan_changed) { |subscription| } # the customer is now on another product or base plan; subscription.replaced_subscription is the previous purchase, if any
|
|
394
407
|
config.on(:subscription_canceled) { |subscription| } # will not renew; access until ends_at
|
|
408
|
+
config.on(:subscription_reactivated) { |subscription| } # the customer took a cancellation back before ends_at (also fires subscription_updated)
|
|
395
409
|
config.on(:subscription_expired) { |subscription| }
|
|
396
410
|
config.on(:subscription_revoked) { |subscription| } # refunded or charged back
|
|
397
411
|
config.on(:purchase_rejected) { |details| } # { owner:, purchase_token: } posted by another account
|
|
@@ -593,10 +607,75 @@ The Play Console's success message only means the message reached **your topic**
|
|
|
593
607
|
| 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) |
|
|
594
608
|
| 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` |
|
|
595
609
|
|
|
610
|
+
## App Store setup
|
|
611
|
+
|
|
612
|
+
The iOS app ([tillbell-ios](https://github.com/pmakerhq/tillbell-ios)) talks to the same paywall with the same messages. Nothing is needed from Apple to verify a purchase: the iOS app sends the transaction signed by Apple, and Tillbell checks the signature against Apple Root CA - G3, which ships with the gem.
|
|
613
|
+
|
|
614
|
+
1. **Set the bundle id** of the iOS app in `config/initializers/tillbell.rb`. Transactions and notifications signed for another app are refused.
|
|
615
|
+
|
|
616
|
+
```ruby
|
|
617
|
+
config.apple.bundle_id = "com.example.app"
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
2. **Create the subscriptions** in App Store Connect (*Monetization* › *Subscriptions*), in one subscription group, ordered from the highest level to the lowest: the App Store decides from the levels whether a plan change is an upgrade (now) or a downgrade (at the next renewal).
|
|
621
|
+
|
|
622
|
+
3. **Point the notifications to your app.** App Store Connect › your app › *App Information* › *App Store Server Notifications*: set both the production and the sandbox URL to `https://<your host>/tillbell/notifications/app_store`, version 2. Renewals, billing retries, expiries, refunds and Ask to Buy approvals arrive there; without them, a subscription is never renewed in your database.
|
|
623
|
+
|
|
624
|
+
4. **Name App Store products in the paywall on iOS.** The App Store has no base plans: each duration is its own product. Declare the App Store product of each plan and let the paywall pick the ids of the store of the app showing the page:
|
|
625
|
+
|
|
626
|
+
```ruby
|
|
627
|
+
config.plans = [{ name: :monthly, product_id: "premium", base_plan_id: "monthly", app_store_product_id: "premium.monthly" }]
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
```erb
|
|
631
|
+
<%= paywall.plan_option(plan: :monthly) { paywall.price } %>
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
A plan change needs nothing more on iOS: the App Store replaces the subscription of the same group by itself, and `paywall.plan_change` knows it.
|
|
635
|
+
|
|
636
|
+
5. **Test** in the simulator with a StoreKit configuration file, then on a device with a sandbox account (*Settings* › *Developer* › *Sandbox Apple Account*). Xcode signs StoreKit Testing transactions itself, not with Apple's chain, so the purchase endpoint answers `invalid` for them unless you allow them in development:
|
|
637
|
+
|
|
638
|
+
```ruby
|
|
639
|
+
config.apple.allow_xcode_transactions = Rails.env.development?
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
They are then accepted without checking their signature, as Apple's own server libraries do for the Xcode environment, still for your bundle id and the signed-in owner's account id. Tillbell ignores the setting outside the development and test environments: anyone could forge an unsigned transaction.
|
|
643
|
+
|
|
644
|
+
6. **Decide what happens if notifications stop.** A renewing subscription only moves on when Apple notifies the renewal. If notifications stop reaching your server, the last state stays, access included. `config.apple.stale_after = 16.days` (at least your billing grace period) makes `grants_access?` false that long after the end of a period no notification renewed; `stale?` tells such subscriptions.
|
|
645
|
+
|
|
646
|
+
How App Store subscriptions differ:
|
|
647
|
+
|
|
648
|
+
- `processor_id` is the `originalTransactionId`, which stays the same across renewals and plan changes.
|
|
649
|
+
- The Pay customer (`Pay::AppStore::Customer`) shares its `processor_id` with the owner's Google Play customer: the paywall sends one account id to both apps, passed to StoreKit as the `appAccountToken`.
|
|
650
|
+
- Statuses: `active` (including Apple's billing grace period), `past_due` in billing retry after the grace period (no access, see `grants_access?`), `canceled` once expired or refunded.
|
|
651
|
+
- Sandbox purchases (TestFlight, App Review) are accepted, so App Review can buy against your production server (Xcode StoreKit Testing ones only in development, see above); `sandbox?` tells them apart, for example to keep them out of revenue reports.
|
|
652
|
+
- `price_amount` and `price_currency` give what Apple charged for the last transaction (an introductory offer included), as for Google Play.
|
|
653
|
+
- Family-shared subscriptions are not recorded: they belong to the family member who bought them.
|
|
654
|
+
- Only the customer can cancel an App Store subscription, in the App Store: `cancel` and `cancel_now!` leave it unchanged (they never raise, so deleting an owner still works). Send the customer to `manage_url` before closing their account (`owner.subscriptions_to_cancel_in_store` lists them). Only Apple refunds, at `Tillbell::AppStore::REFUND_URL`.
|
|
655
|
+
- `replacement?` and `replaced?` are always false: a plan change keeps the same `originalTransactionId`.
|
|
656
|
+
- An Ask to Buy purchase is answered `pending` without a token: the paywall shows its pending message, and the subscription is recorded from Apple's notification once a parent approves it.
|
|
657
|
+
|
|
658
|
+
## Testing your app
|
|
659
|
+
|
|
660
|
+
`require "tillbell/test_helpers"` and include `Tillbell::TestHelpers` in your tests to create store subscriptions without Apple's signature or a call to Google. They go through the same code as a real purchase, events included:
|
|
661
|
+
|
|
662
|
+
```ruby
|
|
663
|
+
# spec/rails_helper.rb
|
|
664
|
+
require "tillbell/test_helpers"
|
|
665
|
+
RSpec.configure { |config| config.include Tillbell::TestHelpers }
|
|
666
|
+
|
|
667
|
+
create_app_store_subscription(user, product_id: "premium.monthly", environment: "Sandbox")
|
|
668
|
+
create_app_store_subscription(user, renewal_info: app_store_renewal_info(billing_retry: true), expires_at: 1.hour.ago)
|
|
669
|
+
create_google_play_subscription(user, state: "SUBSCRIPTION_STATE_ON_HOLD")
|
|
670
|
+
tillbell_events { ... } # => [[:subscription_created, subscription], ...]
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
`app_store_transaction`, `app_store_renewal_info` and `google_subscription` build the payloads alone, to pass to `Pay::AppStore::Subscription.sync` or to stub `Tillbell::GooglePlay.subscription`.
|
|
674
|
+
|
|
596
675
|
## Security
|
|
597
676
|
|
|
598
677
|
- The purchase endpoint inherits from your `ApplicationController`, so your authentication and CSRF protection apply. It never trusts an owner or account id sent by the client.
|
|
599
|
-
- Every purchase is read back from Google, and
|
|
678
|
+
- Every Google Play purchase is read back from Google, and its `obfuscatedAccountId` must match the signed-in owner's account id. Every App Store transaction and notification must be signed by Apple (a certificate chain to Apple Root CA - G3) for the configured bundle id, and a transaction's `appAccountToken` must match the owner's account id.
|
|
600
679
|
- Notifications must carry an OIDC token signed by Google for the configured service account. Anything else gets a 401.
|
|
601
680
|
- The account id sent to Google is a random UUID, never an e-mail or a database id.
|
|
602
681
|
- Purchase tokens are never logged in full.
|
|
@@ -623,7 +702,7 @@ Problems on Google's side (credentials, permissions, notifications) have their o
|
|
|
623
702
|
|---|---|
|
|
624
703
|
| **tillbell-rails** (this repo) | Rails engine: Pay processor, purchase verification, notifications, paywall helper |
|
|
625
704
|
| [tillbell-android](https://github.com/pmakerhq/tillbell-android) | Hotwire Native bridge component for Google Play Billing |
|
|
626
|
-
| [tillbell-ios](https://github.com/pmakerhq/tillbell-ios) | Hotwire Native bridge component for StoreKit 2
|
|
705
|
+
| [tillbell-ios](https://github.com/pmakerhq/tillbell-ios) | Hotwire Native bridge component for StoreKit 2 |
|
|
627
706
|
|
|
628
707
|
## Getting help
|
|
629
708
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Tillbell
|
|
2
|
+
# Receives App Store Server Notifications (version 2), the URL set as the "Production
|
|
3
|
+
# Server URL" (and "Sandbox Server URL") in App Store Connect.
|
|
4
|
+
#
|
|
5
|
+
# The endpoint is public, so every notification must carry Apple's signature for this app;
|
|
6
|
+
# anything else gets a 401. Verified notifications are stored and processed by Pay
|
|
7
|
+
# (+Pay::Webhook+ and +Pay::Webhooks::ProcessJob+), like Google Play notifications, and
|
|
8
|
+
# answered with a 200 straight away. A non-2xx answer would make Apple send it again.
|
|
9
|
+
class AppStoreNotificationsController < Pay::Webhooks::BaseController
|
|
10
|
+
private
|
|
11
|
+
def verified_event
|
|
12
|
+
AppStore.verify_notification!(params[:signedPayload])
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# "DID_RENEW" becomes "did_renew".
|
|
16
|
+
def event_type(notification)
|
|
17
|
+
notification["notificationType"].to_s.downcase
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def queue?(notification)
|
|
21
|
+
Rails.logger.info "[Tillbell] App Store notification #{notification["notificationType"]}" \
|
|
22
|
+
"#{" subtype=#{notification["subtype"]}" if notification["subtype"]}"
|
|
23
|
+
notification.dig("data", "signedTransactionInfo").present?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Pay derives the processor from the controller name; this one is not named after it.
|
|
27
|
+
def processor
|
|
28
|
+
"app_store"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def invalid_signature
|
|
32
|
+
head :unauthorized
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
module Tillbell
|
|
2
|
-
# Records a Google Play purchase made in the native app.
|
|
2
|
+
# Records a Google Play or App Store purchase made in the native app.
|
|
3
3
|
#
|
|
4
|
-
# The paywall posts the purchase token here as soon as the
|
|
5
|
-
#
|
|
6
|
-
#
|
|
4
|
+
# The paywall posts the purchase token here as soon as the purchase sheet succeeds, so
|
|
5
|
+
# access is granted without waiting for a notification. The token is never trusted:
|
|
6
|
+
# Tillbell reads a Google Play purchase from Google, verifies Apple's signature of an App
|
|
7
|
+
# Store transaction (the token the iOS app sends), and checks that the purchase was made
|
|
7
8
|
# with the account id of the signed-in owner.
|
|
8
9
|
#
|
|
9
10
|
# Inherits from +Tillbell.parent_controller+ to run the host app's authentication and
|
|
@@ -15,13 +16,16 @@ module Tillbell
|
|
|
15
16
|
# (a pending purchase does not).
|
|
16
17
|
# [verifying] Google could not be reached; the purchase is verified in the background.
|
|
17
18
|
# [rejected] The purchase belongs to another account.
|
|
18
|
-
# [invalid] Google does not know this purchase token
|
|
19
|
+
# [invalid] Google does not know this purchase token, or Apple did not sign this
|
|
20
|
+
# transaction for this app.
|
|
19
21
|
class PurchasesController < Tillbell.parent_controller.constantize
|
|
20
22
|
def create
|
|
21
23
|
owner = instance_exec(&Tillbell.current_owner)
|
|
22
24
|
return head :unauthorized if owner.nil?
|
|
23
25
|
|
|
24
26
|
purchase_token = params.expect(:purchase_token)
|
|
27
|
+
return record_app_store_transaction(owner, purchase_token) if AppStore.jws?(purchase_token)
|
|
28
|
+
|
|
25
29
|
purchase = GooglePlay.subscription(purchase_token)
|
|
26
30
|
|
|
27
31
|
if GooglePlay.account_id(purchase) == Pay::GooglePlay::Customer.for(owner).processor_id
|
|
@@ -40,5 +44,23 @@ module Tillbell
|
|
|
40
44
|
|
|
41
45
|
render json: { status: "invalid" }, status: :unprocessable_content
|
|
42
46
|
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
# The signature is checked on the server, without calling Apple, so there is no
|
|
50
|
+
# "verifying" answer for the App Store.
|
|
51
|
+
def record_app_store_transaction(owner, jws)
|
|
52
|
+
transaction = AppStore.verify_transaction!(jws)
|
|
53
|
+
|
|
54
|
+
if transaction["appAccountToken"].to_s.downcase == Pay::GooglePlay::Customer.for(owner).processor_id
|
|
55
|
+
subscription = Pay::AppStore::Subscription.sync(transaction)
|
|
56
|
+
render json: { status: "verified", active: subscription.active?, subscription_status: subscription.status }
|
|
57
|
+
else
|
|
58
|
+
Tillbell.instrument(:purchase_rejected, owner: owner, purchase_token: Tillbell.redact(transaction["originalTransactionId"]))
|
|
59
|
+
render json: { status: "rejected" }, status: :forbidden
|
|
60
|
+
end
|
|
61
|
+
rescue Tillbell::Error => error
|
|
62
|
+
Rails.logger.info "[Tillbell] Refusing an App Store transaction: #{error.message}"
|
|
63
|
+
render json: { status: "invalid" }, status: :unprocessable_content
|
|
64
|
+
end
|
|
43
65
|
end
|
|
44
66
|
end
|
|
@@ -8,11 +8,20 @@ module Tillbell
|
|
|
8
8
|
@template = template
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
# A radio button for one
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
|
|
11
|
+
# A radio button for one product, wrapped in a label with the block's content: a Google
|
|
12
|
+
# Play +product_id+ and +base_plan_id+, an App Store +product_id+, or a +plan+ of
|
|
13
|
+
# +Tillbell.plans+ (a name or a plan), whose ids are those of the store of the app showing
|
|
14
|
+
# the page (see +Tillbell.store+; in a browser, those of the store that sells it, Google
|
|
15
|
+
# Play first). A +plan+ the store of the app does not sell renders nothing. Pass +replacing+ (the current
|
|
16
|
+
# Pay::GooglePlay::Subscription) to make it a plan change; +replacement_mode+ is Google's
|
|
17
|
+
# proration mode, e.g. "CHARGE_PRORATED_PRICE", and is passed through untouched.
|
|
18
|
+
def plan_option(product_id: nil, base_plan_id: nil, plan: nil, checked: false, replacing: nil, replacement_mode: nil, **html_options, &block)
|
|
19
|
+
if plan
|
|
20
|
+
plan = find_plan(plan)
|
|
21
|
+
product_id, base_plan_id = plan.product_for(store || (plan.product_id ? :google_play : :app_store)).values_at(:product_id, :base_plan_id)
|
|
22
|
+
return "".html_safe if product_id.blank?
|
|
23
|
+
end
|
|
24
|
+
raise ArgumentError, "plan_option needs a product_id or a plan" if product_id.blank?
|
|
16
25
|
raise ArgumentError, "a plan change needs a replacement_mode" if replacing && replacement_mode.blank?
|
|
17
26
|
|
|
18
27
|
radio = tag.input(
|
|
@@ -26,15 +35,25 @@ module Tillbell
|
|
|
26
35
|
tag.label(**html_options) { radio + @template.capture(&block) }
|
|
27
36
|
end
|
|
28
37
|
|
|
29
|
-
# The plan option for moving from the
|
|
30
|
-
# +Tillbell.plans+ (a name or a plan), checked
|
|
31
|
-
# prorated for an upgrade, deferred to the next renewal for a downgrade
|
|
32
|
-
# +Tillbell.replacement_mode+).
|
|
38
|
+
# The plan option for moving from the store subscription +from+ to the plan +to+ of
|
|
39
|
+
# +Tillbell.plans+ (a name or a plan), checked. On Google Play it carries the replacement
|
|
40
|
+
# mode Google needs: prorated for an upgrade, deferred to the next renewal for a downgrade
|
|
41
|
+
# (see +Tillbell.replacement_mode+). On the App Store it is the target product alone: Apple
|
|
42
|
+
# replaces the subscription of the same group by itself. Use it in a paywall that offers
|
|
43
|
+
# this one plan, in the app of the store +from+ was bought in (see +Tillbell.store+): the
|
|
44
|
+
# other store cannot change it.
|
|
33
45
|
def plan_change(to:, from:, **html_options, &block)
|
|
34
|
-
raise ArgumentError, "plan_change needs the current
|
|
46
|
+
raise ArgumentError, "plan_change needs the current store subscription (from:)" if from.nil?
|
|
47
|
+
|
|
48
|
+
target = find_plan(to)
|
|
49
|
+
if from.app_store?
|
|
50
|
+
raise ArgumentError, "#{to.inspect} is the current plan" if from.plan == target
|
|
51
|
+
raise ArgumentError, "#{to.inspect} has no App Store product" if target.app_store_product_id.blank?
|
|
52
|
+
|
|
53
|
+
return plan_option(**target.product_for(:app_store), checked: true, **html_options, &block)
|
|
54
|
+
end
|
|
35
55
|
|
|
36
56
|
current = from.plan || raise(ArgumentError, "the current subscription (#{from.processor_plan}:#{from.base_plan_id}) is not in Tillbell.plans")
|
|
37
|
-
target = to.is_a?(Tillbell::Plan) ? to : Tillbell.plan(to) || raise(ArgumentError, "#{to.inspect} is not in Tillbell.plans")
|
|
38
57
|
plan_option(product_id: target.product_id, base_plan_id: target.base_plan_id, checked: true, replacing: from,
|
|
39
58
|
replacement_mode: Tillbell.replacement_mode(from: current, to: target), **html_options, &block)
|
|
40
59
|
end
|
|
@@ -72,6 +91,16 @@ module Tillbell
|
|
|
72
91
|
end
|
|
73
92
|
|
|
74
93
|
private
|
|
94
|
+
def find_plan(plan)
|
|
95
|
+
plan.is_a?(Tillbell::Plan) ? plan : Tillbell.plan(plan) || raise(ArgumentError, "#{plan.inspect} is not in Tillbell.plans")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The store of the app showing the page; Google Play ids in a browser, where the paywall
|
|
99
|
+
# never connects anyway.
|
|
100
|
+
def store
|
|
101
|
+
Tillbell.store(@template.request.user_agent)
|
|
102
|
+
end
|
|
103
|
+
|
|
75
104
|
def button(text, action, **html_options)
|
|
76
105
|
tag.button(text, type: "button", disabled: true,
|
|
77
106
|
**with_data(html_options, tillbell_target: "action", action: "tillbell##{action}"))
|
|
@@ -13,6 +13,16 @@ module Tillbell
|
|
|
13
13
|
Tillbell.native?(request.user_agent)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
# The store of the native app showing the page (+:app_store+, +:google_play+) or nil in a
|
|
17
|
+
# browser. Same check as +Tillbell.store+.
|
|
18
|
+
#
|
|
19
|
+
# <% unless tillbell_store == :app_store %>
|
|
20
|
+
# <%= link_to "Subscribe on the web", checkout_path %>
|
|
21
|
+
# <% end %>
|
|
22
|
+
def tillbell_store
|
|
23
|
+
Tillbell.store(request.user_agent)
|
|
24
|
+
end
|
|
25
|
+
|
|
16
26
|
# Renders an unstyled paywall driven by the +tillbell+ Stimulus controller, which
|
|
17
27
|
# talks to the +tillbell+ Hotwire Native bridge component.
|
|
18
28
|
#
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module Pay
|
|
2
|
+
module AppStore
|
|
3
|
+
# The Pay customer behind every App Store purchase of an owner.
|
|
4
|
+
#
|
|
5
|
+
# Its +processor_id+ is the same UUID as the owner's Pay::GooglePlay::Customer: the paywall
|
|
6
|
+
# sends one +obfuscatedAccountId+ to both apps, and the iOS app passes it to StoreKit as the
|
|
7
|
+
# +appAccountToken+. Apple returns it in every signed transaction, which is how Tillbell
|
|
8
|
+
# knows which owner a transaction belongs to.
|
|
9
|
+
class Customer < Pay::Customer
|
|
10
|
+
has_many :subscriptions, dependent: :destroy, class_name: "Pay::AppStore::Subscription"
|
|
11
|
+
|
|
12
|
+
# Returns the App Store customer of +owner+, creating it on first use.
|
|
13
|
+
def self.for(owner)
|
|
14
|
+
owner.add_payment_processor(:app_store, processor_id: Pay::GooglePlay::Customer.for(owner).processor_id)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# The App Store customer whose account id is +app_account_token+, or nil when no owner
|
|
18
|
+
# has it. Created on first use for an owner who only had a Google Play customer.
|
|
19
|
+
def self.for_account_id(app_account_token)
|
|
20
|
+
account_id = app_account_token.to_s.downcase
|
|
21
|
+
return if account_id.blank?
|
|
22
|
+
|
|
23
|
+
find_by(processor_id: account_id) || Pay::GooglePlay::Customer.find_by(processor_id: account_id)&.then { |customer| self.for(customer.owner) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# App Store customers exist only in this database.
|
|
27
|
+
def api_record
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def update_api_record(**)
|
|
32
|
+
self
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Store purchases always start in the native app, never on the server.
|
|
36
|
+
def subscribe(**)
|
|
37
|
+
raise Tillbell::Error, "App Store subscriptions are purchased in the iOS app, through the Tillbell paywall"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def charge(*)
|
|
41
|
+
raise Tillbell::Error, "The App Store does not support server-side charges"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def add_payment_method(*, **)
|
|
45
|
+
raise Tillbell::Error, "The App Store manages payment methods in the customer's Apple Account"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|