2017-07-18 60 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的经验更新了我的建议。 –