2017-02-19 7 views
2

私はFlaskのシンプルなフォームを作成し、Stripe.comでプランを作成しました。私は開発モードでプランニングに加入することができ、ストライプの支払いを反映します。ストリープサイトとリアルタイムで同期して、プランの有効期限やその他のイベントを取得する必要があります。この目的のために、ストライプIDを取得してデータベースに保存するコールバック関数を作成する必要があることを理解しています。セッションよりもデータベースに保存したいと思っています。コールバックルートを作成し、JSON APIからデータベースに値を保存する方法をアドバイスしてください。以下は私の購読するコードです。私は期限切れや他のイベントを表示する必要があります。ストライプフラスコのリクエスト/レスポンス方法

def yearly_charged(): 
    #Need to save customer stripe ID to DB model 
    amount = 1450 

    customer = stripe.Customer.create(
     email='[email protected]', 
     source=request.form['stripeToken'] 
    ) 
    try: 
     charge = stripe.Charge.create(
      customer=customer.id, 
      capture='true', 
      amount=amount, 
      currency='usd', 
      description='standard', 
     ) 
     data="$" + str(float(amount)/100) + " " + charge.currency.upper() 
    except stripe.error.CardError as e: 
     # The card has been declined 
     body = e.json_body 
     err = body['error'] 
     print 
     "Status is: %s" % e.http_status 
     print 
     "Type is: %s" % err['type'] 
     print 
     "Code is: %s" % err['code'] 
     print 
     "Message is: %s" % err['message'] 


    return render_template('/profile/charge.html', data=data, charge=charge) 

テンプレート:

<form action="/charged" method="post"> 
      <div class="form-group"> 
       <label for="email">Amount is 14.95 USD </label> 
       <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
        data-key="{{ key }}" 
        data-description="Yearly recurring billing" 
        data-name="yearly" 
        data-amount="1495" 
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png" 
        data-locale="auto"> 
       </script> 
      </div> 
     </form> 

モデル:

class Students(db.Model): 
    __tablename__='students' 
    ....... 
    student_strip_id = db.Column(db.String(45)) 
    ....... 

は、私は、DBに保存する適切な方法で応答を取得する方法を設定することができますので、以下の機能を考案する手助けが必要です。

@app.route('/oauth/callback/, methods=['POST']) 
    # This is where I guess I have to define callback function to get API data 
    return redirect('/') 

ここでの目的は、フラスコモデルに保存するようにストライプID、有効期限イベントやストライプのAPIオブジェクトから他のサブスクリプションの通知を抽出することです。

答えて

1

最初に、あなたが共有したコードは、サブスクリプションではなく、creates a one-off chargeであることに注意してください(つまり、定期的な料金)。自動的に定期請求を作成する場合は、documentation for subscriptionsを調べる必要があります。

私はあなたの質問を正しく理解していれば、webhooksを使用してサブスクリプションの支払いが成功したことを通知したいと考えています。成功した支払いごとにinvoice.payment_succeededイベントが作成されます。 (サブスクリプションイベントの詳細については、Cfのhere。)

フラスコでは、ウェブフックハンドラは、次のようになります。

import json 
import stripe 
from flask import Flask, request 

app = Flask(__name__) 

@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 
     # Do something with invoice 
+0

は、あなたが大きな助けてきた、ありがとうございました。 –

関連する問題