1

I am using Stripe.js to make payment in my phoenix application.

I want to use the tokenized payment method, for this I have to pass the card information in a form and make a call to Stripe.createToken(card) method.

Once, I receive the token I want to use it in one of the controllers of my phoenix application. I don't have much experience with phoenix. Can somebody, suggest how to pass the token I receive from Stripe.createToken(card) into the controller?

code for page.html

<form action="/charge" method="post" id="payment-form">
  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element">
      <!-- a Stripe Element will be inserted here. -->
    </div>

    <!-- Used to display form errors -->
    <div id="card-errors" role="alert"></div>
    </div>
    <button class="btn btn-primary">Submit Payment</button>
  </form>

js code

// Create a Stripe client
var stripe = Stripe('pk_test_xx96UepEgmX12vaKbpJp1p70');

// Create an instance of Elements
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
  base: {
    color: '#32325d',
    lineHeight: '18px',
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: 'antialiased',
    fontSize: '16px',
    '::placeholder': {
      color: '#aab7c4'
    }
  },
  invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

// Create an instance of the card Element
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    console.log('card-result', result);
    if (result.error) {
      // Inform the user if there was an error
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server
    }
  });
3
  • You can use plugin to save the token and use that plugin in the controller. Commented Jan 2, 2018 at 13:22
  • 1
    Just do a regular ajax request, or redirect to another page and put the token in either a get or post variable. Commented Jan 2, 2018 at 14:39
  • Ok, it came to my mind but I didn't know whether it was the right approach. Thanks. Commented Jan 2, 2018 at 18:35

1 Answer 1

2

So, in your stripe.createToken().then() promise callback you can send the token to your server.

I use axios, but you can use plain JS XHR, or jquery, or whatever you want.

Example:

stripe.createToken(card).then(function(result) {
  console.log('card-result', result);
  if (result.error) {
    // Inform the user if there was an error
    var errorElement = document.getElementById('card-errors');
    errorElement.textContent = result.error.message;
  } else {
    axios.post('/my-route-to-controller', {card_token: result.token})
      .then(function (response) {
        // do something with success
      })
      .catch(function (error) {
        // do something with error
      })
  }
});

Then you make sure you have /my-route-to-controller in your Router and your controller function will look something like:

def create(conn, %{"card_token" => card_token}) do
  # do something with your `card_token`
end

Although you will probably want to submit more params. Just add them to the ajax request and match them in your controller.

Solution 2

Another option would be to add the token value to a hidden form input and just submit the form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.