def gift_purchase(request, context): with transaction.commit_on_success(): save = transaction.savepoint() try: data = { "plan": context["plan"], "giftee": context["gift_form"].cleaned_data["giftee"], "gifter": context["gift_form"].cleaned_data["gifter"], } customer = stripe.Customer.create(card=request.POST["stripeToken"], email=data["gifter"]) stripe.Charge.create( amount=int(context["plan"].price * 100), currency="usd", customer=customer["id"], description="{plan} for {giftee} from {gifter}".format(**data), ) gift = GiftSubscription(**data) gift.save() except Exception as e: transaction.savepoint_rollback(save) error(request, e) return context # TODO: dont concatenate strings to build URLs # Gather e-mail info context = Context( { "gift": gift, "static_abs_url": request.build_absolute_uri(settings.STATIC_URL), "redeem_url": request.build_absolute_uri(reverse("customers_signup") + "?code=" + gift.code.code), } ) from_email = SiteSetting.objects.get(key="gifts.email_from").value gifter_subject = SiteSetting.objects.get(key="gifts.gifter_email_subject").value gifter_html = loader.render_to_string("gifts/gifter_email.html", context_instance=context) gifter_text = loader.render_to_string("gifts/gifter_email.txt", context_instance=context) gifter_email = data["gifter"] giftee_subject = SiteSetting.objects.get(key="gifts.giftee_email_subject").value giftee_html = loader.render_to_string("gifts/giftee_email.html", context_instance=context) giftee_text = loader.render_to_string("gifts/giftee_email.txt", context_instance=context) giftee_email = data["giftee"] # Email gifter email = EmailMultiAlternatives(gifter_subject, gifter_text, from_email, [gifter_email]) email.attach_alternative(gifter_html, "text/html") email.send(fail_silently=False) # Email giftee email = EmailMultiAlternatives(giftee_subject, giftee_text, from_email, [giftee_email]) email.attach_alternative(giftee_html, "text/html") email.send(fail_silently=False) # Redirect to page with message request.session["gifts_purchased"] = gift.code return redirect(reverse("gifts_purchased"))
def gift_subscription(request): init = {'country':'US'} if request.POST: gift = GiftForm(request.POST) if gift.is_valid(): randtoken = get_rand_token() message = gift.cleaned_data['message'] givername = gift.cleaned_data['givername'] giveremail = gift.cleaned_data['giveremail'] receivername = gift.cleaned_data['receivername'] receiveremail = gift.cleaned_data['receiveremail'] gs = GiftSubscription(givername=givername,giveremail=giveremail,message=message, receivername=receivername, receiveremail=receiveremail, gifttoken=randtoken) gs.save() #create new user or check if user exists email = receiveremail.lower() #Check if new invite is already a user ck_u = User.objects.filter(email=email) if len(ck_u) > 0: new_u = ck_u[0] up = new_u.get_profile() else: new_u = User(username=email, password=randtoken, email=email) new_u.save() store = Store.objects.filter(city='San Francisco')[0] up = UserProfile(user=new_u, default_store=store) up.save() gs.giftuser = new_u gs.save() #subscribe user with profile address_line1 = '' address_line2 = '' address_city = '' address_state = '' address_zip = '' up_dict = {'name':receivername, 'address_line1':address_line1, 'address_line2':address_line2, 'address_city': address_city, 'address_state': address_state, 'address_zip': address_zip } stripe.api_key = settings.STRIPE_API_KEY try: """Check for saved version""" user_profile = new_u.get_profile() customer_id = user_profile.stripeprofile if customer_id: customer = stripe.Customer.retrieve(customer_id) else: customer = createStripeCustomer(request, new_u, up_dict, up_exists = True) except UserProfile.DoesNotExist: """Create new and save """ customer = createStripeCustomer(request, new_u, up_dict, up_exists = False) #send gift subscription email token = request.POST.get('stripeToken') descrip = "Gift from %s" % givername try: stripe.Charge.create( amount=4999, currency="usd", card=token, # obtained with stripe.js description= descrip ) valid=True """create default subscription""" sub = Subscription(userprofile=up, subscription=False, subscription_type='un-subscribed') sub.save() """email gifter & receiver""" gifter_receiver_email(giveremail, receiveremail, message, givername, receivername, randtoken) """email admin""" gift_admin_email(givername, giveremail, receivername, receiveremail) template = 'gifts/gift_subscription_thanks.html' data = {'receivername':receivername, 'receiveremail':receiveremail} return render_to_response(template, data,context_instance=RequestContext(request)) except stripe.CardError, e: messages.add_message(request, messages.ERROR, e) valid = False except stripe.InvalidRequestError, e: messages.add_message(request, messages.ERROR, e) valid = False