class @SuperchargedForm constructor: (selector, @options = {}) -> @form = $(selector) return if @form.length == 0 @amount_input = @form.find("[role='charge-amount']") @gateway_name_input = @form.find("[role='charge-gateway-name']") @id_input = if @form.find("[role='charge-id']").length then @form.find("[role='charge-id']") else @form.find("[value='<%= Supercharged::ChargesHelper::FAKE_ORDER_ID %>']") @form.submit => valid = @startPayment() unless valid return false unless parseInt(@id_input.val()) alert("Error: undefined charge id") return false startPayment: -> valid = true @createInternalTransaction( success: (charge)=> @prepareGatewayForm(charge) validationError: (errors)=> valid = false handler = @options.onValidationError handler.call(this, errors) if handler? ) valid createInternalTransaction: (options) -> charge_attributes = @getChargeAttributes() $.ajax( url: "/charges.json", type: "POST", async: false, data: {charge: charge_attributes}, success: (response) -> options.success(response) error: (jqXHR, status, errorThrown)=> return unless jqXHR.status == 422 response = $.parseJSON(jqXHR.responseText) options.validationError(response.errors) ) getChargeAttributes: -> { amount: @amount_input.val(), gateway_name: @gateway_name_input.val() } prepareGatewayForm: (charge) -> @id_input.val(charge.id)