使用Paypal定期更新宝石的订阅计划

xps15z

我为Stripe设置了此设置,但对于PayPal重复性宝石,我似乎无法弄清楚。我希望允许用户更改其付款计划。如果用户已订阅1个月的订阅(plan_id 1),则他们应该能够升级到Year的订阅(plan_id 12)。

任何帮助,将不胜感激!

订阅控制器

  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 you for 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 updatesubscription
      @user = current_user
      @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
      if @user.subscription.plan_id == 12
      @customer.update_subscription(:plan => "1", :prorate => true)
      current_user.subscription.update_attributes(:plan_id => 1)
      flash.alert = 'Your subscription has been changed to monthly!'
      redirect_to root_url
    elsif @user.subscription.plan_id == 1
      @customer.update_subscription(:plan => "12", :prorate => true)
      current_user.subscription.update_attributes(:plan_id => 12)
     current_user.save!
      flash.alert = 'Your subscription has been changed to annually!'
      redirect_to root_url
    end
     end

     def cancelsubscription
       @user = current_user
         @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
         @customer.cancel_subscription()
         current_user.subscription.update_attributes(:cancelled => 1)
         current_user.save!
         flash.alert = 'Your subscription has been cancelled successfully!'
         redirect_to root_url
       end

       def showcard
         @user = current_user
         Stripe::Customer.retrieve(@user.subscription.stripe_customer_token).cards.all()
       end

       def changecard
           @user = current_user       
           @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)

             card = @customer.cards.create({
               :card => @user.subscription.stripe_customer_token
             })

             @customer.default_card = card
             @customer.save
           end

           def suspend
             @user = current_user
             @user.subscription.suspend_paypal
             current_user.subscription.update_attributes(:cancelled => 1)
               flash.alert = 'Billing has been suspended!'
                redirect_to root_url
           end

           def reactivate
             @user = current_user
             @user.subscription.reactivate_paypal
             current_user.subscription.update_attributes(:cancelled => nil)
               flash.alert = 'Billing has been activated!'
                redirect_to root_url
           end

         def updatebilling
             @user = current_user
             customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
             customer.cards.retrieve("#{@user.subscription.stripe_card_id}").delete() 
             customer.cards.create({
                   card: {
                   number: params[:user][:scardnumber],
                   exp_month: params[:user][:sexp_month],
                   exp_year: params[:user][:sexp_year],
                   cvc: params[:user][:scvc],
                   name: params[:user][:sname],
                   address_line1: params[:user][:sbilling_address1],
                   address_line2: params[:user][:sbilling_address2],
                   address_city: params[:user][:saddress_city],
                   address_zip: params[:user][:saddress_zip],
                   address_state: params[:user][:saddress_state],
                   address_country: params[:user][:saddress_country]
                   }
                 })
                 if customer.save!
                   @user.stripe_card_id = customer.active_card.id
                   @user.save!
                   flash.alert = 'Billing information updated successfully!'
                   redirect_to root_url
                 else
                   flash.alert = 'Stripe error'
                   redirect_to root_url
                 end
               end
end

PayPal付款模式:

   def initialize(subscription)
      @subscription = subscription
    end

    def checkout_details
      process :checkout_details
    end

    def checkout_url(options)
      process(:checkout, options).checkout_url
    end

    def make_recurring
      process :request_payment
      process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.zone.now
    end

    def suspend
        process :suspend, :profile_id => @subscription.paypal_recurring_profile_token
    end

    def reactivate
        process :reactivate, :profile_id => @subscription.paypal_recurring_profile_token
    end

  private

    def process(action, options = {})
      options = options.reverse_merge(
        token: @subscription.paypal_payment_token,
        payer_id: @subscription.paypal_customer_token,
        description: @subscription.plan.name,
        amount: @subscription.plan.price,
        currency: "USD"
      )
      response = PayPal::Recurring.new(options).send(action)
      raise response.errors.inspect if response.errors.present?
      response
    end
  end

订阅模式:

  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 was a problem with your credit card."
    false
  end

  def payment_provided?
    stripe_card_token.present? || paypal_payment_token.present?
  end

  def suspend_paypal
    paypal.suspend
    save
  end


  def reactivate_paypal
    paypal.reactivate
    save
  end
end
亚伦

不幸的是,无法在PayPal上更新更改订阅的条款。必须取消此订阅并设置一个新的订阅。

API参考UpdateRecurringPaymentsProfile

您将需要计算按比例分配的差额,并INITAMT在设置新配置文件时向他们收取费用,然后设置新的开始日期,该日期AMT将在其期限到期后收集。

