ruby on rails - How to call in the view for User to cancel Stripe subscription -
i new rails , trying figure out how setup view user cancel subscription or update (to higher or lower plan). added code subscription controller, not understanding how call in view can take action switching plan 1 plan 12 (monthly yearly) , cancel subscription altogether.
the code in controller should allow user cancel subscription or change plan. need creating view action it.
subscriptions controller:
def new plan = plan.find(params[:plan_id]) @subscription = plan.subscriptions.build if params[:payerid] @subscription.paypal_customer_token = params[:payerid] @subscription.paypal_payment_token = params[:token] @subscription.email = @subscription.paypal.checkout_details.email end end def create @subscription = subscription.new(params[:subscription]) if @subscription.save_with_payment redirect_to @subscription, :notice => "thank subscribing!" else render :new end end def show @subscription = subscription.find(params[:id]) end def paypal_checkout plan = plan.find(params[:plan_id]) subscription = plan.subscriptions.build redirect_to subscription.paypal.checkout_url( return_url: new_subscription_url(:plan_id => plan.id), cancel_url: root_url ) end def update_subscription @customer = stripe::customer.retrieve(@user.stripe_customer_token) @subscription = customer.subscriptions.retrieve(@user.plan_id) @subscription.plan = params[:plan_id] subscription.save end def cancel_subscription @customer = stripe::customer.retrieve(@user.stripe_customer_token) @customer.subscriptions.retrieve(@user.plan_id).delete() end end routes:
resources :charges resources :subscriptions resources :plans 'paypal/checkout', to: 'subscriptions#paypal_checkout' subscription model:
belongs_to :plan belongs_to :subscription belongs_to :user validates_presence_of :plan_id validates_presence_of :email attr_accessor :stripe_card_token, :paypal_payment_token def save_with_payment if valid? if paypal_payment_token.present? save_with_paypal_payment else save_with_stripe_payment end end end def paypal paypalpayment.new(self) end def save_with_paypal_payment response = paypal.make_recurring self.paypal_recurring_profile_token = response.profile_id save! end def save_with_stripe_payment customer = stripe::customer.create(description: email, plan: plan_id, card: stripe_card_token) self.stripe_customer_token = customer.id save! rescue stripe::invalidrequesterror => e logger.error "stripe error while creating customer: #{e.message}" errors.add :base, "there problem credit card." false end def payment_provided? stripe_card_token.present? || paypal_payment_token.present? end end
you need add routes:
get "subscriptions/cancelsubscription"
then in view, <%= link_to "cancel subscription", subscriptions_cancelsubscription_path, :data => { :confirm => "are sure?" } %>
Comments
Post a Comment