Module: PurchaseKit::PaywallHelper

Defined in:
app/helpers/purchase_kit/paywall_helper.rb

Instance Method Summary collapse

Instance Method Details

#purchasekit_paywall(customer_id:, success_path: main_app.root_path, **options) {|PaywallBuilder| ... } ⇒ Object

Renders a paywall form that triggers native in-app purchases

Example (with Pay):

<% pay_customer = current_user.set_payment_processor(:purchasekit) %>
<%= purchasekit_paywall customer_id: pay_customer.id, success_path: dashboard_path do |paywall| %>
  <%= paywall.plan_option product: @annual, selected: true do %>
    Annual - <%= paywall.price %>
  <% end %>
  <%= paywall.submit "Subscribe" %>
<% end %>

Parameters:

  • customer_id (String, Integer)

    The identifier passed back in webhook events. With Pay, this must be ‘Pay::Customer.id` (the webhook handler does `Pay::Customer.find(customer_id)`). Without Pay, use your own user ID.

  • success_path (String) (defaults to: main_app.root_path)

    Where to redirect after successful purchase

Yields:

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 20

def purchasekit_paywall(customer_id:, success_path: main_app.root_path, **options)
  raise ArgumentError, "customer_id is required" if customer_id.blank?

  builder = PaywallBuilder.new(self)

  form_data = (options.delete(:data) || {}).merge(
    controller: "purchasekit--paywall",
    purchasekit__paywall_customer_id_value: customer_id
  )

  form_with(url: purchase_kit.purchases_path, id: "purchasekit_paywall", data: form_data, **options) do |form|
    hidden = hidden_field_tag(:customer_id, customer_id)
    hidden += hidden_field_tag(:success_path, success_path)
    hidden += hidden_field_tag(:environment, "sandbox", data: {purchasekit__paywall_target: "environment"})
    hidden + capture { yield builder }
  end
end