编辑:这是假设您正在使用此gem由于我找不到任何尝试为您自动执行此操作的地方。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Paypal定期宝石-暂停付款

来自分类Dev

PayPal订阅API:使用curl检索现有计费计划ID的信息

来自分类Dev

Paypal定期订阅购物车自动取消总是发生

来自分类Dev

使用Paypal Merchant SDK或Paypal REST API进行定期付款

来自分类Dev

贝宝(Paypal)API-更新定期付款资料

来自分类Dev

如何使用Windows Task Scheduler设置XLSM文件的定期计划

来自分类Dev

PayPal Rest API-更新计费计划返回URL

来自分类Dev

在PayPal中实施定期订阅造成麻烦,无法获取profileid作为响应

来自分类Dev

在PayPal中实施定期订阅造成麻烦,无法获得profileid作为响应

来自分类Dev

贝宝(Paypal)一项定期结算中的多个订阅

来自分类Dev

结合使用Stripe Intents API和计费(定期订阅)

来自分类Dev

使用带条带动态计划的会员订阅

来自分类Dev

使用带条带动态计划的会员订阅

来自分类Dev

授权定期计费订阅

来自分类Dev

使用试用和折扣代码的Paypal定期结算

来自分类Dev

使用Paypal的iOS信用卡定期付款

来自分类Dev

使用IPN模拟器发送Paypal定期付款命令

来自分类Dev

尝试使用ActiveMerchant设置Paypal定期付款时,“令牌无效”

来自分类Dev

使用IPN模拟器发送Paypal定期付款命令

来自分类Dev

使用试用和折扣码的Paypal定期结算

来自分类Dev

禁用使用PayPal进行付款的访客结帐以进行定期付款

来自分类Dev

PayFlow PayPal定期付款

来自分类Dev

如何使用流星定期更新变量

来自分类Dev

使用QThread定期更新QTableWidget pyqt

来自分类Dev

使用Workmanager的Android定期位置更新

来自分类Dev

如何使用PayPal Payflow托管页面实施PayPal定期付款?不使用IPN

来自分类Dev

使用Powershell 4.0更新计划的任务操作

来自分类Dev

PayPal Sandbox使用REST API对订阅进行否定测试

来自分类Dev

使用Svelte / RxJs / RxFire订阅文档。如何更新订阅

Related 相关文章

  1. 1

    Paypal定期宝石-暂停付款

  2. 2

    PayPal订阅API:使用curl检索现有计费计划ID的信息

  3. 3

    Paypal定期订阅购物车自动取消总是发生

  4. 4

    使用Paypal Merchant SDK或Paypal REST API进行定期付款

  5. 5

    贝宝(Paypal)API-更新定期付款资料

  6. 6

    如何使用Windows Task Scheduler设置XLSM文件的定期计划

  7. 7

    PayPal Rest API-更新计费计划返回URL

  8. 8

    在PayPal中实施定期订阅造成麻烦,无法获取profileid作为响应

  9. 9

    在PayPal中实施定期订阅造成麻烦,无法获得profileid作为响应

  10. 10

    贝宝(Paypal)一项定期结算中的多个订阅

  11. 11

    结合使用Stripe Intents API和计费(定期订阅)

  12. 12

    使用带条带动态计划的会员订阅

  13. 13

    使用带条带动态计划的会员订阅

  14. 14

    授权定期计费订阅

  15. 15

    使用试用和折扣代码的Paypal定期结算

  16. 16

    使用Paypal的iOS信用卡定期付款

  17. 17

    使用IPN模拟器发送Paypal定期付款命令

  18. 18

    尝试使用ActiveMerchant设置Paypal定期付款时,“令牌无效”

  19. 19

    使用IPN模拟器发送Paypal定期付款命令

  20. 20

    使用试用和折扣码的Paypal定期结算

  21. 21

    禁用使用PayPal进行付款的访客结帐以进行定期付款

  22. 22

    PayFlow PayPal定期付款

  23. 23

    如何使用流星定期更新变量

  24. 24

    使用QThread定期更新QTableWidget pyqt

  25. 25

    使用Workmanager的Android定期位置更新

  26. 26

    如何使用PayPal Payflow托管页面实施PayPal定期付款?不使用IPN

  27. 27

    使用Powershell 4.0更新计划的任务操作

  28. 28

    PayPal Sandbox使用REST API对订阅进行否定测试

  29. 29

    使用Svelte / RxJs / RxFire订阅文档。如何更新订阅

热门标签

归档