Reputation: 234
I am trying to implement a simple view in Flask to test Stripe payments. But it is not connecting to my subscription plan and there is no error to trouble shoot. Publishable key is set in Ubuntu environment properly as I checked in shell. Following is my View and Form:
Flask:
stripe_keys = {
'secret_key': os.environ['SECRET_KEY'],
'publishable_key': os.environ['PUBLISHABLE_KEY']
}
stripe.api_key = stripe_keys['secret_key']
@app.route('/payments/subscribe', methods=['GET', 'POST'])
def chagrges(self):
stripe.api_key = stripe_keys['secret_key']
amount = 500
customer = stripe.Customer.create(
email='[email protected]',
source=request.form.get['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Standard Student Package $5'
)
return render_template('charge.html', amount=amount)
My form:
<form action="/charge" method="POST">
<article>
<label>
<span>$ 5.00 Standard Package</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key=pk_test_0edgLiaV6OlWvDzipIkAC5G7
data-description="Student Standard Package"
data-amount="500"
data-locale="auto">
</script>
</form>
My subscription plan I've created at stripe account is:
ID:standard
Name: standard
Price: $5.00 USD/year
Trial period:No trial
Please advise.
Upvotes: 0
Views: 869
Reputation: 154
You are creating one time charge to a customer, this charge will not be linked to any subscription or create a subscription.
For enabling subscription, you need create plans through API or from stripe dashboard. then subscribe that customer to a plan using API
stripe.Subscription.create(
customer="<customer_id>",
plan="plan_name"
)
As soon as you create a subscription, customer is automatically charged,i. e. a charge object is created for subscribed amount specified in plan.
Upvotes: 1
Reputation: 2899
Log in to stripe.com and go to dashboard. Select test mode from left bottom. Click on "Subscriptions" and go to plan in opened window. Create a plan with same information that you provided in your view. Subscribe again and go to stripe dashboard. Click "home" and you will see your first purchase there.
Upvotes: 3