Exemple #1
0
def test__campaign_id_validation():
    ID_15_VALID_CHARS = "111AAA222bbb333"
    ID_18_VALID_CHARS = "111AAA222bbb333ccc"

    ID_15_INVALID_CHARS = "1!1A;-+22bbb333"
    ID_18_INVALID_CHARS = "111AAA222bbb333#c;"

    ID_INCORRECT_LENGTH = "AAADDD"

    opp = Opportunity(sf_connection=sf)

    opp.campaign_id = ID_15_VALID_CHARS
    assert not opp.has_invalid_campaign_id_format()

    opp.campaign_id = ID_18_VALID_CHARS
    assert not opp.has_invalid_campaign_id_format()

    opp.campaign_id = ID_15_INVALID_CHARS
    assert opp.has_invalid_campaign_id_format()

    opp.campaign_id = ID_18_INVALID_CHARS
    assert opp.has_invalid_campaign_id_format()

    opp.campaign_id = ID_INCORRECT_LENGTH
    assert opp.has_invalid_campaign_id_format()
Exemple #2
0
def add_opportunity(contact=None, form=None, customer=None, quarantine=False):
    """
    This will add a single donation to Salesforce.
    """

    logging.info("----Adding opportunity...")

    opportunity = Opportunity(contact=contact)
    opportunity.amount = form.get("amount", 0)
    opportunity.stripe_customer = customer["id"]
    opportunity.campaign_id = form["campaign_id"]
    opportunity.referral_id = form["referral_id"]
    opportunity.description = "Texas Tribune Membership"
    opportunity.agreed_to_pay_fees = form["pay_fees_value"]
    opportunity.encouraged_by = form["reason"]
    opportunity.lead_source = "Stripe"
    opportunity.quarantined = quarantine

    customer = stripe.Customer.retrieve(customer["id"])
    card = customer.sources.retrieve(customer.sources.data[0].id)
    year = card.exp_year
    month = card.exp_month
    day = calendar.monthrange(year, month)[1]

    opportunity.stripe_card_expiration = f"{year}-{month:02d}-{day:02d}"
    opportunity.stripe_card_brand = card.brand
    opportunity.stripe_card_last_4 = card.last4

    opportunity.save()
    return opportunity
Exemple #3
0
def add_business_opportunity(account=None, form=None, customer=None):
    """
    Adds a single business membership to Salesforce.
    """

    year = datetime.now(tz=ZONE).strftime("%Y")
    opportunity = Opportunity(account=account)
    opportunity.record_type_name = "Business Membership"
    opportunity.name = f"{year} Business {account.name} One time"
    opportunity.amount = form.get("amount", 0)
    opportunity.stripe_customer = customer["id"]
    opportunity.campaign_id = form["campaign_id"]
    opportunity.referral_id = form["referral_id"]
    opportunity.description = "Texas Tribune Business Membership"
    opportunity.agreed_to_pay_fees = form["pay_fees_value"]
    opportunity.encouraged_by = form["reason"]
    opportunity.lead_source = "Stripe"
    opportunity.save()
    return opportunity
