ruby-on-rails - 使用 paypal 定期 gem 升级订阅计划

标签 ruby-on-rails paypal

我对 Stripe 有这样的设置,但我似乎无法弄清楚 PayPal 循环 gem 的设置。我希望允许用户切换他们的付款计划。如果用户订阅了 1 个月订阅 (plan_id 1),他们应该能够升级到一年订阅 (plan_id 12)。

如有任何帮助,我们将不胜感激!

订阅 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 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 Reference对于 UpdateRecurringPaymentsProfile

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

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

关于ruby-on-rails - 使用 paypal 定期 gem 升级订阅计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670881/

相关文章:

ruby-on-rails - 单一 Ruby on Rails 表单中的基本和付费注册

c# - Paypal 返回 URL 未正确触发

javascript - 带有服务器集成的 PayPal 客户端 JavaScript SDK - 设置付款金额

ruby-on-rails - Rails - 使用 ajax 上传图片

ruby-on-rails - 在运行时使用虚拟属性扩充 Rails 模型

ruby-on-rails - aws-s3 gem 除非定义? @@{ :instance_writer=>true}

ruby-on-rails - 替换 URI.escape 以避免 Lint/UriEscapeUnescape 警告?

ruby-on-rails - 如何使我的 Google map 叠加层更高(在 Rails 中)

php - 查看 Paypal API - 什么是 cURL?

Postman 的 PayPal 无效客户端错误 - 沙盒模式