app/controllers/ishapi/payments_controller.rb in ishapi-0.1.8.171 vs app/controllers/ishapi/payments_controller.rb in ishapi-0.1.8.172

- old
+ new

@@ -41,76 +41,83 @@ puts! e, 'e' render :status => 404, :json => {} end end - ## This is for guyd _vp_ 20200721 + ## This is for guyd _vp_ 2020-07-21 + ## It's been a while! _vp_ 2022-03-01 def create2 authorize! :create, ::Ish::Payment + current_user.profile.update_attributes({ is_purchasing: true }) begin amount_cents = 503 # @TODO: change - ::Stripe.api_key = STRIPE_SK + ::Stripe.api_key = ::STRIPE_SK intent = Stripe::PaymentIntent.create({ amount: amount_cents, currency: 'usd', metadata: { integration_check: "accept_a_payment" }, }) payment = Ish::Payment.create!( client_secret: intent.client_secret, - email: @current_user.email, + email: current_user.email, payment_intent_id: intent.id, - profile_id: @current_user.profile.id ) + profile_id: current_user.profile.id ) render json: { client_secret: intent.client_secret } rescue Mongoid::Errors::DocumentNotFound => e - puts! e, 'e' + puts! e, '#create2 Mongoid::Errors::DocumentNotFound' render :status => 404, :json => e end end + ## ## webhook + ## def stripe_confirm authorize! :open_permission, ::Ishapi payload = request.body.read event = nil begin event = Stripe::Event.construct_from(JSON.parse(payload, symbolize_names: true)) rescue StandardError => e - puts! e, 'e' + puts! e, 'could not #stripe_confirm' render status: 400, json: { status: :not_ok } return end payment_intent = event.data.object payment = Ish::Payment.where( payment_intent_id: payment_intent.id ).first if payment && payment_intent['status'] == 'succeeded' + payment.update_attributes( status: :confirmed ) - payment.profile.update_attributes!( n_unlocks: payment.profile.n_unlocks + 5 ) + n_unlocks = payment.profile.n_unlocks + 5 + + payment.profile.update_attributes!( n_unlocks: n_unlocks, is_purchasing: false ) # @TODO: it's not always 5? adjust end render status: 200, json: { status: :ok } end def unlock authorize! :unlock, ::Ish::Payment item = Object::const_get(params['kind']).find params['id'] - puts! params, 'unlocking...' - existing = Purchase.where( user_profile: @current_user.profile, item: item ).first if existing render status: 200, json: { status: :ok, message: 'already purchased' } return end - @current_user.profile.update_attributes n_unlocks: @current_user.profile.n_unlocks - 1 # @TODO: the number is variable + @current_user.profile.inc( n_unlocks: -item.premium_tier ) + purchase = ::Gameui::PremiumPurchase.create!( item: item, user_profile: @current_user.profile, ) - render status: 200, json: { status: :ok } + @profile = @current_user.profile + render 'ishapi/users/account' end end end