0
我創造新的支付有redirect_to的後無重定向@Contract
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
通的所有數據URL支付服務器與阿賈克斯。
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 + ")")
)
run方法後支付/ 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
如果successed婚前狀態應該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"}
如何正確redirect_to的@Contract,如果婚前狀態Successed?感謝您的提前。
我不知道如何。我使用mangopay的standart js庫:* mangoPay.cardRegistration.sendDataWithAjax * – dev85
可以顯示示例 – dev85