Saving JSON data to Python/Flask Field

481 views
Skip to first unread message

Anonymous

unread,
Feb 27, 2017, 7:42:56 AM2/27/17
to Stripe API Discussion
Hi,
Is there a way to save stripe json response in Flask Model tables?
For instance I need to save customer_id from 

            stripe.Customer.create(
                        email=request.form['email'],
                        plan='someplan',
                        ).
How to grab customer ID because I tried to to it like this:
            event_json = json.loads(request.data)
            event = stripe.Customer.retrieve(event_json)['id']
            customer = event.data.object

But it says no json received against it. I need to receive bunch of events in many veiws and I will be really thankful if someone
can give me quick direction.

Thanks

Andrew Nelder

unread,
Feb 28, 2017, 1:37:45 PM2/28/17
to Stripe API Discussion
Hey,

When you say "Flask Model Tables" are you talking about tables in your HTML Code or are you referring to some kind of ORM that you've included in Flask (like SQLAlchemy, Pewee, etc.)?  I'm *thinking* maybe you're asking how to cast a JSON request from Stripe's webhooks to Stripe objects from the Stripe Python bindings?

To turn a Stripe-Python object into a JSON blob, you can do something like this:

```
import json
cus = stripe.Customer.create(...)
json_cus = json.dumps(cus)
```

Then to cast to a Stripe object from a webhook (which only issues Event objects), you can do something like this:

```
import stripe
ev = stripe.Event.construct_from(json.loads(request.data), None)
event_id = ev.id  # event id
event_type = ev.type  # the event type
customer_id = ev.data.object.id  # this is only the customer id for `customer.*` events
```

Does that make sense?

Also, I could be wrong, but I think you can also use `get_json()`-method [1] to actually parse the json from the request rather than importing the default `json`-library too.

Hope that helps!  Let me know if I misunderstood your question.

Cheers,
Andrew


[1] http://flask.pocoo.org/docs/0.12/api/#flask.Request.get_json


--
You received this message because you are subscribed to the Google Groups "Stripe API Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss+unsubscribe@lists.stripe.com.
To post to this group, send email to api-d...@lists.stripe.com.
Visit this group at https://groups.google.com/a/lists.stripe.com/group/api-discuss/.

Anonymous

unread,
Mar 2, 2017, 7:38:54 AM3/2/17
to Stripe API Discussion
I am referring to SQLAlchemy fields directly. I am able to save customer.id, subscription.id but not the date for some reason. I need to display complete information about the subscription to end user.

Also the confusion is that if I create a response view like below:

@app.route('/webhook' methods=['POST']
def webhook():
    event_json = json.loads(request.data)
    event = stripe.Event.retrieve(event_json['id'])

    if event.type == 'invoice.payment_succeeded':
        invoice = event.data.object
        ...........
How to integrate the response with other views like cancel/subscription views?
Do I need to include all the other views with webhooks on stripe side?
Because I need all my views to show expiry, due date, invoice number etc.

So lets say I have a subscription views for yearly package like below then how to connect json synchronised element for next due date.

@app.route('/yearly', methods=['GET', 'POST'])
def yearly_charged_rec(user_id=None):
    if not user_authorized():
        return redirect('/')
    
    if request.method == 'POST':
        data = get_profile_data(session['auth_token'])       
        profile_data = data['StudentProfile']
        student = get_profile_data(session['auth_token'])['StudentProfile']    
        stripe_token = request.form['stripeToken']
        email = request.form['stripeEmail']
        customer = stripe.Customer.create(
            email=email,
            source=request.form['stripeToken']
        )
        try:
            subscription = stripe.Subscription.create(
                customer=customer.id,
                plan="yearlyrec",
            )
            student_ids


            student.stripe_customer_id = customer.id
            student.stripe_subscription_id = subscription.id
            package = Package(

                stripe_id = customer.id,
                student_email=request.form['stripeEmail'],
                is_active=True,
                package_type='yearlyrec',
                subscription_id=subscription.id
            )
            dbase.session.add(package)
            dbase.session.commit()

        except stripe.error.CardError as e:
            body = e.json_body
            err = body['error']
            expiry=??
            due_date = ??

    return render_template('/profile/charge/monthlycharge.html', expiry=expiry, due_date=due_date)

This way I can upload my models with the required information and can upload user related fields in my templates easily.
So assumption is to get data from webhook and post it to each view...or do I need to include all the views links in Stripe webhooks?
Hope information was presented in meaningful way.
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.
Reply all
Reply to author
Forward
0 new messages