redirect_to @contract后没有重定向

时间:2016-08-02 12:12:48

标签: ruby-on-rails ruby

我创建了新的付款

def new
    @contract = Contract.find(session[:contract_id])
    # Register card for user
    @card_preregistration = MangoPay::CardRegistration.create({
        UserId: current_user.mangopay_id,
        Currency: "EUR",
        CardType: "CB_VISA_MASTERCARD"
      })
    session[:card_id] = @card_preregistration['Id']
    locals card_reg: @card_preregistration
  end

使用ajax将所有数据传递到网址付款服务器。

register_card.coffee

jQuery ->
  $('#payment-form').submit (e) ->
    e.preventDefault()  
    $this = $(this)

    mangoPay.cardRegistration.init
      cardRegistrationURL: $("#CardRegistrationURL").val()
      preregistrationData: $("#PreregistrationData").val()
      accessKey: $("#AccessKey").val()

    cardData = {
      cardNumber: $("#card_number").val()
      cardExpirationDate: $("#card_expiration_date").val()
      cardCvx: $("#cardCvx").val()
    }

    mangoPay.cardRegistration.sendDataWithAjax(
      # URL to capture response
      "http://site:8080/finialize",
      # Card data
      cardData,
      # Result Ajax callback
      (data) ->
        #console.log(data)
      # Error Ajax callback
      (xhr, status, error) -> 
        alert("Payment error : " + xhr.responseText + " (" + status + " - " + error + ")")

    )

运行方法 payment / finialize_payment

def finialize_payment
    @contract = Contract.find(session[:contract_id])  
  begin
    card_registration = MangoPay::CardRegistration.update(session[:card_id], {
      RegistrationData: "data=#{params['data']}",
      Tag: "custom tag"
    })

    if card_registration['Status'] != "VALIDATED" 
      flash[:error] = "Cannot create card. Payment has not been created."
    end
    #get created virtual card object
    card = MangoPay::Card.fetch(card_registration['CardId'])

    # create temporary wallet for user
    wallet = MangoPay::Wallet.create({
      Owners: [card_registration['UserId']],
      Currency: 'EUR',
      Description: 'Temporary wallet for payment demo'
    })
    # create pay-in CARD DIRECT
    payIn = MangoPay::PayIn::Card::Direct.create({
      CardId: card['Id'], 
      CreditedWalletId: wallet['Id'],
      CardType: 'CB_VISA_MASTERCARD',
      Culture: 'FR',
      AuthorId: card_registration['UserId'],
      ReturnURL: 'http://localhost:8080/',
      DebitedFunds: { Amount: @contract.amount.to_i, Currency: 'EUR' },
      Fees: {Amount: 0, Currency: 'EUR'},
      #payment type as CARD
      PaymentDetails: {CardType: card['CardType'], CardId: card['Id']},
      #execution type as DIRECT
      SecureModeReturnURL: 'http://test.site'
    })
     #if created Pay-in object has status SUCCEEDED it's mean that all is fine
     if payIn['Status'] == 'SUCCEEDED'
        redirect_to @contract
        flash[:notice] = "Pay-In has been created successfully."        
     else
       # if created Pay-in object has status different than SUCCEEDED 
        # that occurred error and display error message
        flash[:notice] = "Pay-In has been created with status: #{payIn['Status']}"
     end

  rescue MangoPay::ResponseError => e 
    flash[:error] = " Code: #{ e.code } Message: #{ e.message }"
  end

  end

如果payIn状态成功,则应该是redirect_to @contract 但该页面未重新加载。控制台显示渲染合同/显示:

Started GET "/finialize?data=fq7ztNH9ztspcfzpUGj0_V3LhW5PKCuOSJd3CnWIdMfxq6ij__ENfQKBL_aHSaveqk7FwpB65dRgiot-92qsK0CUwTIbKLWEd9f-weFksTiJZU28-RIz5QNUh_6FYHM7_uh-M22NjZ6dU5YsJBBYuA" for 10.240.0.195 at 2016-08-02 11:50:35 +0000
Cannot render console from 10.240.0.195! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PaymentsController#finialize_payment as */*
  Parameters: {"data"=>"fq7ztNH9ztspcfzpUGj0_V3LhW5PKCuOSJd3CnWIdMfxq6ij__ENfQKBL_aHSaveqk7FwpB65dRgiot-92qsK0CUwTIbKLWEd9f-weFksTiJZU28-RIz5QNUh_6FYHM7_uh-M22NjZ6dU5YsJBBYuA"}
  Contract Load (3.4ms)  SELECT  "contracts".* FROM "contracts" WHERE "contracts"."id" = $1 LIMIT 1  [["id", 10]]
Redirected to http://localhost.io:8080/contracts/e7834c9a
Completed 302 Found in 2773ms (ActiveRecord: 3.4ms)


Started GET "/contracts/e7834c9a" for 10.240.1.18 at 2016-08-02 11:50:38 +0000
Cannot render console from 10.240.1.18! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ContractsController#show as */*
  Parameters: {"id"=>"e7834c9a"}

如果payIn状态为Successed,redirect_to @contract的正确程度如何?谢谢你提前。

1 个答案:

答案 0 :(得分:0)

如果我是你,我会让服务器返回某种json对象来指示finalize方法是否顺利。有了服务器的这个json有效负载,你可以让javascript指示它应该重定向到哪个页面。