@VoteyDisciple, -, RSpec ( "", ). VCR API ( ), , sleep .
, Stripe API, .
VCR.use_cassette('retry') do |cassette|
Stripe::Customer.all.each &:delete
Stripe::Coupon.all.each &:delete
Stripe::Plan.all.each &:delete
Stripe::Plan.create(
id: 'test',
amount: 100_00,
currency: 'AUD',
interval: 'month',
interval_count: 1,
name: 'RSpec Test'
)
customer = Stripe::Customer.create email: 'test@test.test'
token = card_token cassette, '4000000000000341'
customer.sources.create token: 'TOKEN for 0341'
subscription = customer.subscriptions.create(
plan: 'test',
trial_end: 2.seconds.from_now.to_i
)
sleep 180 if cassette.recording?
invoice = customer.invoices.detect { |invoice| invoice.total > 0 }
begin
invoice.pay
rescue Stripe::CardError
end
invoice.refresh
expect(invoice.paid).to eq(false)
expect(invoice.attempted).to eq(true)
token = card_token cassette, '4242424242424242'
card = customer.sources.create token: token
customer.default_source = card.id
customer.save
invoice.refresh
expect(invoice.paid).to eq(true)
end
- . , , , ruby, ( ):
def card_token(cassette, card = '4242424242424242')
return 'tok_my_default_test_token' unless cassette.recording?
token = `phantomjs --ignore-ssl-errors=true --ssl-protocol=any ./spec/fixtures/stripe_tokens.js #{ENV['STRIPE_PUBLISH_KEY']} #{card}`.strip
raise "Unexpected token: #{token}" unless token[/^tok_/]
token
end
javascript, phantomjs (stripe_tokens.js), :
var page = require('webpage').create(),
system = require('system');
var key = system.args[1],
card = system.args[2];
page.onCallback = function(data) {
console.log(data);
phantom.exit();
};
page.open('spec/fixtures/stripe_tokens.html', function(status) {
if (status == 'success') {
page.evaluate(function(key, card) {
Stripe.setPublishableKey(key);
Stripe.card.createToken(
{number: card, cvc: "123", exp_month: "12", exp_year: "2019"},
function(status, response) { window.callPhantom(response.id) }
);
}, key, card);
}
});
, HTML (stripe_tokens.html) :
<html>
<head>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
</head>
<body></body>
</html>
, , , ! :)