2016-02-27 38 views
0

我是ruby/rails/spree的新手。我正在使用spree-3.0.7實施印度支付網關。 我能夠處理訂單,但付款狀態始終爲balance_due。 控制器代碼Spree offsite payments Payu.in集成:如何將訂單標記爲付款

def confirm 
    payment_method = Spree::PaymentMethod.find(payment_method_id) 

    Spree::LogEntry.create({ 
    source: payment_method, 
    details: params.to_yaml 
    }) 

    order = current_order || raise(ActiveRecord::RecordNotFound) 

    if(address = order.bill_address || order.ship_address) 
    firstname = address.firstname 
    end 

    #confirm for correct hash and order amount requested before marking an payment as 'complete' 
    checksum_matched = payment_method.checksum_ok?([params[:status], '', '', '', '', '', '', params[:udf4], params[:udf3], params[:udf2], params[:udf1], order.email, firstname, @productinfo, params[:amount], params[:txnid]], params[:hash]) 
    if !checksum_matched 
    flash.alert = 'Malicious transaction detected.' 
    redirect_to checkout_state_path(order.state) 
    return 
    end 
    #check for order amount 
    if !payment_method.amount_ok?(order.total, params[:amount]) 
    flash.alert = 'Malicious transaction detected. Order amount not matched.' 
    redirect_to checkout_state_path(order.state) 
    return 
    end 

    payment = order.payments.create!({ 
    source_type: 'Spree::Gateway::Payumoney',#could be something generated by system 
    amount: order.total, 
    payment_method: payment_method 
    }) 
    payment.started_processing! 
    payment.pend! 

    order.next 
    order.update_attributes({:state => "complete", :completed_at => Time.now}) 

    if order.complete? 
    order.update! 
    flash.notice = Spree.t(:order_processed_successfully) 

    redirect_to order_path(order) 
    return 
    else 
    redirect_to checkout_state_path(order.state) 
    return 
    end 
end 

網關/型號代碼

require "offsite_payments" 
module Spree 
    class Gateway::Payumoney < Gateway 
    preference :merchant_id, :string 
    preference :secret_key, :string 


    def provider_class 
    ::OffsitePayments.integration('Payu_In') 
    end 

    def provider 
    #assign payment mode 
    OffsitePayments.mode = preferred_test_mode == true ? :test : :production 
    provider_class 
    end 

    def checksum(items) 
    provider_class.checksum(preferred_merchant_id, preferred_secret_key, items) 
    end  

    def auto_capture? 
    true 
    end 

    def method_type 
    "payumoney" 
    end 

    def support?(source) 
    true 
    end 

    def authorization 
    self 
    end 

    def purchase(amount, source, gateway_options={}) 
    ActiveMerchant::Billing::Response.new(true, "payumoney success") 
    end 

    def success? 
    true 
    end 

    def txnid(order) 
    order.id.to_s + order.number.to_s 
    end 

    def service_provider 
    "payu_paisa" 
    end 

    def checksum_ok?(itms, pg_hash) 
    Digest::SHA512.hexdigest([preferred_secret_key, *itms, preferred_merchant_id].join("|")) == pg_hash 
    end 

    def amount_ok?(order_total, pg_amount) 
    BigDecimal.new(pg_amount) == order_total 
    end 
end 

在大禮包支付DOC https://guides.spreecommerce.com/developer/payments.html他們已經提到,如果auto_capture?回報true然後購買方法將被調用,但購買法是沒有得到調用。 任何人都可以指向我正確的方向?

回答

0

我通過標記支付作爲完整的固定的問題。

刪除

payment.started_processing! 
payment.pend! 

添加

payment.complete 

上述

order.next 

我在github上發表了我的代碼寶石 https://github.com/isantoshsingh/spree_payumoney

+0

請參閱此[評論](http://stackoverflow.com/questions/35666949/spree-offsite-付款一體化如何標記一個訂單付費/ 35739951?noredirect = 1#comment59155688_35739951)一次。 – jaspreet21anand

0

不必調用以下命令 payment.started_processing! payment.pend!

剛剛離開其初始狀態的付款。即checkout狀態並完成您的訂單。

因爲當訂單完成process_payments!被調用。 這種方法處理未處理的支付,其標準是像下面 def unprocessed_payments payments.select { |payment| payment.checkout? } end

希望這能解決你的情況:)

+0

但'purchase'方法爲n不打電話。我有幾次失敗的嘗試,沒有這兩個命令。 – Santosh

+0

可能是因爲您已經使用'update_attributes'來標記'complete'命令而不是使用'state_machine'。 嘗試'while order.next; end_'而不是'order.update_attributes({:state =>「complete」,:completed_at => Time.now})' 如果您檢查'checkout.rb',在order_transits時'state_machine'中有回調用於payment_processing到完成狀態。 – jaspreet21anand

+0

我試過'while order.next;結束「,但這甚至沒有將訂單標記爲完整。 – Santosh