def add_blast_subscription(form=None, customer=None): """ Adds a Blast subscription. Blast subscriptions are always recurring. They have two email addresses: one for billing and one for the newsletter subscription. """ form = clean(form) first_name = form["first_name"] last_name = form["last_name"] email = form["subscriber_email"] logging.info("----Getting contact...") contact = Contact.get_or_create(email=email, first_name=first_name, last_name=last_name) logging.info(contact) rdo = RDO(contact=contact) rdo.stripe_customer = customer["id"] rdo.campaign_id = form["campaign_id"] rdo.referral_id = form["referral_id"] rdo.lead_source = "Stripe" rdo.amount = form.get("amount", 0) rdo.agreed_to_pay_fees = form["pay_fees_value"] # Blast specific: rdo.installments = 0 rdo.description = "Blast Subscription" rdo.open_ended_status = "Open" if int(float(rdo.amount)) == 40: rdo.installment_period = "monthly" else: rdo.installment_period = "yearly" now = datetime.now(tz=ZONE).strftime("%Y-%m-%d %I:%M:%S %p %Z") rdo.name = f"{first_name} {last_name} - {now} - The Blast" rdo.type = "The Blast" rdo.billing_email = form["stripeEmail"] rdo.blast_subscription_email = form["subscriber_email"] logging.info("----Saving RDO....") apply_card_details(rdo=rdo, customer=customer) rdo.save() logging.info(rdo) # get opportunities opportunities = rdo.opportunities() today = datetime.now(tz=ZONE).strftime("%Y-%m-%d") opp = [ opportunity for opportunity in opportunities if opportunity.expected_giving_date == today ][0] try: charge(opp) except ChargeException: # TODO should we alert slack? Did not because we had no notifications here before. pass return True
def test__format_blast_rdo(): rdo = RDO(sf_connection=sf) rdo.referral_id = "1234" rdo.lead_source = "Stripe" rdo.contact_id = "0031700000BHQzBAAX" rdo.installment_period = "monthly" rdo.stripe_customer = "cus_78MqJSBejMN9gn" rdo.amount = 40 rdo.name = "foo" rdo.installments = 0 rdo.open_ended_status = "Open" rdo.description = "Monthly Blast Subscription" rdo.agreed_to_pay_fees = True rdo.type = "The Blast" rdo.billing_email = "*****@*****.**" rdo.blast_subscription_email = "*****@*****.**" rdo.quarantined = True response = rdo._format() expected_response = { "Referral_ID__c": "1234", "Encouraged_to_contribute_by__c": None, "npe03__Date_Established__c": today, "Lead_Source__c": "Stripe", "npe03__Contact__c": "0031700000BHQzBAAX", "npe03__Installment_Period__c": "monthly", "Stripe_Customer_ID__c": "cus_78MqJSBejMN9gn", "npe03__Amount__c": "40.00", "Name": "foo", "npe03__Installments__c": 0, "npe03__Open_Ended_Status__c": "Open", "Stripe_Description__c": "Monthly Blast Subscription", "Stripe_Agreed_to_pay_fees__c": True, "Type__c": "The Blast", "Billing_Email__c": "*****@*****.**", "Blast_Subscription_Email__c": "*****@*****.**", "npe03__Organization__c": None, "npe03__Recurring_Donation_Campaign__c": None, "Stripe_Card_Brand__c": None, "Stripe_Card_Last_4__c": None, "Stripe_Card_Expiration__c": None, "Quarantined__c": True, } response["Name"] = "foo" assert response == expected_response