Skip to content

Instantly share code, notes, and snippets.

@lucas-nelson
Last active March 11, 2019 03:44
Show Gist options
  • Save lucas-nelson/1ce01ac616b977024c9f5e5d38ce33e6 to your computer and use it in GitHub Desktop.
Save lucas-nelson/1ce01ac616b977024c9f5e5d38ce33e6 to your computer and use it in GitHub Desktop.
Tablet subs issues

finding things

# show the valid IAPR records for the parent
parent = Parent.find(…)
pp InAppPurchaseReceipt.where(parent: parent).where.not(blob: nil); nil
# show the receipts from an Apple call by product
blob = InAppPurchaseReceipt.where(parent: parent).where.not(blob: nil).first.blob
pp TabletApi::Service::VerifyAppleReceipt.call(blob).first.group_by(&:product_id); nil
# find the blob we've logged for the parent (local PC)
cd ~Blake/readingeggs
bx grep-log --grep-opts="-A 5 -B 1" "'parent.id: 4053044'"
# does the parent account still need IAPR 'migration'?
TabletApi::Policy::Migration.allowed?(parent.id, blob)
# does the student_subscription point to a different blob?
student = Student.find(…)
StudentSubscription.
  where(student: student).
  joins(:in_app_purchase_receipts).
  where.not(in_app_purchase_receipts: {blob: nil}).
  map { |ss| ss.in_app_purchase_receipts }.
  flatten.
  uniq
# find errors in production logs:
bx grep-log "'PurchasesController\\.update_all.*failure'"

fixing things

# recreate the InAppPurchaseReceipt record for the parent based on the blob
parent = Parent.find(…)
TabletApi::Workflow::Migration::ConvertIAPRData.call(parent, blob, success: success, failure: failure)
# create any missing student_subscriptions for the parent's children
parent = Parent.find(…)
success = -> { puts "success" }
failure = ->(error) { puts "failure: #{error}" }
TabletApi::Workflow::Renewal::Parent::CreateSubscription.call(parent, success: success, failure: failure)
# manually change the IAPR record student_subscriptions
# and create the missing subs
student_sub_ids = […, …]
good_iapr = StudentSubscription.find(…).in_app_purchase_receipts.first
success = -> { puts "success" }
failure = ->(error) { puts "failure: #{error}" }
StudentSubscription.where(id: student_sub_ids).each do |ss|
  ss.in_app_purchase_receipts.clear << good_iapr
  ss.save!

  TabletApi::Workflow::Renewal::Student::CreateSubscription.call(ss, good_iapr, success: success, failure: failure)
end
#
# manually create a missing subscription record for a student
# - the Apple response to the verify(blob) says the student should have subs
# - but we have no tablet student_subscription records for the student
#
success = -> { puts "success" }
failure = ->(error) { puts "failure: #{error}" }
product = 'Tablet_Monthly_RE_Combined_Maths_1st_Prod'
blob = '…'
parent = Parent.find(…)
student = Student.find(…)
transaction_id = '190000394915509'
::TabletApi::Workflow::Purchase::CreateSubscription.call(
  parent_id: parent.id,
  apple_product_title: product,
  student_info: {id: student.id, first_name: student.first_name, year_of_birth: student.year_of_birth},
  transaction_id: transaction_id,
  original_transaction_id: transaction_id,
  receipt_blob: blob,
  currency_id: parent.currency.id,
  success: success,
  failure: failure
)
# then manually change the start_date/end_date to match the Apple response...
apple_receipt = TabletApi::Service::VerifyAppleReceipt.call(blob).first.select { |x| x.product_id == product }.sort_by(&:purchased_at).first

student.student_subscriptions.select { |x| x.subscription.name =~ /#{product}/ }.each { |y| y.update!(start_date: apple_receipt.purchased_at.utc.beginning_of_day, end_date: apple_receipt.expires_at.utc.beginning_of_day) }
# if some of many students are no longer subscribers, need to exclude them by hand and renew by student_sub
parent = Parent.find(…)
success = -> { puts "success" }
failure = ->(error) { puts "failure: #{error}" }
TabletApi::Query::Renewal::StudentSubs.for_parent(parent.id).
  reject { |sub_and_iapr| sub_and_iapr[:student_sub].student_id == … }.
  each do |sub_and_iapr| 
    TabletApi::Workflow::Renewal::Student::CreateSubscription.call(
      sub_and_iapr[:student_sub],
      sub_and_iapr[:iapr],
      success: success,
      failure: failure
    )
  end

Blob in the IAPR record does not match the blob logged on a recent login

https://trello.com/c/hCcQwfo8

IAPR and subcriptions missing for one of the children

https://trello.com/c/9TYgjTW5

Parent had no IAPR records at all

https://trello.com/c/3UiCuZa3

Apple receipt says the parent has not paid

https://trello.com/c/gBRjkA3D

No blob in the database and parent has not logged in, nothing we can do

https://trello.com/c/CEPo9LbS

System would have autocorrected on the next login

https://trello.com/c/1y8gZMv2

We mixed up the subscriptions on the students

https://trello.com/c/5VSG9F5o

Four active subscriptions for two children

https://trello.com/c/hyhcXXyA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment