2017-07-18 63 views
0

你好,我有給定的代碼如何改變軌道response.message

def create_profile(payment) 
     return unless payment.source.gateway_customer_profile_id.nil? 
     options = { 
     email: payment.order.email, 
     login: preferred_secret_key, 
     }.merge! address_for(payment) 

     source = update_source!(payment.source) 
     if source.number.blank? && source.gateway_payment_profile_id.present? 
     creditcard = source.gateway_payment_profile_id 
     else 
     creditcard = source 
     end 

     response = provider.store(creditcard, options) 
     if response.success? 
     cc_type=payment.source.cc_type 
     response_cc_type = response.params['sources']['data'].first['brand'] 
     cc_type = CARD_TYPE_MAPPING[response_cc_type] if CARD_TYPE_MAPPING.include?(response_cc_type) 

     payment.source.update_attributes!({ 
      cc_type: cc_type, # side-effect of update_source! 
      gateway_customer_profile_id: response.params['id'], 
      gateway_payment_profile_id: response.params['default_source'] || response.params['default_card'] 
     }) 

     else 
     payment.send(:gateway_error, response.message) 
     end 
    end 

我需要response.message改變消息,使用response = [ { message: "fraud card"} ].to_json我試過,但它給錯誤`

undefined method `message' for "[{"message":"fraud card"}]":String 

我還曾試圖response.message = 'fraud error',仍然提示錯誤。我得到的迴應是:

params: 
    error: 
    message: Your card was declined. 
    type: card_error 
    code: card_declined 
    decline_code: fraudulent 
    charge: ch_1AgncyJEfCzWOpKDdoxn1x1R 
message: Your card was declined. 
success: false 
test: false 
authorization: ch_1AgncyJEfCzWOpKDdoxn1x1R 
fraud_review: 
error_code: card_declined 
emv_authorization: 
avs_result: 
    code: 
    message: 
    street_match: 
    postal_match: 
cvv_result: 
    code: 
    message: 

現在,我的要求是,以檢查是否decline_code是欺詐比我的消息應該是fraud error。請讓我知道如何改變這一點。

+0

行的事response.message返回一個字符串?是否響應對象有消息二傳手(我猜沒有)? –

回答

0

基於您的評論,你使用盛宴網關。通過傳遞一個字符串,而不是正確的響應對象,您的解決方案規避了Spree's default implementation其中記錄錯誤信息的響應。

我會做的,而不是爲下Spree's suggested approach for logic customization適應gateway_error方法您的需求:

# app/models/spree/payment_decorator.rb 
Spree::Payment.class_eval do 
    private 

    def gateway_error(error) 
    if error.is_a? ActiveMerchant::Billing::Response 
     # replace this with your actual implementation, e.g. based on response.params['error']['code'] 
     text = 'fraud message' 
    elsif error.is_a? ActiveMerchant::ConnectionError 
     text = Spree.t(:unable_to_connect_to_gateway) 
    else 
     text = error.to_s 
    end 
    logger.error(Spree.t(:gateway_error)) 
    logger.error(" #{error.to_yaml}") 
    raise Core::GatewayError.new(text) 
    end 
end 

這不是因爲它最乾淨的實現並複製&膏現有的代碼。但是,這只是施普雷如何(我已經實現並促成了多個施普雷商店和定製邏輯時,特別是民營邏輯,它總是有點痛苦)。

希望有所幫助。

+0

我使用的是給定的寶石https://github.com/spree/spree_gateway – railslearner

+0

我已根據您的評論和Spree的經驗更新了我的建議。 –