def process_webhook(self, event_type, passthrough, transaction_id, pennies, months): if event_type == 'noop': return try: payment_blob = validate_blob(passthrough) except GoldException as e: g.log.error('%s %s: bad payment_blob %s' % (self.name, transaction_id, e)) self.abort403() goldtype = payment_blob['goldtype'] buyer = payment_blob['buyer'] recipient = payment_blob.get('recipient', None) signed = payment_blob.get('signed', False) giftmessage = payment_blob.get('giftmessage', None) comment = payment_blob.get('comment', None) comment = comment._fullname if comment else None existing = retrieve_gold_transaction(transaction_id) if event_type == 'cancelled': subject = 'gold payment cancelled' msg = ('your gold payment has been cancelled, contact ' '%(gold_email)s for details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(transaction_id) elif event_type == 'succeeded': if existing and existing.status == 'processed': g.log.info('POST_goldwebhook skipping %s' % transaction_id) return payer_email = '' payer_id = '' subscription_id = None complete_gold_purchase(passthrough, transaction_id, payer_email, payer_id, subscription_id, pennies, months, goldtype, buyer, recipient, signed, giftmessage, comment) elif event_type == 'failed': subject = 'gold payment failed' msg = ('your gold payment has failed, contact %(gold_email)s for ' 'details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) # probably want to update gold_table here elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = 'gold refund' msg = ('your gold payment has been refunded, contact ' '%(gold_email)s for details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) reverse_gold_purchase(transaction_id)
def process_webhook(self, event_type, passthrough, transaction_id, pennies, months): if event_type == 'noop': return try: payment_blob = validate_blob(passthrough) except GoldError as e: g.log.error('%s %s: bad payment_blob %s' % (self.name, transaction_id, e)) self.abort403() goldtype = payment_blob['goldtype'] buyer = payment_blob['buyer'] recipient = payment_blob.get('recipient', None) signed = payment_blob.get('signed', False) giftmessage = payment_blob.get('giftmessage', None) comment = payment_blob.get('comment', None) comment = comment._fullname if comment else None existing = retrieve_gold_transaction(transaction_id) if event_type == 'cancelled': subject = 'gold payment cancelled' msg = ('your gold payment has been cancelled, contact ' '%(gold_email)s for details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(transaction_id) elif event_type == 'succeeded': if existing and existing.status == 'processed': g.log.info('POST_goldwebhook skipping %s' % transaction_id) return payer_email = '' payer_id = '' subscription_id = None complete_gold_purchase(passthrough, transaction_id, payer_email, payer_id, subscription_id, pennies, months, goldtype, buyer, recipient, signed, giftmessage, comment) elif event_type == 'failed': subject = 'gold payment failed' msg = ('your gold payment has failed, contact %(gold_email)s for ' 'details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) # probably want to update gold_table here elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = 'gold refund' msg = ('your gold payment has been refunded, contact ' '%(gold_email)s for details' % {'gold_email': g.goldthanks_email}) send_system_message(buyer, subject, msg) reverse_gold_purchase(transaction_id)
def process_webhook(self, event_type, webhook): if event_type == "noop": return existing = retrieve_gold_transaction(webhook.transaction_id) if not existing and webhook.passthrough: try: webhook.load_blob() except GoldException as e: g.log.error("%s: payment_blob %s", webhook.transaction_id, e) self.abort403() msg = None if event_type == "cancelled": subject = _("reddit gold payment cancelled") msg = _("Your reddit gold payment has been cancelled, contact " "%(gold_email)s for details") % { "gold_email": g.goldthanks_email } if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(webhook.transaction_id) elif event_type == "succeeded": if existing and existing.status == "processed": g.log.info("POST_goldwebhook skipping %s" % webhook.transaction_id) return self.complete_gold_purchase(webhook) elif event_type == "failed": subject = _("reddit gold payment failed") msg = _("Your reddit gold payment has failed, contact " "%(gold_email)s for details") % { "gold_email": g.goldthanks_email } elif event_type == "failed_subscription": subject = _("reddit gold subscription payment failed") msg = _( "Your reddit gold subscription payment has failed. " "Please go to http://www.reddit.com/subscription to " "make sure your information is correct, or contact " "%(gold_email)s for details" ) % {"gold_email": g.goldthanks_email} elif event_type == "refunded": if not (existing and existing.status == "processed"): return subject = _("reddit gold refund") msg = _("Your reddit gold payment has been refunded, contact " "%(gold_email)s for details") % { "gold_email": g.goldthanks_email } reverse_gold_purchase(webhook.transaction_id) if msg: if existing: buyer = Account._byID(int(existing.account_id), data=True) elif webhook.buyer: buyer = webhook.buyer else: return send_system_message(buyer, subject, msg)
def reverse_gold_purchase(transaction_id): transaction = retrieve_gold_transaction(transaction_id) if not transaction: raise GoldException('gold_table %s not found' % transaction_id) buyer = Account._byID(int(transaction.account_id), data=True) recipient = None days = transaction.days months = days / 31 secret = transaction.secret if '{' in secret: secret.strip('{}') # I goofed pieces = secret.split(',') else: pieces = secret.split('-') goldtype = pieces[0] if goldtype == 'gift': recipient_name, secret = pieces[1:] recipient = Account._by_name(recipient_name) gold_recipient = recipient or buyer with gold_lock(gold_recipient): gold_recipient._sync_latest() if goldtype in ('onetime', 'autorenew'): subtract_gold_days(buyer, days) elif goldtype == 'creddits': subtract_gold_creddits(buyer, months) elif goldtype == 'gift': subtract_gold_days(recipient, days) subject = 'your gifted gold has been reversed' message = 'sorry, but the payment was reversed' send_system_message(recipient, subject, message) update_gold_transaction(transaction_id, 'reversed')
def reverse_gold_purchase(transaction_id): transaction = retrieve_gold_transaction(transaction_id) if not transaction: raise GoldException("gold_table %s not found" % transaction_id) buyer = Account._byID(int(transaction.account_id), data=True) recipient = None days = transaction.days months = days / 31 secret = transaction.secret if "{" in secret: secret.strip("{}") # I goofed pieces = secret.split(",") else: pieces = secret.split("-") goldtype = pieces[0] if goldtype == "gift": recipient_name, secret = pieces[1:] recipient = Account._by_name(recipient_name) gold_recipient = recipient or buyer with gold_lock(gold_recipient): gold_recipient._sync_latest() if goldtype in ("onetime", "autorenew"): subtract_gold_days(buyer, days) elif goldtype == "creddits": subtract_gold_creddits(buyer, months) elif goldtype == "gift": subtract_gold_days(recipient, days) subject = "your gifted gold has been reversed" message = "sorry, but the payment was reversed" send_system_message(recipient, subject, message) update_gold_transaction(transaction_id, "reversed")
def reverse_gold_purchase(transaction_id): transaction = retrieve_gold_transaction(transaction_id) if not transaction: raise GoldException('gold_table %s not found' % transaction_id) buyer = Account._byID(int(transaction.account_id), data=True) recipient = None days = transaction.days months = days / 31 secret = transaction.secret if '{' in secret: secret.strip('{}') # I goofed pieces = secret.split(',') else: pieces = secret.split('-') goldtype = pieces[0] if goldtype == 'gift': recipient_name, secret = pieces[1:] recipient = Account._by_name(recipient_name) gold_recipient = recipient or buyer with gold_lock(gold_recipient): gold_recipient._sync_latest() if goldtype in ('onetime', 'autorenew'): subtract_gold_days(buyer, days) elif goldtype == 'creddits': subtract_gold_creddits(buyer, months) elif goldtype == 'gift': subtract_gold_days(recipient, days) subject = 'your gifted gold has been reversed' message = 'sorry, but the payment was reversed' send_system_message(recipient, subject, message) update_gold_transaction(transaction_id, 'reversed')
def process_webhook(self, event_type, webhook): if event_type == 'noop': return existing = retrieve_gold_transaction(webhook.transaction_id) if not existing and webhook.passthrough: try: webhook.load_blob() except GoldException as e: g.log.error('%s: payment_blob %s', webhook.transaction_id, e) self.abort403() msg = None if event_type == 'cancelled': subject = _('reddit gold payment cancelled') msg = _('Your reddit gold payment has been cancelled, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(webhook.transaction_id) elif event_type == 'succeeded': if (existing and existing.status in ('processed', 'unclaimed', 'claimed')): g.log.info('POST_goldwebhook skipping %s' % webhook.transaction_id) return self.complete_gold_purchase(webhook) elif event_type == 'failed': subject = _('reddit gold payment failed') msg = _('Your reddit gold payment has failed, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} elif event_type == 'failed_subscription': subject = _('reddit gold subscription payment failed') msg = _('Your reddit gold subscription payment has failed. Please ' 'go to [your gold subscription page](%(gold_subscription)s) ' 'to make sure your information is correct, or contact ' '%(gold_email)s for details') msg %= {'gold_subscription': '/gold/subscription', 'gold_email': g.goldthanks_email} elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = _('reddit gold refund') msg = _('Your reddit gold payment has been refunded, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} reverse_gold_purchase(webhook.transaction_id) if msg: if existing: buyer = Account._byID(int(existing.account_id), data=True) elif webhook.buyer: buyer = webhook.buyer else: return try: send_system_message(buyer, subject, msg) except MessageError: g.log.error('process_webhook: send_system_message error')
def process_webhook(self, event_type, webhook): if event_type == 'noop': return existing = retrieve_gold_transaction(webhook.transaction_id) if not existing and webhook.passthrough: try: webhook.load_blob() except GoldException as e: g.log.error('%s: payment_blob %s', webhook.transaction_id, e) if self.abort_on_error: self.abort403() else: return msg = None if event_type == 'cancelled': subject = _('reddit gold payment cancelled') msg = _('Your reddit gold payment has been cancelled, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(webhook.transaction_id) elif event_type == 'succeeded': if (existing and existing.status in ('processed', 'unclaimed', 'claimed')): g.log.info('POST_goldwebhook skipping %s' % webhook.transaction_id) return self.complete_gold_purchase(webhook) elif event_type == 'failed': subject = _('reddit gold payment failed') msg = _('Your reddit gold payment has failed, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} elif event_type == 'deleted_subscription': # the subscription may have been deleted directly by the user using # POST_delete_subscription, in which case gold_subscr_id is already # unset and we don't need to message them if webhook.buyer and webhook.buyer.gold_subscr_id: subject = _('reddit gold subscription cancelled') msg = _('Your reddit gold subscription has been cancelled ' 'because your credit card could not be charged. ' 'Contact %(gold_email)s for details') msg %= {'gold_email': g.goldthanks_email} webhook.buyer.gold_subscr_id = None webhook.buyer._commit() elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = _('reddit gold refund') msg = _('Your reddit gold payment has been refunded, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} reverse_gold_purchase(webhook.transaction_id) if msg: if existing: buyer = Account._byID(int(existing.account_id), data=True) elif webhook.buyer: buyer = webhook.buyer else: return try: send_system_message(buyer, subject, msg) except MessageError: g.log.error('process_webhook: send_system_message error')
def process_webhook(self, event_type, webhook): if event_type == 'noop': return existing = retrieve_gold_transaction(webhook.transaction_id) if not existing and webhook.passthrough: try: webhook.load_blob() except GoldException as e: g.log.error('%s: payment_blob %s', webhook.transaction_id, e) self.abort403() msg = None if event_type == 'cancelled': subject = _('reddit gold payment cancelled') msg = _('Your reddit gold payment has been cancelled, contact ' '%(gold_email)s for details') % { 'gold_email': g.goldthanks_email } if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(webhook.transaction_id) elif event_type == 'succeeded': if existing and existing.status == 'processed': g.log.info('POST_goldwebhook skipping %s' % webhook.transaction_id) return self.complete_gold_purchase(webhook) elif event_type == 'failed': subject = _('reddit gold payment failed') msg = _('Your reddit gold payment has failed, contact ' '%(gold_email)s for details') % { 'gold_email': g.goldthanks_email } elif event_type == 'failed_subscription': subject = _('reddit gold subscription payment failed') msg = _('Your reddit gold subscription payment has failed. ' 'Please go to http://www.reddit.com/subscription to ' 'make sure your information is correct, or contact ' '%(gold_email)s for details') % { 'gold_email': g.goldthanks_email } elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = _('reddit gold refund') msg = _('Your reddit gold payment has been refunded, contact ' '%(gold_email)s for details') % { 'gold_email': g.goldthanks_email } reverse_gold_purchase(webhook.transaction_id) if msg: if existing: buyer = Account._byID(int(existing.account_id), data=True) elif webhook.buyer: buyer = webhook.buyer else: return send_system_message(buyer, subject, msg)
def process_webhook(self, event_type, webhook): if event_type == 'noop': return existing = retrieve_gold_transaction(webhook.transaction_id) if not existing and webhook.passthrough: try: webhook.load_blob() except GoldException as e: g.log.error('%s: payment_blob %s', webhook.transaction_id, e) self.abort403() msg = None if event_type == 'cancelled': subject = _('reddit gold payment cancelled') msg = _('Your reddit gold payment has been cancelled, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(webhook.transaction_id) elif event_type == 'succeeded': if (existing and existing.status in ('processed', 'unclaimed', 'claimed')): g.log.info('POST_goldwebhook skipping %s' % webhook.transaction_id) return self.complete_gold_purchase(webhook) elif event_type == 'failed': subject = _('reddit gold payment failed') msg = _('Your reddit gold payment has failed, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} elif event_type == 'deleted_subscription': # the subscription may have been deleted directly by the user using # POST_delete_subscription, in which case gold_subscr_id is already # unset and we don't need to message them if webhook.buyer and webhook.buyer.gold_subscr_id: subject = _('reddit gold subscription cancelled') msg = _('Your reddit gold subscription has been cancelled ' 'because your credit card could not be charged. ' 'Contact %(gold_email)s for details') msg %= {'gold_email': g.goldthanks_email} webhook.buyer.gold_subscr_id = None webhook.buyer._commit() elif event_type == 'refunded': if not (existing and existing.status == 'processed'): return subject = _('reddit gold refund') msg = _('Your reddit gold payment has been refunded, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} reverse_gold_purchase(webhook.transaction_id) if msg: if existing: buyer = Account._byID(int(existing.account_id), data=True) elif webhook.buyer: buyer = webhook.buyer else: return try: send_system_message(buyer, subject, msg) except MessageError: g.log.error('process_webhook: send_system_message error')
def process_webhook(self, event_type, passthrough, transaction_id, pennies, months): if event_type == "noop": return try: payment_blob = validate_blob(passthrough) except GoldError as e: g.log.error("%s %s: bad payment_blob %s" % (self.name, transaction_id, e)) self.abort403() goldtype = payment_blob["goldtype"] buyer = payment_blob["buyer"] recipient = payment_blob.get("recipient", None) signed = payment_blob.get("signed", False) giftmessage = payment_blob.get("giftmessage", None) comment = payment_blob.get("comment", None) comment = comment._fullname if comment else None existing = retrieve_gold_transaction(transaction_id) if event_type == "cancelled": subject = "gold payment cancelled" msg = "your gold payment has been cancelled, contact " "%(gold_email)s for details" % { "gold_email": g.goldthanks_email } send_system_message(buyer, subject, msg) if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens reverse_gold_purchase(transaction_id) elif event_type == "succeeded": if existing and existing.status == "processed": g.log.info("POST_goldwebhook skipping %s" % transaction_id) return payer_email = "" payer_id = "" subscription_id = None complete_gold_purchase( passthrough, transaction_id, payer_email, payer_id, subscription_id, pennies, months, goldtype, buyer, recipient, signed, giftmessage, comment, ) elif event_type == "failed": subject = "gold payment failed" msg = "your gold payment has failed, contact %(gold_email)s for " "details" % { "gold_email": g.goldthanks_email } send_system_message(buyer, subject, msg) # probably want to update gold_table here elif event_type == "refunded": if not (existing and existing.status == "processed"): return subject = "gold refund" msg = "your gold payment has been refunded, contact " "%(gold_email)s for details" % { "gold_email": g.goldthanks_email } send_system_message(buyer, subject, msg) reverse_gold_purchase(transaction_id)