App.addChild('MoipForm', {
el: 'form.moip',
getMoipToken: function(onSuccess){
var that = this;
if($('#MoipWidget').length > 0) {
if(_.isFunction(onSuccess)){
onSuccess();
}
} else {
$.post('/payment/moip/' + this.backerId + '/get_moip_token').success(function(response, textStatus){
that.paymentChoice.$('input').attr('disabled', 'disabled');
if(response.get_token_response.status == 'fail'){
that.checkoutFailure({Code: 0, Mensagem: response.get_token_response.message});
}
else{
$('#catarse_moip_form').prepend(response.widget_tag);
if(_.isFunction(onSuccess)){
onSuccess(response);
}
}
});
}
},
checkoutFailure: function(data) {
this.loader.hide();
var response_data = (data.length > 0 ? data[0] : data);
if(response_data.Codigo == 914){
response_data.Mensagem += '. Tente recarregar a página e repetir a operação de pagamento.';
}
this.message.find('p').html(response_data.Mensagem);
this.message.fadeIn('fast');
$('input[type="submit"]').removeAttr('disabled').show();
},
checkoutSuccessful: function(data) {
var that = this;
$.post('/payment/moip/' + this.backerId + '/moip_response', {response: data}).success(function(){
that.loader.hide();
// Bail out when get an error from MoIP
if(data.Status == 'Cancelado'){
return that.checkoutFailure({Codigo: 0, Mensagem: data.Classificacao.Descricao + '. Verifique os dados de pagamento e tente novamente.'})
}
// Go on otherwise
if(data.url && $('#payment_type_cards_section').css('display') != 'block') {
var link = $(''+data.url+'')
link.attr('href', data.url);
$('.link_content:visible').empty().html(link);
$('.payment_section:visible .subtitle').fadeIn('fast');
}
else {
var thank_you = $('#project_review').data('thank-you-path');
if(thank_you){
location.href = thank_you;
}
else {
location.href = '/';
}
}
});
},
activate: function(){
this.message = this.$('.next_step_after_valid_document .alert-danger');
this.backerId = $('input#backer_id').val();
this.projectId = $('input#project_id').val();
this.loader = this.$('.loader');
window.checkoutSuccessful = _.bind(this.checkoutSuccessful, this);
window.checkoutFailure = _.bind(this.checkoutFailure, this);
}
});