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 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 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 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 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)
"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" }) if billing_plan.create(): print("Billing Plan [%s] created successfully" % (billing_plan.id)) else: print(billing_plan.error)
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 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 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 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})