Exemple #4
0
def test__format_slack():

    opportunity = Opportunity(sf_connection=sf)
    opportunity.account_id = "0011700000BpR8PAAV"
    opportunity.amount = 9
    opportunity.encouraged_by = "Because I love the Trib!"
    opportunity.name = "D C ([email protected])"
    opportunity.stripe_id = "cus_78MqJSBejMN9gn"
    opportunity.agreed_to_pay_fees = True
    opportunity.referral_id = "1234"
    opportunity.lead_source = "Stripe"
    opportunity.description = "The Texas Tribune Membership"
    opportunity.stripe_customer = "cus_78MqJSBejMN9gn"
    opportunity.campaign_id = "111111111111111"
    opportunity.campaign_name = "Test Campaign Name"

    no_campaign = Opportunity(sf_connection=sf)
    no_campaign.account_id = "0011700000BpR8PAAV"
    no_campaign.amount = 9
    no_campaign.encouraged_by = "Because I love the Trib!"
    no_campaign.name = "D C ([email protected])"
    no_campaign.stripe_id = "cus_78MqJSBejMN9gn"
    no_campaign.agreed_to_pay_fees = True
    no_campaign.referral_id = "1234"
    no_campaign.lead_source = "Stripe"
    no_campaign.description = "The Texas Tribune Membership"
    no_campaign.stripe_customer = "cus_78MqJSBejMN9gn"

    rdo = RDO(sf_connection=sf)
    rdo.referral_id = "1234"
    rdo.encouraged_by = "Because I love the Trib!"
    rdo.lead_source = "Stripe"
    rdo.contact_id = "0031700000BHQzBAAX"
    rdo.installment_period = "yearly"
    rdo.stripe_customer = "cus_78MqJSBejMN9gn"
    rdo.amount = 100
    rdo.name = "foo"
    rdo.installments = 3
    rdo.open_ended_status = None
    rdo.description = "Texas Tribune Circle Membership"
    rdo.agreed_to_pay_fees = True
    rdo.type = "Giving Circle"
    rdo.campaign_id = "000000000000000"
    rdo.campaign_name = "Recurring Test Campaign Name"

    contact = Contact(sf_connection=sf)
    contact.email = "*****@*****.**"
    contact.first_name = "D"
    contact.last_name = "C"
    contact.lead_source = "Stripe"
    contact.work_email = "*****@*****.**"

    account = Account(sf_connection=sf)
    account.name = "Acme Inc."
    account.website = "http://acme.com"
    account.shipping_street = "Street"
    account.shipping_city = "Austin"
    account.shipping_postalcode = "78701"
    account.shipping_state = "TX"
    account.record_type_name = "Household"

    actual = construct_slack_message(account=account,
                                     rdo=rdo,
                                     opportunity=None,
                                     contact=None)
    expected = "Acme Inc. pledged $100 [yearly] (Because I love the Trib!) (Recurring Test Campaign Name)"

    assert actual == expected

    actual = construct_slack_message(account=None,
                                     rdo=rdo,
                                     opportunity=None,
                                     contact=contact)
    expected = "D C pledged $100 [yearly] (Because I love the Trib!) (Recurring Test Campaign Name)"

    assert actual == expected

    actual = construct_slack_message(account=None,
                                     rdo=None,
                                     opportunity=opportunity,
                                     contact=contact)
    expected = (
        "D C pledged $9 [one-time] (Because I love the Trib!) (Test Campaign Name)"
    )

    assert actual == expected

    actual = construct_slack_message(account=None,
                                     rdo=None,
                                     opportunity=no_campaign,
                                     contact=contact)
    expected = "D C pledged $9 [one-time] (Because I love the Trib!) "

    assert actual == expected
Exemple #5
0
def authorization_notification(payload):

    amzn_id = payload["AuthorizationNotification"]["AuthorizationDetails"][
        "AmazonAuthorizationId"]

    # trim everything after the last dash - seems like there should be a more
    # straightforward way to do this
    match = re.search("^(.*)[-]", amzn_id)
    amzn_id = match.group(1)
    logging.info(amzn_id)

    client = AmazonPayClient(
        mws_access_key=MWS_ACCESS_KEY,
        mws_secret_key=MWS_SECRET_KEY,
        merchant_id=AMAZON_MERCHANT_ID,
        region="na",
        currency_code="USD",
        sandbox=AMAZON_SANDBOX,
    )
    response = client.get_order_reference_details(
        amazon_order_reference_id=amzn_id)
    response = response.to_dict()

    logging.info(json.dumps(response, indent=4))

    details = response["GetOrderReferenceDetailsResponse"][
        "GetOrderReferenceDetailsResult"]["OrderReferenceDetails"]

    amount = details["OrderTotal"]["Amount"]
    logging.info(amount)
    name = HumanName(details["Buyer"]["Name"])
    first_name = name.first
    last_name = name.last
    email = details["Buyer"]["Email"]
    zipcode = get_zip(details=details)
    description = details["SellerOrderAttributes"]["StoreName"]

    logging.info("----Getting contact....")
    contact = Contact.get_or_create(email=email,
                                    first_name=first_name,
                                    last_name=last_name,
                                    zipcode=zipcode)
    logging.info(contact)

    if contact.first_name == "Subscriber" and contact.last_name == "Subscriber":
        logging.info(f"Changing name of contact to {first_name} {last_name}")
        contact.first_name = first_name
        contact.last_name = last_name
        contact.save()

    if contact.first_name != first_name or contact.last_name != last_name:
        logging.info(
            f"Contact name doesn't match: {contact.first_name} {contact.last_name}"
        )

    if zipcode and not contact.created and contact.mailing_postal_code != zipcode:
        contact.mailing_postal_code = zipcode
        contact.save()

    logging.info("----Adding opportunity...")

    opportunity = Opportunity(contact=contact, stage_name="Closed Won")
    opportunity.amount = amount
    opportunity.description = description
    opportunity.lead_source = "Amazon Alexa"
    opportunity.amazon_order_id = amzn_id
    opportunity.campaign_id = AMAZON_CAMPAIGN_ID
    opportunity.name = (
        f"[Alexa] {contact.first_name} {contact.last_name} ({contact.email})")
    opportunity.save()
    logging.info(opportunity)
    notify_slack(contact=contact, opportunity=opportunity)
    if contact.duplicate_found:
        send_multiple_account_warning(contact)