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'], "description": request.form['description'], "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://www.cancel.com", "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": request.form['return_url'], "setup_fee": { "currency": request.form['currency'], "value": request.form['setup_fee'] } }, "payment_definitions": [ { "amount": { "currency": request.form['currency'], "value": request.form['amount'] }, "charge_models": [ { "amount": { "currency": request.form['currency'], "value": request.form['shipping'] }, "type": "SHIPPING" }, { "amount": { "currency": request.form['currency'], "value": request.form['tax'] }, "type": "TAX" } ], "cycles": request.form['cycles'], "frequency": request.form['frequency'], "frequency_interval": request.form['frequency_interval'], "name": request.form['payment_name'], "type": request.form['payment_type'] } ], "type": request.form['type'] } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): print( "Billing Plan [%s] created successfully" % (billing_plan.id)) else: print(billing_plan.error) return redirect(url_for('admin')) return render_template('create.html') else: return redirect(url_for('login'))
def admin(): """If merchant is logged in display billing plans in created and active states """ if session.get('logged_in') and session.get('merchant'): plans_created_query_dict = BillingPlan.all({ "status": "CREATED", "sort_order": "DESC" }) plans_created = plans_created_query_dict.to_dict().get('plans') if not plans_created: plans_created = [] plans_active_query_dict = BillingPlan.all({ "status": "ACTIVE", "page": 4, "total_required": "yes" }) plans_active = plans_active_query_dict.to_dict().get('plans') if not plans_active: plans_active = [] return render_template('admin.html', plans_created=plans_created, plans_active=plans_active) else: return redirect(url_for('login'))
def create_plan(): return 'done' billing_plan_attributes = { "name": 'Haliya Publishing - Monthly', "description": 'Monthly Subscription', "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://www.cancel.com", "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": "http://127.0.0.1:5000/billing-plans", "setup_fee": { "currency": "USD", "value": "10" } }, "payment_definitions": [{ "amount": { "currency": "USD", "value": "10" }, "cycles": 0, # Infinite "frequency": "DAY", "frequency_interval": "1", "name": "MONTHLY", "type": "REGULAR" }], "type": "INFINITE" } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): return ("Billing Plan [%s] created successfully" % (billing_plan.id))
def admin(): """If merchant is logged in display billing plans in created and active states """ if session.get('logged_in') and session.get('merchant'): plans_created = BillingPlan.all({"status": "CREATED", "sort_order": "DESC"})['plans'] plans_active = BillingPlan.all({"status": "ACTIVE", "sort_order": "DESC"})['plans'] return render_template('admin.html', plans_created=plans_created, plans_active=plans_active) else: return redirect(url_for('login'))
def post(self, status="1"): data = json.loads(self.request.body.decode('utf-8')) billing_plan = BillingPlan.find(data['id']) if status == "1": if billing_plan.activate(): msg = "Billing Plan activated successfully" print(msg) else: msg = billing_plan.error print(msg) else: billing_plan_update_attributes = [{ "op": "replace", "path": "/", "value": { "state": "INACTIVE" } }] if billing_plan.replace(billing_plan_update_attributes): msg = "Billing Plan inactivated successfully" print(msg) else: msg = billing_plan.error print(msg) self.write({'response': msg})
def subscriptions(): """If customer is logged in display active billing plans in descending order of creation time """ if session.get('logged_in') and session.get('customer'): plans_active = BillingPlan.all({"status": "ACTIVE", "sort_order": "DESC"})['plans'] return render_template('subscriptions.html', plans=plans_active) else: return redirect(url_for('login'))
def create_plan(plan): billing_plan_attributes = { "name": plan['name'], "description": plan.get('description', ''), "merchant_preferences": { "auto_bill_amount": plan.get('auto_bill_amount', True) and 'yes' or 'no', "cancel_url": plan.get('cancel_url', None), "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": plan.get('return_url'), "setup_fee": { "currency": plan.get('setup_fee_currency', 'USD'), "value": plan.get('setup_fee_amount', 0.00) } }, "payment_definitions": [{ "amount": { "currency": plan.get('plan_currency', 'USD'), "value": plan.get('plan_amount') }, "cycles": plan.get('cycles', 0), "frequency": plan.get('frequency_unit', 'MONTH'), "frequency_interval": plan.get('frequency_value', 1), "name": plan.get('payment_name', 'Regular'), "type": plan.get('payment_type', 'REGULAR'), }], "type": plan.get('type', 'INFINITE') } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): return billing_plan else: raise RuntimeError(billing_plan.error)
def admin(): """If merchant is logged in display billing plans in created and active states """ if session.get('logged_in') and session.get('merchant'): plans_created_query_dict = BillingPlan.all({"status": "CREATED", "sort_order": "DESC"}) plans_created = plans_created_query_dict.to_dict().get('plans') if not plans_created: plans_created = [] plans_active_query_dict = BillingPlan.all({"status": "ACTIVE", "page_size": 5, "page": 0, "total_required": "yes"}) plans_active = plans_active_query_dict.to_dict().get('plans') if not plans_active: plans_active = [] return render_template('admin.html', plans_created=plans_created, plans_active=plans_active) else: return redirect(url_for('login'))
def active_plan(self, plan_id): try: billing_plan = BillingPlan.find(plan_id) if billing_plan.activate(): return billing_plan.state else: raise Exception(billing_plan.error['message']) except ResourceNotFound as error: raise Exception("Billing Plan Not Found")
def get_billing_plans(self, status="ACTIVE"): """ status: CREATED, ACTIVE, INACTIVE, ALL :param status: :return: """ plans = BillingPlan.all({"status": status}) if 'plans' in plans: return plans["plans"] return []
def activate(): """Merchant activates plan after creation """ 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', '')) if billing_plan.replace(billing_plan_update_attributes): billing_plan = BillingPlan.find(request.args.get('id', '')) else: print(billing_plan.error) return redirect(url_for('admin')) else: return redirect(url_for('login'))
def get(self): status = self.get_argument('status') # ACTIVE INACTIVE plans = [] plans_query_dict = BillingPlan.all({ "status": status, "sort_order": "DESC" }) if plans_query_dict: plans = plans_query_dict.to_dict().get('plans') self.write({'response': plans})
def activate(): """Merchant activates plan after creation """ if session.get('logged_in') and session.get('merchant'): billing_plan = BillingPlan.find(request.args.get('id', '')) if billing_plan.activate(): print("Billing Plan [%s] activated successfully" % (billing_plan.id)) else: print(billing_plan.error) return redirect(url_for('admin')) else: return redirect(url_for('login'))
def create_billing_plan(self, name, description, cancel_url, return_url, amount, frequency, payment_name): billing_plan = BillingPlan({ "name": name, "description": description, "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": cancel_url, "return_url": return_url, # "notify_url": "http://webhook.moonz.info/5b91f4c1-dd3r-4a8a-acef-e3e3efg32", "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "setup_fee": { "currency": "USD", "value": "0" } }, "payment_definitions": [{ "amount": { "currency": "USD", "value": amount }, "cycles": "0", "frequency": frequency, "frequency_interval": "1", "name": payment_name, "type": "REGULAR" }], "type": "INFINITE" }) if billing_plan.create(): return True, billing_plan print(billing_plan.error) return False, billing_plan.error['message']
def get(self): plans_created = [] plans_active = [] plans_created_query_dict = BillingPlan.all({ "status": "CREATED", "sort_order": "DESC" }) plans_created = plans_created_query_dict.to_dict().get('plans') plans_active_query_dict = BillingPlan.all({ "status": "ACTIVE", "page_size": 5, "page": 0, "total_required": "yes" }) plans_active = plans_active_query_dict.to_dict().get('plans') self.write({ "response": { "plans_created": plans_created, "plans_active": plans_active } })
def create_plan(plan): billing_plan_attributes = { "name": plan['name'], "description": plan.get('description', ''), "merchant_preferences": { "auto_bill_amount": plan.get('auto_bill_amount', True) and 'yes' or 'no', "cancel_url": plan.get('cancel_url', None), "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": plan.get('return_url'), "setup_fee": { "currency": plan.get('setup_fee_currency', 'USD'), "value": plan.get('setup_fee_amount', 0.00) } }, "payment_definitions": [ { "amount": { "currency": plan.get('plan_currency', 'USD'), "value": plan.get('plan_amount') }, "cycles": plan.get('cycles', 0), "frequency": plan.get('frequency_unit', 'MONTH'), "frequency_interval": plan.get('frequency_value', 1), "name": plan.get('payment_name', 'Regular'), "type": plan.get('payment_type', 'REGULAR'), } ], "type": plan.get('type', 'INFINITE') } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): return billing_plan else: raise RuntimeError(billing_plan.error)
def subscriptions(): """If customer is logged in display active billing plans in descending order of creation time """ if session.get('logged_in') and session.get('customer'): plans_active_query_dict = BillingPlan.all({"status": "ACTIVE", "sort_order": "DESC"}) if plans_active_query_dict: plans_active = plans_active_query_dict.to_dict().get('plans') else: plans_active = [] return render_template('subscriptions.html', plans=plans_active) else: return redirect(url_for('login'))
def delete_plan(self, plan_id): try: billing_plan = BillingPlan.find(plan_id) billing_plan_update_attributes = [{ "op": "replace", "path": "/", "value": { "state": "DELETED" } }] if billing_plan.replace(billing_plan_update_attributes): return billing_plan else: raise Exception(billing_plan.error) except ResourceNotFound as error: raise Exception("Billing Plan Not Found")
def activate_plan(): """ Function to activate plan """ try: if current_user.user_type != 'admin': flash('You are not allowed to access this page', 'danger') return redirect(url_for('userbp.dashboard')) configure_paypal() pid = request.args.get('id', '') billing_plan = BillingPlan.find(pid) if billing_plan.activate(): subplan = Subscription.by_planid(pid) subplan.status = True db.session.commit() else: errorlog.error('Plan activate Error', details=str(billing_plan.error)) return redirect(url_for('paymentbp.subscriptions')) except Exception as err: errorlog.error('Plan activate Error', details=str(err)) return render_template('error.html', message="Error!")
def get_plan(self, plan_id): try: billing_plan = BillingPlan.find(plan_id) return billing_plan except ResourceNotFound as error: print("Billing Plan Not Found")
from paypalrestsdk import BillingPlan, ResourceNotFound import logging logging.basicConfig(level=logging.INFO) try: billing_plan = BillingPlan.find("P-0NJ10521L3680291SOAQIVT") print("Got Billing Plan Details for Billing Plan[%s]" % (billing_plan.id)) except ResourceNotFound as error: print("Billing Plan Not Found")
from paypalrestsdk import BillingPlan, ResourceNotFound import logging logging.basicConfig(level=logging.INFO) try: billing_plan = BillingPlan.find("P-0NJ10521L3680291SOAQIVT") print( ("Got Billing Plan Details for Billing Plan[%s]" % (billing_plan.id))) if billing_plan.activate(): billing_plan = BillingPlan.find("P-0NJ10521L3680291SOAQIVT") print(("Billing Plan [%s] state changed to [%s]" % (billing_plan.id, billing_plan.state))) else: print((billing_plan.error)) except ResourceNotFound as error: print("Billing Plan Not Found")
def post(self): ''' curl -v -X POST http://localhost:5000/create \ -H "Content-Type:application/json" \ -H "Authorization: Bearer Access-Token" \ -d '{ "name": "Plan with Regular and Trial Payment Definitions", "description": "Plan with regular and trial payment definitions.", "type": "fixed", "payment_definitions": [ { "name": "Regular payment definition", "type": "REGULAR", "frequency": "MONTH", "frequency_interval": "2", "amount": { "value": "100", "currency": "USD" }, "cycles": "12", "charge_models": [ { "type": "SHIPPING", "amount": { "value": "10", "currency": "USD" } }, { "type": "TAX", "amount": { "value": "12", "currency": "USD" } }] }, { "name": "Trial payment definition", "type": "trial", "frequency": "week", "frequency_interval": "5", "amount": { "value": "9.19", "currency": "USD" }, "cycles": "2", "charge_models": [ { "type": "SHIPPING", "amount": { "value": "1", "currency": "USD" } }, { "type": "TAX", "amount": { "value": "2", "currency": "USD" } }] }], "merchant_preferences": { "setup_fee": { "value": "1", "currency": "USD" }, "return_url": "https://example.com/return", "cancel_url": "https://example.com/cancel", "auto_bill_amount": "YES", "initial_fail_amount_action": "CONTINUE", "max_fail_attempts": "0" } }' ''' data = json.loads(self.request.body.decode('utf-8')) billing_plan_attributes = { "name": data['name'], "description": data['description'], "type": data['type'], "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "https://example.com/cancel", "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": 'https://example.com/success', "setup_fee": { "currency": data['currency'], "value": data['setup_fee'] } }, "payment_definitions": [{ "cycles": data['cycles'], "frequency": data['frequency'], "frequency_interval": data['frequency_interval'], "name": data['payment_name'], "type": data['payment_type'], "amount": { "currency": data['currency'], "value": data['value'] }, "charge_models": [{ "amount": { "currency": data['currency'], "value": data['shipping'] }, "type": "SHIPPING" }, { "amount": { "currency": data['currency'], "value": data['tax'] }, "type": "TAX" }] }] } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): msg = billing_plan.id print("Billing Plan [%s] created successfully" % (msg)) else: msg = billing_plan.error print(msg) self.write({"response": msg})
def find_billing_plan(self, billing_plan_id): billing_plan = BillingPlan.find(billing_plan_id) return billing_plan
def get_all_billing_plans(self): history = BillingPlan.all({"status": "ACTIVE", "sort_order": "DESC"}) #if history: # for plan in history.plans: # print("{}".format(plan.id)) return history
billing_plan = BillingPlan({ "description": "Create Plan for Regular", "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://www.cancel.com", "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": "http://www.success.com", "setup_fee": { "currency": "USD", "value": "25" } }, "name": "Testing1-Regular1", "payment_definitions": [ { "amount": { "currency": "USD", "value": "100" }, "charge_models": [ { "amount": { "currency": "USD", "value": "10.60" }, "type": "SHIPPING" }, { "amount": { "currency": "USD", "value": "20" }, "type": "TAX" } ], "cycles": "0", "frequency": "MONTH", "frequency_interval": "1", "name": "Regular 1", "type": "REGULAR" }, { "amount": { "currency": "USD", "value": "20" }, "charge_models": [ { "amount": { "currency": "USD", "value": "10.60" }, "type": "SHIPPING" }, { "amount": { "currency": "USD", "value": "20" }, "type": "TAX" } ], "cycles": "4", "frequency": "MONTH", "frequency_interval": "1", "name": "Trial 1", "type": "TRIAL" } ], "type": "INFINITE" })
from paypalrestsdk import BillingPlan import logging logging.basicConfig(level=logging.INFO) history = BillingPlan.all({"status": "CREATED"}) print("List BillingPlan:") for plan in history.plans: print(" -> BillingPlan[%s]" % (plan.id))
def activate_plan(plan_id): billing_plan = BillingPlan.find(plan_id) if not billing_plan.activate(): raise RuntimeError(billing_plan.error) return billing_plan
def subscriptions_create(): """ Function to create new subscription plan """ try: if current_user.user_type != 'admin': flash('You are not allowed to access this page', 'danger') return redirect(url_for('userbp.dashboard')) configure_paypal() form = payment_form.create_plan() if form.validate_on_submit(): billing_plan_attributes = { "name": request.form['name'], "description": request.form['description'], "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": url_for('paymentbp.cancel', _external=True), "initial_fail_amount_action": "continue", "max_fail_attempts": "1", "return_url": url_for('paymentbp.execute', _external=True), "setup_fee": { "currency": 'USD', "value": request.form['setup_fee'] } }, "payment_definitions": [{ "amount": { "currency": 'USD', "value": request.form['amount'] }, "cycles": "0", "frequency": request.form['frequency'], "frequency_interval": "1", "name": request.form['payment_definition_name'], "type": 'Regular' }], "type": 'INFINITE' } billing_plan = BillingPlan(billing_plan_attributes) if billing_plan.create(): subs = Subscription( subscription_name=request.form['name'], subscription_price=Decimal(request.form['amount']), subscription_id=billing_plan.id, subscription_details=request.form['description'], created_at=func.now(), updated_at=func.now(), status=False) db.session.add(subs) db.session.commit() flash('Plan is Created', 'success') return redirect(url_for('paymentbp.subscriptions')) else: flash('Plan could not Created', 'danger') errorlog.error('Subscription Create Error', details=str(billing_plan.error)) return render_template('payment/create_plan.html', cform=form) except Exception as err: errorlog.error('Subscription Create Error', details=str(err)) return render_template('error.html', message="Error!")
def delete_plan(plan_id): p = BillingPlan.find(plan_id, api=api) p.replace([{"op": "replace", "path": "/", "value": {"state": "DELETED"}}])
def generate_billing_plan(membershipplan: MembershipPlan, replace=False): base_plan = { "name": membershipplan.name, "description": membershipplan.description, "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://www.paypal.com/cancel", "initial_fail_amount_action": "continue", "max_fail_attempts": "0", "return_url": "http://www.paypal.com/execute", }, "payment_definitions": [{ "amount": { "currency": "GBP", "value": membershipplan.price }, "cycles": "0", "frequency": "MONTH", "frequency_interval": membershipplan.interval_months, "name": "{} Monthly".format(membershipplan.interval_months), "type": "REGULAR" }], "type": "INFINITE" } if membershipplan.payment_key and not replace: raise ValueError( "This membership plan already has a membership key, may need replacing" ) draft_plan = BillingPlan(base_plan, api=api) do_replace = False current_plan = None for current_plan in getplans()['plans']: if current_plan['name'] == draft_plan['name']: if not replace: raise ValueError( "A plan with that name already exists at id {}") else: do_replace = True if do_replace: current_plan.replace([{ "op": "replace", "path": "/", "value": draft_plan.to_dict() }]) plan_id = current_plan['id'] else: draft_plan.create() for current_plan in getplans()['plans']: if current_plan['name'] == draft_plan['name']: plan_id = current_plan['id'] break membershipplan.payment_key = plan_id membershipplan.save()
def getplans(): plans = BillingPlan.all(api=api) if 'plans' in plans: return plans['plans'] else: return None
def init_plans(): from paypalrestsdk import BillingPlan pro_plan = BillingPlan({ "name": "Pro subscription plan", "description": "Gives you private researches, ability to scrape Play store and many other features", "type": "INFINITE", "payment_definitions": [{ "name": "Pro Plan", "type": "REGULAR", "frequency_interval": "1", "frequency": "MONTH", "amount": { "currency": "USD", "value": "1" } }], "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://localhost:5080/payment/pro/cancel", "initial_fail_amount_action": "continue", "max_fail_attempts": "0", "return_url": "http://localhost:5080/payment/pro/execute", "setup_fee": { "currency": "USD", "value": "1" } } }) if pro_plan.create(): print("Billing Plan [%s] created successfully (pro)" % pro_plan.id) if pro_plan.activate(): pro_plan = BillingPlan.find(pro_plan.id) print("Billing Plan [%s] state changed to %s (pro)" % (pro_plan.id, pro_plan.state)) app.config['PRO'] = pro_plan.id else: print(pro_plan.error) else: print(pro_plan.error) premium_plan = BillingPlan({ "name": "Premium subscription plan", "description": "Subscription which lets you work without any limitations", "type": "INFINITE", "payment_definitions": [{ "name": "Pro Plan", "type": "REGULAR", "frequency_interval": "1", "frequency": "MONTH", "amount": { "currency": "USD", "value": "2" } }], "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": "http://localhost:5080/payment/premium/cancel", "initial_fail_amount_action": "continue", "max_fail_attempts": "0", "return_url": "http://localhost:5080/payment/premium/execute", "setup_fee": { "currency": "USD", "value": "1" } } }) if premium_plan.create(): print("Billing Plan [%s] created successfully (premium)" % premium_plan.id) if premium_plan.activate(): premium_plan = BillingPlan.find(premium_plan.id) print("Billing Plan [%s] state changed to %s (premium)" % (premium_plan.id, premium_plan.state)) app.config['PREMIUM'] = premium_plan.id else: print(premium_plan.error) else: print(premium_plan.error)
def create_billing_plan(self, return_url, cancel_url, item_price, num_cycles, frequency): plan = { "description": "Basic Plan subscription", "merchant_preferences": { "auto_bill_amount": "yes", "cancel_url": cancel_url, "initial_fail_amount_action": "continue", "max_fail_attempts": "2", "return_url": return_url, # to be charged immediately # replacement for TRIAL PERIOD "setup_fee": { "currency": "USD", "value": "1.00" } }, "name": "Basic Plan subscription", "payment_definitions": [ { "type": "REGULAR", "frequency": frequency, "frequency_interval": "1", "cycles": str(num_cycles), "amount": { "currency": "USD", "value": str(item_price) }, "name": "Succeeding Month Recurring Payments", "charge_models": [ { "type": "TAX", "amount": { "currency": "USD", "value": "0.00" } }, { "type": "SHIPPING", "amount": { "currency": "USD", "value": "0.00" } } ], }, #{ # "type": "TRIAL", # "frequency": "Day", # "frequency_interval": "2", # "cycles": str(1), # "amount": { # "currency": "USD", # "value": str(1) # }, # "name": "First Month Prorated Payment", # "charge_models": [ # { # "type": "TAX", # "amount": { # "currency": "USD", # "value": "0.00" # } # }, # { # "type": "SHIPPING", # "amount": { # "currency": "USD", # "value": "0.00" # } # } # ], #}, ], "type": "FIXED" } billing_plan = BillingPlan(plan) if billing_plan.create(): #print("Billing Plan {} creation successful!".format(billing_plan.id)) if billing_plan.activate(): #print("Billing Plan {} activation successful {}".format(billing_plan.id, billing_plan.state)) pass else: print("Billing Plan activation failed! {}".format(billing_plan.error)) else: print("Billing Plan creation failed!".format(billing_plan.error)) return billing_plan.id
def activate_plan(): billing_plan = BillingPlan.find('P-3NF352338H658800LZS4KMVY') if billing_plan.activate(): return "Billing Plan [%s] activated successfully" % billing_plan.id else: return billing_plan.error
from paypalrestsdk import BillingPlan import logging logging.basicConfig(level=logging.INFO) history = BillingPlan.all({"status": "CREATED", "page_size": 5, "page": 1, "total_required": "yes"}) print(history) print("List BillingPlan:") for plan in history.plans: print(" -> BillingPlan[%s]" % (plan.id))
from paypalrestsdk import BillingPlan import logging logging.basicConfig(level=logging.INFO) history = BillingPlan.all( {"status": "CREATED", "page_size": 5, "page": 1, "total_required": "yes"}) print(history) print("List BillingPlan:") for plan in history.plans: print(" -> BillingPlan[%s]" % (plan.id))