def _send_one_time_tip(req, podcast, owner_us, amount): email = req.POST.get('email') token = req.POST.get('token') tip_user = TipUser.tip_user_from(email_address=email) application_fee = int(amount * 0.3) if owner_us.plan == PLAN_DEMO else 0 try: stripe_charge = stripe.Charge.create( amount=amount, application_fee=application_fee, currency='usd', description='Tip for %s' % podcast.name, destination=owner_us.stripe_payout_managed_account, source=token, ) except Exception as e: rollbar.report_message('Error when sending tip: %s' % str(e), 'error') return {'error': str(e)} podcast.total_tips += amount podcast.save() tip_event = TipEvent( tipper=tip_user, podcast=podcast, amount=amount, fee_amount=application_fee, stripe_charge=stripe_charge.id) tip_event.save() send_notification_email( None, ugettext('Thanks for leaving a tip!'), ugettext('Your tip was sent: %s received $%0.2f. Thanks for supporting your ' 'favorite content creators!') % (podcast.name, float(amount) / 100), email=email) send_notification_email( podcast.owner, ugettext('Your podcast was tipped!'), ugettext('%s received a tip of $%0.2f from %s. You should send them an email ' 'thanking them for their generosity.') % ( podcast.name, float(amount) / 100, tip_user.email_address)) NotificationHook.trigger_notification( podcast=podcast, trigger_type='tip', data={'tipper': tip_user.email_address, 'amount': amount}) return {'success': True}
def _send_one_time_tip(req, podcast, owner_us, amount): email = req.POST.get('email') token = req.POST.get('token') tip_user = TipUser.tip_user_from(email_address=email) application_fee = int(amount * 0.3) if owner_us.plan == PLAN_DEMO else 0 try: stripe_charge = stripe.Charge.create( amount=amount, application_fee=application_fee, currency='usd', description='Tip for %s' % podcast.name, destination=owner_us.stripe_payout_managed_account, source=token, ) except Exception as e: rollbar.report_message('Error when sending tip: %s' % str(e), 'error') return {'error': str(e)} Podcast.objects.filter(id=podcast.id).update(total_tips=F('total_tips') + amount) tip_event = TipEvent( tipper=tip_user, podcast=podcast, amount=amount, fee_amount=application_fee, stripe_charge=stripe_charge.id) tip_event.save() send_notification_email( None, ugettext('Thanks for leaving a tip!'), ugettext('Your tip was sent: %s received $%0.2f. Thanks for supporting your ' 'favorite content creators!') % (podcast.name, float(amount) / 100), email=email) send_notification_email( podcast.owner, ugettext('Your podcast was tipped!'), ugettext('%s received a tip of $%0.2f from %s. You should send them an email ' 'thanking them for their generosity.') % ( podcast.name, float(amount) / 100, tip_user.email_address)) NotificationHook.trigger_notification( podcast=podcast, trigger_type='tip', data={'tipper': tip_user.email_address, 'amount': amount}) return {'success': True}
def ep_comment_box(req, podcast_slug, episode_id): pod = get_object_or_404(Podcast, slug=podcast_slug) if not UserSettings.user_meets_plan(pod.owner, plans.FEATURE_MIN_COMMENT_BOX): raise Http404() ep = get_object_or_404(PodcastEpisode, podcast=pod, id=episode_id) if not req.POST: return _pmrender(req, 'feedback/comment_episode.html', { 'podcast': pod, 'episode': ep }) try: if not _validate_recaptcha(req): raise Exception('Invalid ReCAPTCHA') ip = analyze.get_request_ip(req) f = Feedback(podcast=pod, episode=ep, sender=req.POST.get('email'), message=req.POST.get('message'), sender_ip=ip) f.save() send_notification_email( pod.owner, ugettext('[Pinecast] You got some feedback!'), 'Go check the Feedback page of %s--an episode on %s--to see what was written.\n\n' 'https://pinecast.com%s' % (ep.title, pod.name, reverse('podcast_episode', podcast_slug=podcast_slug, episode_id=str(ep.id)) + '#tab-feedback')) NotificationHook.trigger_notification(podcast=pod, trigger_type='feedback', data={ 'episode': ep, 'content': req.POST.get('message'), 'sender': req.POST.get('email') }) except Exception: return _pmrender(req, 'feedback/comment_episode.html', { 'podcast': pod, 'episode': ep, 'error': True, 'default': req.POST }) return _pmrender(req, 'feedback/thanks.html', {'podcast': pod})
def ep_comment_box(req, podcast_slug, episode_id): pod = get_object_or_404(Podcast, slug=podcast_slug) if not UserSettings.user_meets_plan(pod.owner, plans.FEATURE_MIN_COMMENT_BOX): raise Http404() ep = get_object_or_404(PodcastEpisode, podcast=pod, id=episode_id) if not req.POST: return _pmrender(req, 'feedback/comment_episode.html', {'podcast': pod, 'episode': ep}) try: if not _validate_recaptcha(req): raise Exception('Invalid ReCAPTCHA') ip = analyze.get_request_ip(req) f = Feedback( podcast=pod, episode=ep, sender=req.POST.get('email'), message=req.POST.get('message'), sender_ip=ip ) f.save() send_notification_email( pod.owner, ugettext('[Pinecast] You got some feedback!'), 'Go check the Feedback page of %s--an episode on %s--to see what was written.\n\n' 'https://pinecast.com%s' % (ep.title, pod.name, reverse('podcast_episode', podcast_slug=podcast_slug, episode_id=str(ep.id)) + '#tab-feedback') ) NotificationHook.trigger_notification( podcast=pod, trigger_type='feedback', data={'episode': ep, 'content': req.POST.get('message'), 'sender': req.POST.get('email')}) except Exception: return _pmrender(req, 'feedback/comment_episode.html', {'podcast': pod, 'episode': ep, 'error': True, 'default': req.POST}) return _pmrender(req, 'feedback/thanks.html', {'podcast': pod})
def hook(req): try: body = json.loads(req.body.decode('utf-8')) except Exception as e: rollbar.report_message('Error parsing Stripe hook JSON: %s' % str(e), 'warn') return HttpResponse(status=400) if not settings.DEBUG: try: # Validate the event stripe.Event.retrieve(body['id'], stripe_account=body.get('user_id')) except Exception as e: rollbar.report_message('Error fetching Stripe event: %s' % str(e), 'warn') return HttpResponse(status=400) if body['type'] == 'invoice.payment_succeeded' and body.get('user_id'): sub = _get_tip_subscription(body) if not sub: return {'warning': 'subscription unrecognized'} amount = int(body['data']['object']['total']) pod = sub.podcast tip_event = TipEvent(tipper=sub.tipper, podcast=pod, amount=amount, recurring_tip=sub) tip_event.save() Podcast.objects.filter(id=pod.id).update(total_tips=F('total_tips') + amount) email = sub.tipper.email_address send_notification_email( None, ugettext('Thanks for leaving a tip!'), ugettext( 'Your tip was sent: %s received $%0.2f. Thanks for supporting your ' 'favorite content creators!') % (pod.name, float(amount) / 100), email=email) send_notification_email( pod.owner, ugettext('%s received a tip of $%0.2f') % (pod.name, float(amount) / 100), ugettext( '%s received a tip of $%0.2f from %s as part of a monthly ' 'subscription to the show. You should send them an email ' 'thanking them for their generosity.') % (pod.name, float(amount) / 100, email)) NotificationHook.trigger_notification(podcast=pod, trigger_type='tip', data={ 'tipper': sub.tipper.email_address, 'amount': amount }) return {'success': 'emails sent, tip event processed'} elif body['type'] == 'invoice.payment_succeeded': sub = stripe.Subscription.retrieve( body['data']['object']['subscription']) if not sub.discount: return {'success': 'ignoring undiscounted subscription'} coupon_code = sub.discount.coupon.id try: coupon_owner = UserSettings.objects.get(coupon_code=coupon_code) except UserSettings.DoesNotExist: return {'success': 'coupon not owned by referrer'} if coupon_owner.plan == payment_plans.PLAN_DEMO or coupon_owner.plan == payment_plans.PLAN_COMMUNITY: return {'success': 'coupon owned by free user'} min_charge = float('inf') valid_charges = 0 # We limit this to three. If it goes over or under, we can ignore the charge. invoices = stripe.Invoice.list(customer=sub.customer, limit=3) if len(invoices.data) < 2: return {'success': 'did not reach two invoices yet'} for invoice in invoices.data: if not invoice.paid: continue valid_charges += 1 invoice_amount = 0 for line in invoice.lines.data: invoice_amount += line.amount if invoice_amount < min_charge: min_charge = invoice_amount if valid_charges != 2: return {'success': 'did not have two successful invoices'} try: owner_cust = coupon_owner.get_stripe_customer() except Exception as e: rollbar.report_message( 'Error fetching coupon owner stripe customer: %s' % str(e), 'error') return {'success': 'coupon owner does not exist'} else: if not owner_cust: rollbar.report_message( 'Coupon owner did not have valid stripe customer', 'error') return {'success': 'coupon owner does not exist'} amount = min_charge * 2 # Negative amounts are credits owner_cust.account_balance -= amount owner_cust.save() send_notification_email( coupon_owner.user, ugettext('You have referral credit!'), ugettext('One of your referrals has crossed their two-month mark ' 'as a paying customer. Your account has been credited ' '$%.2f.') % (float(amount) / 100)) return {'success': 'user credited'} elif body['type'] == 'invoice.payment_failed': if body.get('user_id'): return _handle_failed_tip_sub(body) else: return _handle_failed_subscription(body) return {'success': 'ignored'}
def hook(req): try: body = json.loads(req.body.decode('utf-8')) except Exception as e: rollbar.report_message( 'Error parsing Stripe hook JSON: %s' % str(e), 'warn') return HttpResponse(status=400) if not settings.DEBUG: try: # Validate the event stripe.Event.retrieve( body['id'], stripe_account=body.get('user_id')) except Exception as e: rollbar.report_message( 'Error fetching Stripe event: %s' % str(e), 'warn') return HttpResponse(status=400) if body['type'] == 'invoice.payment_succeeded' and body.get('user_id'): sub = _get_tip_subscription(body) if not sub: return {'warning': 'subscription unrecognized'} amount = int(body['data']['object']['total']) pod = sub.podcast tip_event = TipEvent( tipper=sub.tipper, podcast=pod, amount=amount, recurring_tip=sub) tip_event.save() pod.total_tips += amount pod.save() email = sub.tipper.email_address send_notification_email( None, ugettext('Thanks for leaving a tip!'), ugettext('Your tip was sent: %s received $%0.2f. Thanks for supporting your ' 'favorite content creators!') % (pod.name, float(amount) / 100), email=email) send_notification_email( pod.owner, ugettext('%s received a tip of $%0.2f') % (pod.name, float(amount) / 100), ugettext('%s received a tip of $%0.2f from %s as part of a monthly ' 'subscription to the show. You should send them an email ' 'thanking them for their generosity.') % (pod.name, float(amount) / 100, email)) NotificationHook.trigger_notification( podcast=pod, trigger_type='tip', data={'tipper': sub.tipper.email_address, 'amount': amount}) return {'success': 'emails sent, tip event processed'} elif body['type'] == 'invoice.payment_succeeded': sub = stripe.Subscription.retrieve(body['data']['object']['subscription']) if not sub.discount: return {'success': 'ignoring undiscounted subscription'} coupon_code = sub.discount.coupon.id try: coupon_owner = UserSettings.objects.get(coupon_code=coupon_code) except UserSettings.DoesNotExist: return {'success': 'coupon not owned by referrer'} if coupon_owner.plan == payment_plans.PLAN_DEMO or coupon_owner.plan == payment_plans.PLAN_COMMUNITY: return {'success': 'coupon owned by free user'} min_charge = float('inf') valid_charges = 0 # We limit this to three. If it goes over or under, we can ignore the charge. invoices = stripe.Invoice.list(customer=sub.customer, limit=3) if len(invoices.data) < 2: return {'success': 'did not reach two invoices yet'} for invoice in invoices.data: if not invoice.paid: continue valid_charges += 1 invoice_amount = 0 for line in invoice.lines.data: invoice_amount += line.amount if invoice_amount < min_charge: min_charge = invoice_amount if valid_charges != 2: return {'success': 'did not have two successful invoices'} try: owner_cust = coupon_owner.get_stripe_customer() except Exception as e: rollbar.report_message('Error fetching coupon owner stripe customer: %s' % str(e), 'error') return {'success': 'coupon owner does not exist'} else: if not owner_cust: rollbar.report_message('Coupon owner did not have valid stripe customer', 'error') return {'success': 'coupon owner does not exist'} amount = min_charge * 2 # Negative amounts are credits owner_cust.account_balance -= amount owner_cust.save() send_notification_email( coupon_owner.user, ugettext('You have referral credit!'), ugettext('One of your referrals has crossed their two-month mark ' 'as a paying customer. Your account has been credited ' '$%.2f.') % (float(amount) / 100)) return {'success': 'user credited'} elif body['type'] == 'invoice.payment_failed': if body.get('user_id'): return _handle_failed_tip_sub(body) else: return _handle_failed_subscription(body) return {'success': 'ignored'}