Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the Card Number from Stripe

So I am using Stripe Elements. This is the code:

<script src="https://js.stripe.com/v3/"></script>

<div 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>

  <asp:Button ID="btnPay" OnClick="btnPay_Click" runat="server" Text="Submit Payment" />
</div>
ate a Stripe client
var stripe = Stripe('pk_test_2ddddddddddr');

// 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: '24px',
    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) {
    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
      stripeTokenHandler(result.token);
    }
  });
});

If you look closely, I change the Form into DIV and button into ASP.NET type, What I am trying to do now is when I click the Button btnPay, from the code-behind, I would like to retrieve the card number, how to do that?

Thanks

like image 833
iDeal Avatar asked Dec 08 '25 13:12

iDeal


1 Answers

You shouldn't be retrieving the card. Just let stripe do things for you. However, If you want to retrieve the last 4 digits, it is possible within the result.

result.token.card.last4

This is useful if you want to inform the user the card number this way **** **** **** 1834.

like image 170
kimy82 Avatar answered Dec 11 '25 01:12

kimy82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!