Sha256: b41a8a37ab2b3b65a14b85e7166da03609eb062581f4b20f25af5e16468d9896

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

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)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
supercharged-2.0.4 app/assets/javascripts/charge_form.js.coffee.erb
supercharged-2.0.3 app/assets/javascripts/charge_form.js.coffee.erb
supercharged-2.0.2 app/assets/javascripts/charge_form.js.coffee.erb
supercharged-2.0.1 app/assets/javascripts/charge_form.js.coffee.erb
supercharged-2.0.0 app/assets/javascripts/charge_form.js.coffee.erb