'After receiving a message from a payment processor, apply gold.
Shared endpoint for all payment processing systems. Validation of gold
purchase (sender, recipient, etc.) should happen before hitting this.'

@classmethod
def complete_gold_purchase(cls, webhook):

    secret = webhook.passthrough
    transaction_id = webhook.transaction_id
    payer_email = webhook.payer_email
    payer_id = webhook.payer_id
    subscr_id = webhook.subscr_id
    pennies = webhook.pennies
    months = webhook.months
    goldtype = webhook.goldtype
    buyer = webhook.buyer
    buyer_email = webhook.buyer_email
    recipient = webhook.recipient
    signed = webhook.signed
    giftmessage = webhook.giftmessage
    thing = webhook.thing
    days = days_from_months(months)
    if (goldtype == 'code'):
        send_gold_code(buyer, months, days, transaction_id, payer_email, pennies, buyer_email)
        return
    gold_recipient = (recipient or buyer)
    with gold_recipient.get_read_modify_write_lock() as lock:
        gold_recipient.update_from_cache(lock)
        secret_pieces = [goldtype]
        if (goldtype == 'gift'):
            secret_pieces.append(recipient.name)
        secret_pieces.append((secret or transaction_id))
        secret = '-'.join(secret_pieces)
        if (goldtype in ('onetime', 'autorenew')):
            admintools.adjust_gold_expiration(buyer, days=days)
            if (goldtype == 'onetime'):
                subject = 'thanks    for    buying    reddit    gold!'
                if g.lounge_reddit:
                    message = strings.lounge_msg
                else:
                    message = ':)'
            elif has_prev_subscr_payments(subscr_id):
                secret = None
                (subject, message) = subscr_pm(pennies, months, new_subscr=False)
            else:
                (subject, message) = subscr_pm(pennies, months, new_subscr=True)
        elif (goldtype == 'creddits'):
            buyer._incr('gold_creddits', months)
            subject = 'thanks    for    buying    creddits!'
            message = ("To    spend    them,    visit    %s://%s/gold    or    your    favorite    person's    userpage." % (g.default_scheme, g.domain))
        elif (goldtype == 'gift'):
            send_gift(buyer, recipient, months, days, signed, giftmessage, thing)
            subject = 'thanks    for    giving    reddit    gold!'
            message = ('Your    gift    to    %s    has    been    delivered.' % recipient.name)
        try:
            create_claimed_gold(transaction_id, payer_email, payer_id, pennies, days, secret, buyer._id, c.start_time, subscr_id=subscr_id, status='processed')
        except IntegrityError:
            g.log.error('gold:    got    duplicate    gold    transaction')
        try:
            message = append_random_bottlecap_phrase(message)
            send_system_message(buyer, subject, message, distinguished='gold-auto')
        except MessageError:
            g.log.error('complete_gold_purchase:    send_system_message    error')
