Skip to content

Instantly share code, notes, and snippets.

@SeraphimSerapis
Created July 16, 2014 15:56
Show Gist options
  • Select an option

  • Save SeraphimSerapis/3e924047219ce2a9f4bf to your computer and use it in GitHub Desktop.

Select an option

Save SeraphimSerapis/3e924047219ce2a9f4bf to your computer and use it in GitHub Desktop.
{% for plan in plans_created %}
<li class="list-group-item">
<div class="row">
<div class="col-lg-3 col-md-3 text-center">
</div>
<div class="col-lg-7 col-md-7 section-box">
<h2>{{ plan.name }}</h2>
<p>{{ plan.description }}</p>
</div>
</div>
</li>
{% endfor %}
<form action="{{ url_for('activate', id=plan.id) }}" method="post">
<input type="hidden" id="activate" name="activate" value="activate" />
<button class="btn btn-success btn-md" type="submit"><span class="glyphicon glyphicon-cloud-upload"></span> Activate</button>
</form>
from paypalrestsdk import BillingPlan, BillingAgreement
# billing_plan_attributes is a Python dictionary with plan attributes
billing_plan = BillingPlan(billing_plan_attributes)
billing_plan.create()
@app.route("/create", methods=['GET', 'POST'])
def create():
"""Merchant creates new billing plan from input values in the form
"""
if session.get('logged_in') and session.get('merchant'):
if request.method == 'POST':
billing_plan_attributes = {
"name": request.form['name']
}
# Result is a Json object with an array of plan objects
plans_created = BillingPlan.all({"status": "CREATED", "sort_order": "DESC"})
@app.route("/activate", methods=['POST'])
def activate():
if session.get('logged_in') and session.get('merchant'):
billing_plan_update_attributes = [
{
"op": "replace",
"path": "/",
"value": {
"state": "ACTIVE"
}
}
]
billing_plan = BillingPlan.find(request.args.get('id', ''))
billing_plan.replace(billing_plan_update_attributes)
@app.route("/subscribe", methods=['POST'])
def subscribe():
if session.get('logged_in') and session.get('customer'):
billing_agreement = BillingAgreement({
"description": "Agreement for organization plan",
"start_date": "2015-02-19T00:37:04Z",
"plan": {
"id": request.args.get('id', '')
},
"payer": {
"payment_method": "paypal"
},
"shipping_address": {
"line1": "StayBr111idge Suites",
"line2": "Cro12ok Street",
"city": "San Jose",
"state": "CA",
"postal_code": "95112",
"country_code": "US"
}
})
if billing_agreement.create():
print("Billing Agreement created successfully")
for link in billing_agreement.links:
if link.rel == "approval_url":
approval_url = link.href
return redirect(approval_url)
else:
print(billing_agreement.error)
return redirect(url_for('subscriptions'))
else:
return redirect(url_for('login'))
@app.route("/execute")
def execute():
payment_token = request.args.get('token', '')
billing_agreement_response = BillingAgreement.execute(payment_token)
return redirect(url_for('agreement_details', id=billing_agreement_response.id))
billing_agreement = BillingAgreement.find(request.args.get('id', ''))
transactions = billing_agreement.search_transactions(start_date, end_date)
<form action="{{ url_for('subscribe', id=plan.id) }}" method="post">
<input type="hidden" id="subscribe" name="subscribe" value="subscribe" />
<button class="btn btn-success btn-md" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span> Subscribe</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment