Authorize.net:用户如何指定默认付款方式?我怎么猜?

时间:2018-03-15 03:04:02

标签: ruby authorize.net

当用户edits their payment methods on the hosted payment profile manage page时,他们可以输入多种付款方式和信用卡。无法选择默认值。

enter image description here

当我在收费前立即获取付款资料并省略付款资料ID以获取默认值时,会出错。

https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-payment-profile

  

注意:如果先前已将付款资料设置为默认付款资料,您可以使用customerProfileId作为唯一参数提交此请求。如果先前已指定默认付款资料,则仅使用客户资料ID提交此请求将导致返回默认付款资料的信息。如果没有将付款资料指定为默认付款资料,则未指定付款资料将导致错误。

irb(main):016:0> request = GetCustomerPaymentProfileRequest.new
irb(main):017:0> request.customerProfileId = @subscription.authorizenet.customer_profile_id
=> "1503823608"
irb(main):018:0> response = transaction.get_customer_payment_profile(request)
irb(main):019:0> response.messages.resultCode
=> "Error"
irb(main):020:0> response.messages.messages[0].text
=> "No default payment/shipping profile found."

那么如何获取默认的付款资料ID?是的,我可以所有支付配置文件,但是,我怎么知道客户打算使用哪一个?

1 个答案:

答案 0 :(得分:0)

这是不可能的。您应该显示另一个表单供用户选择默认值。完全迟钝。让我想要违反PCI合规性并且自己存储所有信用卡。

https://community.developer.authorize.net/t5/Integration-and-Testing/Get-Accept-Customer-Profile-Page/m-p/59069/highlight/true#M33670

  

客户无法在表单上指明他们希望将哪个配置文件作为默认配置文件。但是,您可以通过使用API​​执行getCustomerPaymentProfileListRequest来检索[所有]付款配置文件,然后以客户可以指示哪个应该是默认值的方式将该列表呈现给客户来实现此目的。然后,您将执行updateCustomerPaymentProfileRequest以将所选的配置文件设置为默认值。

我最后只允许一个付款资料。如果付款配置文件ID不存在,则显示ADD表单。如果是,则显示EDIT表单。唯一的缺点是它不允许用户在创建付款资料后在信用卡和银行帐户之间切换。

routes.rb
  resource :payment_profile, only: [:edit] do # Authorize.net customer profile & payment profile
    collection do
      get :anedit
      post :charge
    end 
  end
payment_profiles_controller.rb
class PaymentProfilesController < ApplicationController
  include AuthorizeNet::API
  before_action :set_subscription
  layout false, only: :anedit

  def edit
    unless @subscription.authorizenet
      create_empty_customer_profile
    else
      get_customer_profile # update profile as they may have added a payment profile (or not)
    end
  end

  def anedit
    unless @subscription.authorizenet # in case #edit failed to create a new empty profile
      render plain: "No customer profile available to edit. Please try again later." and return
    end
    @payment_profile_id = @subscription.authorizenet.customer_payment_profile_id
    if Rails.env == 'production'
      if @payment_profile_id
        @action_url = "https://accept.authorize.net/customer/editPayment"
      else
        @action_url = "https://accept.authorize.net/customer/addPayment"
      end
    else
      if @payment_profile_id
        @action_url = "https://test.authorize.net/customer/editPayment"
      else
        @action_url = "https://test.authorize.net/customer/addPayment"
      end
    end

    # show hosted form
    ...
    setting5 = SettingType.new
    setting5.settingName = SettingNameEnum::HostedProfileBillingAddressRequired
    setting5.settingValue = true

    settings = Settings.new([setting1, setting2, setting3, setting4, setting5, setting6])

    request = GetHostedProfilePageRequest.new
    request.customerProfileId = @subscription.authorizenet.customer_profile_id
    request.hostedProfileSettings = settings

    response = transaction.get_hosted_profile_page(request)

    if response.messages.resultCode == MessageTypeEnum::Ok
      # puts "Successfully got Accept Customer page token."
      # puts "  Response code: #{response.messages.messages[0].code}"
      # puts "  Response message: #{response.messages.messages[0].text}"
      # puts "  Token: #{response.token}"
      @token = response.token
    else
      # puts "#{response.messages.messages[0].code}"
      # puts "#{response.messages.messages[0].text}"
      render plain: "Failed to get hosted profile page with customer profile ID #{request.customerProfileId}: #{response.messages.messages[0].code} #{response.messages.messages[0].text}"
      return
    end
  end

  # create authorize.net customer profile
  def create_empty_customer_profile
    # Build the request object
    request = CreateCustomerProfileRequest.new
    # Build the profile object containing the main information about the customer profile
    request.profile = CustomerProfileType.new
    request.profile.merchantCustomerId = @user.id
    request.profile.email = @user.email

    response = transaction.create_customer_profile(request)

    if response != nil
      puts response.messages.resultCode
      if response.messages.resultCode == MessageTypeEnum::Ok
        puts "Successfully created a customer profile with id: #{response.customerProfileId}"
        puts "  Customer Payment Profile Id List:"
        response.customerPaymentProfileIdList.numericString.each do |id|
          puts "    #{id}"
        end
        puts "  Customer Shipping Address Id List:"
        response.customerShippingAddressIdList.numericString.each do |id|
          puts "    #{id}"
        end
        @subscription.create_authorizenet user: @user, customer_profile_id: response.customerProfileId #, customer_payment_profile_id: response.customerPaymentProfileIdList.numericString.first
      else
        puts response.messages.messages[0].code
        puts response.messages.messages[0].text
        flash.now.alert = "Failed to create a new customer profile: #{response.messages.messages[0].code} #{response.messages.messages[0].text}"
        #render :new
      end
    else
      puts "Response is null"
      flash.now.alert = "Failed to create a new customer profile."
    end
  end

  def get_customer_profile
    request = GetCustomerProfileRequest.new
    request.customerProfileId = @subscription.authorizenet.customer_profile_id

    response = transaction.get_customer_profile(request)

    if response.messages.resultCode == MessageTypeEnum::Ok
      puts "Successfully retrieved customer profile of customer ID #{request.customerProfileId}."
      response.profile.paymentProfiles.each do |paymentProfile|
        puts "  Payment Profile ID #{paymentProfile.customerPaymentProfileId}" 
        puts "  Payment Details:"
        if paymentProfile.billTo != nil
          puts "    Last Name: #{paymentProfile.billTo.lastName}"
          puts "    Address: #{paymentProfile.billTo.address}"    
        end
      end
      if response.subscriptionIds != nil && response.subscriptionIds.subscriptionId != nil
        puts "  List of subscriptions: "
        response.subscriptionIds.subscriptionId.each do |subscriptionId|
          puts "    #{subscriptionId}"
        end
      end
      # now update the payment profile id
      @subscription.authorizenet.update customer_payment_profile_id: response.profile.paymentProfiles.first&.customerPaymentProfileId
    else
      puts response.messages.messages[0].text
      flash.alert = "Failed to get profile of customer ID #{request.customerProfileId}."
    end 
    return response
  end

和观点

edit.haml
-content_for :head do
  %meta(name="turbolinks-cache-control" content="no-preview")
%iframe{src: anedit_user_subscription_payment_profile_path(@user), width: '100%', height: '900px', frameborder: 0}
anedit.haml
%form#authorizenetform{:action => @action_url, :method => "post"}
  %input{:type => "hidden", :name => "token", :value => @token}/
  -if @payment_profile_id
    %input{:type => "hidden", :name => "paymentProfileId", :value => @payment_profile_id}/ 
  %input{:type => "submit", :value => "Update Payment"}/
-# Separate iframe document without layout. No access to JQuery.
:javascript
  document.getElementById('authorizenetform').submit();