supporter.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. get '/supporter/?' do
  2. @title = 'Become a Supporter'
  3. erb :'welcome'
  4. end
  5. post '/supporter/end' do
  6. require_login
  7. redirect '/' unless parent_site.paying_supporter?
  8. parent_site.end_supporter_membership!
  9. flash[:success] = "Your supporter membership has been cancelled. We're sorry to see you go, but thanks again for your support! Remember, you can always become a supporter again in the future."
  10. redirect '/supporter'
  11. end
  12. post '/supporter/update' do
  13. require_login
  14. plan_type = 'supporter'
  15. if is_special_upgrade
  16. require_admin
  17. site = Site[username: params[:username]]
  18. plan_type = 'special'
  19. if site.nil?
  20. flash[:error] = 'Cannot find the requested user.'
  21. redirect '/admin'
  22. end
  23. end
  24. site ||= parent_site
  25. DB.transaction do
  26. if site.stripe_customer_id
  27. customer = Stripe::Customer.retrieve site.stripe_customer_id
  28. customer.cards.each {|card| card.delete}
  29. if !params[:stripe_token].blank?
  30. customer.sources.create source: params[:stripe_token]
  31. end
  32. begin
  33. subscription = customer.subscriptions.create plan: plan_type
  34. rescue Stripe::CardError => e
  35. flash[:error] = "Error: #{Rack::Utils.escape_html e.message}"
  36. redirect '/supporter'
  37. end
  38. site.plan_ended = false
  39. site.plan_type = plan_type
  40. site.stripe_subscription_id = subscription.id
  41. site.save_changes validate: false
  42. else
  43. begin
  44. customer = Stripe::Customer.create(
  45. source: params[:stripe_token],
  46. description: "#{site.username} - #{site.id}",
  47. email: site.email,
  48. plan: plan_type
  49. )
  50. rescue Stripe::CardError => e
  51. flash[:error] = "Error: #{Rack::Utils.escape_html e.message} This is likely caused by incorrect information, or an issue with your credit card. Please try again, or contact your bank."
  52. redirect '/supporter'
  53. end
  54. site.stripe_customer_id = customer.id
  55. site.stripe_subscription_id = customer.subscriptions.first.id
  56. site.plan_ended = false
  57. site.plan_type = plan_type
  58. site.save_changes validate: false
  59. end
  60. end
  61. if site.email
  62. if is_special_upgrade
  63. site.send_email(
  64. subject: "[Neocities] Your site has been upgraded to supporter!",
  65. body: Tilt.new('./views/templates/email/supporter_upgrade.erb', pretty: true).render(self)
  66. )
  67. redirect '/admin'
  68. end
  69. site.send_email(
  70. subject: "[Neocities] You've become a supporter!",
  71. body: Tilt.new('./views/templates/email/subscription.erb', pretty: true).render(
  72. self, {
  73. username: site.username,
  74. plan_name: Site::PLAN_FEATURES[params[:plan_type].to_sym][:name],
  75. plan_space: Site::PLAN_FEATURES[params[:plan_type].to_sym][:space].pretty,
  76. plan_bw: Site::PLAN_FEATURES[params[:plan_type].to_sym][:bandwidth].pretty
  77. })
  78. )
  79. end
  80. if is_special_upgrade
  81. flash[:success] = "#{site.username} has been upgraded to supporter."
  82. redirect '/admin'
  83. end
  84. redirect '/supporter/thanks'
  85. end
  86. get '/supporter/thanks' do
  87. require_login
  88. erb :'supporter/thanks'
  89. end
  90. get '/supporter/bitcoin/?' do
  91. @title = 'Bitcoin Supporter'
  92. erb :'supporter/bitcoin'
  93. end
  94. get '/supporter/paypal' do
  95. require_login
  96. redirect '/supporter' if parent_site.supporter?
  97. hash = paypal_recurring_authorization_hash
  98. if parent_site.paypal_token
  99. hash.merge! token: parent_site.paypal_token
  100. end
  101. ppr = PayPal::Recurring.new hash
  102. paypal_response = ppr.checkout
  103. if !paypal_response.valid?
  104. flash[:error] = 'There was an issue connecting to Paypal, please contact support.'
  105. redirect '/supporter'
  106. end
  107. redirect paypal_response.checkout_url
  108. end
  109. get '/supporter/paypal/return' do
  110. require_login
  111. if params[:token].nil? || params[:PayerID].nil?
  112. flash[:error] = 'Unknown error, could not complete the request. Please contact Neocities support.'
  113. end
  114. ppr = PayPal::Recurring.new(paypal_recurring_hash.merge(
  115. token: params[:token],
  116. payer_id: params[:PayerID]
  117. ))
  118. paypal_response = ppr.request_payment
  119. unless paypal_response.approved? && paypal_response.completed?
  120. flash[:error] = 'Unknown error, could not complete the request. Please contact Neocities support.'
  121. redirect '/supporter'
  122. end
  123. site = current_site.parent || current_site
  124. ppr = PayPal::Recurring.new(paypal_recurring_authorization_hash.merge(
  125. frequency: 1,
  126. token: params[:token],
  127. period: :monthly,
  128. reference: site.id.to_s,
  129. payer_id: params[:PayerID],
  130. start_at: 1.month.from_now,
  131. failed: 3,
  132. outstanding: :next_billing
  133. ))
  134. paypal_response = ppr.create_recurring_profile
  135. site.paypal_token = params[:token]
  136. site.paypal_profile_id = paypal_response.profile_id
  137. site.paypal_active = true
  138. site.plan_type = 'supporter'
  139. site.plan_ended = false
  140. site.save_changes validate: false
  141. redirect '/supporter/thanks'
  142. end
  143. def paypal_recurring_hash
  144. {
  145. ipn_url: "https://neocities.org/webhooks/paypal",
  146. description: 'Neocities Supporter - Monthly',
  147. amount: Site::PLAN_FEATURES[:supporter][:price].to_s,
  148. currency: 'USD'
  149. }
  150. end
  151. def paypal_recurring_authorization_hash
  152. paypal_recurring_hash.merge(
  153. return_url: "https://neocities.org/supporter/paypal/return",
  154. cancel_url: "https://neocities.org/supporter",
  155. ipn_url: "https://neocities.org/webhooks/paypal"
  156. )
  157. end
  158. def is_special_upgrade
  159. params[:username] && params[:plan_type] == 'special'
  160. end