Class: RubyNative::IAP::RestoresController

Inherits:
ActionController::Base
  • Object
show all
Includes:
Decodable, Verifiable
Defined in:
app/controllers/ruby_native/iap/restores_controller.rb

Constant Summary

Constants included from Verifiable

Verifiable::APPLE_ROOT_CERT_PATH

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/ruby_native/iap/restores_controller.rb', line 9

def create
  customer_id = params[:customer_id]
  return head :bad_request if customer_id.blank?

  transactions = Array(params[:signed_transactions])
  return head :bad_request if transactions.empty?

  transactions.each do |signed_transaction|
    transaction = decode_and_verify_jws(signed_transaction)

    event = Event.new(
      type: "subscription.created",
      status: "active",
      owner_token: customer_id,
      product_id: transaction["productId"],
      original_transaction_id: transaction["originalTransactionId"],
      transaction_id: transaction["transactionId"],
      purchase_date: parse_timestamp(transaction["purchaseDate"]),
      expires_date: parse_timestamp(transaction["expiresDate"]),
      environment: transaction["environment"]&.downcase,
      notification_uuid: SecureRandom.uuid,
      success_path: params[:success_path]
    )

    RubyNative.fire_subscription_callbacks(event)
  end

  head :ok
rescue VerificationError, JWT::DecodeError => e
  Rails.logger.warn "[RubyNative] Restore verification failed: #{e.message}"
  head :unprocessable_entity
end