I am new to angularjs and just trying to integrate stripe api of version 3 in my angular app. I tried to follow the steps given in the stripe site. Now the problem is that in stripe site whole code is based on plain js or jquery. I tried to put the whole js code from stripe into my angular controller and it loads fine, but the problem is that card element remains blank in ui form, plus i am not getting any kind of error on console.
Here is my controller code and html code:
myapp.controller('paymentCtrl', ['$scope', function($scope)
{
var stripe = Stripe('abcdedfght');
var elements = stripe.elements();
var card = elements.create('card',
{
hidePostalCode: true,
style: {
base: {
iconColor: '#F99A52',
color: '#32315E',
lineHeight: '48px',
fontWeight: 400,
fontFamily: '"Helvetica Neue", "Helvetica", sans-serif',
fontSize: '15px',
'::placeholder': {
color: '#CFD7DF',
}
},
}
});
card.mount('#card-element');
function setOutcome(result)
{
var successElement = document.querySelector('.success');
var errorElement = document.querySelector('.error');
successElement.classList.remove('visible');
errorElement.classList.remove('visible');
if (result.token)
{
// Use the token to create a charge or a customer
// https://stripe.com/docs/charges
successElement.querySelector('.token').textContent = result.token.id;
successElement.classList.add('visible');
}
else if (result.error)
{
errorElement.textContent = result.error.message;
errorElement.classList.add('visible');
}
}
card.on('change', function(event)
{
setOutcome(event);
});
document.querySelector('form').addEventListener('submit', function(e)
{
e.preventDefault();
var form = document.querySelector('form');
var extraDetails = {
name: form.querySelector('input[name=cardholder-name]').value,
address_zip: form.querySelector('input[name=address-zip]').value
};
stripe.createToken(card, extraDetails).then(setOutcome);
});
}]);
HTML file for the angularjs state:
<div ng-controller='paymentCtrl'>
<form>
<div class="group">
<label>
<span>Name</span>
<input name="cardholder-name" class="field" placeholder="Jane Doe" />
</label>
<label>
<span>Phone</span>
<input class="field" placeholder="(123) 456-7890" type="tel" />
</label>
</div>
<div class="group">
<label>
<span>Card</span>
<div id="card-element" class="field"></div>
</label>
</div>
<button type="submit">Pay $25</button>
<div class="outcome">
<div class="error" role="alert"></div>
</div>
</form>
</div>
I tried to change this form to directive also, but it still doesn't work.
Note: is loaded in a file called main.html which is basically the entry point of my angularjs app.
Edit: After trying various times i found that card is mounting properly and card input is sent when i post, so problem is whatever i am entering on card input is not getting displayed.
Is it because I am on localhost and https is not there?
It should work fine under http with your test key, only on your production system https is mandatory.
I also use angular (4) and I had similar problems with the card input not showing. It turned out that in my case it was just a styling issue. By playing around with the styles, I got the card input field showing up. In the end I added these style elements (shown inline for clarity):
<div>
<div style="width: 30em" #stripecardelement id="card-element"></div>
</div>
<div>
<!-- Used to display Element errors -->
<span style="width: 30em; height: 2em; letter-spacing: 0em" id="card-errors" role="alert"></span>
</div>