buy now flow complete except email confirmation
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import braintree
|
||||
from django.utils import timezone
|
||||
import datetime
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
BRAINTREE_MERCHANT_ID,BRAINTREE_PUBLIC_KEY,BRAINTREE_PRIVATE_KEY
|
||||
@@ -16,8 +18,8 @@ gateway = braintree.BraintreeGateway(
|
||||
from rowers.models import Rower,PaidPlan
|
||||
from rowers.utils import ProcessorCustomerError
|
||||
|
||||
def create_customer(rower):
|
||||
if not rower.customer_id:
|
||||
def create_customer(rower,force=False):
|
||||
if not rower.customer_id or force:
|
||||
result = gateway.customer.create(
|
||||
{
|
||||
'first_name':rower.user.first_name,
|
||||
@@ -28,14 +30,25 @@ def create_customer(rower):
|
||||
raise ProcessorCustomerError
|
||||
else:
|
||||
rower.customer_id = result.customer.id
|
||||
rower.paymentprocessor = 'braintree'
|
||||
rower.save()
|
||||
return rower.customer_id
|
||||
else:
|
||||
return rower.customer_id
|
||||
|
||||
|
||||
|
||||
def get_client_token(rower):
|
||||
client_token = gateway.client_token.generate({
|
||||
"customer_id":rower.customer_id,
|
||||
try:
|
||||
client_token = gateway.client_token.generate({
|
||||
"customer_id":rower.customer_id,
|
||||
})
|
||||
except ValueError:
|
||||
customer_id = create_customer(rower,force=True)
|
||||
|
||||
client_token = gateway.client_token.generate({
|
||||
"customer_id": customer_id,
|
||||
})
|
||||
|
||||
return client_token
|
||||
|
||||
@@ -51,3 +64,125 @@ def get_plans_costs():
|
||||
plan.save()
|
||||
|
||||
return plans
|
||||
|
||||
def make_payment(rower,data):
|
||||
nonce_from_the_client = data['payment_method_nonce']
|
||||
amount = data['amount']
|
||||
amount = str(amount)
|
||||
|
||||
result = gateway.transaction.sale({
|
||||
"amount": amount,
|
||||
"payment_method_nonce": nonce_from_the_client,
|
||||
"options": {
|
||||
"submit_for_settlement": True
|
||||
}
|
||||
})
|
||||
if result.is_success:
|
||||
transaction = result.transaction
|
||||
amount = transaction.amount
|
||||
|
||||
return amount
|
||||
else:
|
||||
return 0,''
|
||||
|
||||
def create_subscription(rower,data):
|
||||
planid = data['plan']
|
||||
plan = PaidPlan.objects.get(id=planid)
|
||||
nonce_from_the_client = data['payment_method_nonce']
|
||||
amount = data['amount']
|
||||
|
||||
# create or find payment method
|
||||
result = gateway.payment_method.create({
|
||||
"customer_id": rower.customer_id,
|
||||
"payment_method_nonce": nonce_from_the_client
|
||||
})
|
||||
|
||||
if result.is_success:
|
||||
payment_method_token = result.payment_method.token
|
||||
else:
|
||||
return False
|
||||
|
||||
result = gateway.subscription.create({
|
||||
"payment_method_token": payment_method_token,
|
||||
"plan_id": plan.external_id
|
||||
})
|
||||
|
||||
if result.is_success:
|
||||
rower.paidplan = plan
|
||||
rower.planexpires = timezone.now()+datetime.timedelta(days=365)
|
||||
rower.teamplanexpires = timezone.now()+datetime.timedelta(days=365)
|
||||
rower.clubsize = plan.clubsize
|
||||
rower.paymenttype = plan.paymenttype
|
||||
rower.rowerplan = plan.shortname
|
||||
rower.save()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
return False
|
||||
|
||||
def cancel_subscription(rower,id):
|
||||
themessages = []
|
||||
errormessages = []
|
||||
try:
|
||||
result = gateway.subscription.cancel(id)
|
||||
messages.append("Subscription canceled")
|
||||
except:
|
||||
errormessages.append("We could not find the subscription record in our customer database")
|
||||
return False, themessages, errormessages
|
||||
|
||||
rower.paidplan = None
|
||||
rower.teamplanexpires = timezone.now()
|
||||
rower.planexpires = timezone.now()
|
||||
rower.clubsize = 0
|
||||
rower.rowerplan = 'basic'
|
||||
rower.save()
|
||||
themessages.append("Your plan was reset to basic")
|
||||
|
||||
return True, themessages,errormessages
|
||||
|
||||
|
||||
def find_subscriptions(rower):
|
||||
try:
|
||||
result = gateway.customer.find(rower.customer_id)
|
||||
except:
|
||||
raise ProcessorCustomerError("We could not find the customer in the database")
|
||||
|
||||
active_subscriptions = []
|
||||
|
||||
cards = result.credit_cards
|
||||
for card in cards:
|
||||
for subscription in card.subscriptions:
|
||||
if subscription.status == 'Active':
|
||||
active_subscriptions.append(subscription)
|
||||
|
||||
try:
|
||||
paypal_accounts = result.paypal_accounts
|
||||
for account in accuonts:
|
||||
for subscription in account.subscriptions:
|
||||
if subscription.status == 'Active':
|
||||
active_subscriptions.append(subscription)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
result = []
|
||||
|
||||
for subscription in active_subscriptions:
|
||||
|
||||
plan = PaidPlan.objects.filter(paymentprocessor="braintree",
|
||||
external_id=subscription.plan_id)[0]
|
||||
|
||||
thedict = {
|
||||
'end_date': subscription.billing_period_end_date,
|
||||
'plan_id': subscription.plan_id,
|
||||
'price': subscription.price,
|
||||
'id': subscription.id,
|
||||
'plan': plan.name
|
||||
}
|
||||
|
||||
result.append(thedict)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user