0

I am working on an Ionic 3 app with WooCommerce api. Currently I am using stripe to handle payments...Been able to get a card token from stripe after initial card check but I am lost in creating a charge from the card.

Piece of my code

component.html

<ion-content>

<div class="form-row">
  <div id="card-element">
    <!-- a Stripe Element will be inserted here. -->
  </div>

  <!-- Used to display Element errors -->
  <div id="card-errors" role="alert"></div>
</div>

<button ion-button block large>Make Payment</button>

component.ts

export class CardPage {
  stripe = Stripe('pk_test_**********');

  setupStripe() {
    let elements = this.stripe.elements();
    var style = {
      base: {
        color: '#32325d',
        lineHeight: '24px',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        fontSize: '16px',
        '::placeholder': {
        color: '#aab7c4'
      }
    },
    invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

this.card = elements.create('card', { style: style });

this.card.mount('#card-element');

this.card.addEventListener('change', event => {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

var form = document.getElementById('payment-form');

form.addEventListener('submit', event => {
  event.preventDefault();

  this.stripe.createToken(this.card).then(token => {

    if (token.error) {

      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = token.error.message;
    } else {

      console.log(token);
      this.stripeTokenHandler(token);
    }
  });
});
}

stripeTokenHandler(token) {
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form

   form.onsubmit();
  }
 }

Using this my aim is to be able to charge the user's card the desired sum of products. Stripe says i should send the token to server but I am unable to figure out how to...pls how can I achieve this?

CE0
  • 191
  • 3
  • 7
  • 18

1 Answers1

0

with stripe api in ionic you can only get card token and verify card. you should send the card token to your backend server to charge the card holder.

take card details -> obtain card token -> send to backend -> charge card

follow this link for backend server charging.

NRaghavendra
  • 126
  • 1
  • 7