Repository: mozmeao/basket

Issue:
SubHub > Salesforce integration update to process reactivated subscriptions
Update basket to accept new event type for reactivated subscriptions. Mappings can be found here ("Reactivation" worksheet) (https://docs.google.com/spreadsheets/d/1z6aXj_9GUpl5g_hR2k_SmjY5tyhxvnoHGOr8e-cbo18/edit#gid=962422475

Jackie will let us know when the payload is ready to be tested. 

Desiree will update SFDC's sandbox to include the new stage name


Discussion:
Stage name of "Reactivation" added to the ADVO sandbox in preparation for testing. 

Resolving patch:
diff --git a/basket/news/tasks.py b/basket/news/tasks.py
--- a/basket/news/tasks.py
+++ b/basket/news/tasks.py
@@ -786,6 +786,10 @@ def process_subhub_event_subscription_charge(data):
         statsd.incr('news.tasks.process_subhub_event.subscription_charge.user_not_found')
         raise RetryTask('Could not find user. Try again.')
 
+    nickname = data['nickname']
+    if isinstance(nickname, list):
+        nickname = nickname[0]
+
     # if a customer re-instates service after a cancellation, the record needs to be updated
     sfdc.opportunity.upsert(f'PMT_Invoice_ID__c/{data["invoice_id"]}', {
         'Amount': cents_to_dollars(data['plan_amount']),
@@ -805,11 +809,42 @@ def process_subhub_event_subscription_charge(data):
         'PMT_Subscription_ID__c': data['subscription_id'],
         'PMT_Transaction_ID__c': data['charge'],
         'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE,
-        'Service_Plan__c': data['nickname'],
+        'Service_Plan__c': nickname,
         'StageName': 'Closed Won',
     })
 
 
+@et_task
+def process_subhub_event_subscription_reactivated(data):
+    statsd.incr('news.tasks.process_subhub_event.subscription_reactivated')
+    user_data = get_user_data(payee_id=data['customer_id'],
+                              extra_fields=['id'])
+    if not user_data:
+        statsd.incr('news.tasks.process_subhub_event.subscription_reactivated.user_not_found')
+        raise RetryTask('Could not find user. Try again.')
+
+    nickname = data['nickname']
+    if isinstance(nickname, list):
+        nickname = nickname[0]
+
+    sfdc.opportunity.create({
+        'Amount': cents_to_dollars(data['plan_amount']),
+        'Billing_Cycle_End__c': iso_format_unix_timestamp(data['current_period_end']),
+        'CloseDate': iso_format_unix_timestamp(data.get('close_date', time())),
+        'Credit_Card_Type__c': data['brand'],
+        'Last_4_Digits__c': data['last4'],
+        'Donation_Contact__c': user_data['id'],
+        'Event_Id__c': data['event_id'],
+        'Event_Name__c': data['event_type'],
+        'Name': 'Subscription Services',
+        'Payment_Source__c': 'Stripe',
+        'PMT_Subscription_ID__c': data['subscription_id'],
+        'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE,
+        'Service_Plan__c': nickname,
+        'StageName': 'Reactivation',
+    })
+
+
 SUB_STAGE_NAMES = {
     'customer.subscription_cancelled': 'Subscription Canceled',
     'customer.deleted': 'Account Deleted',
@@ -877,6 +912,10 @@ def process_subhub_event_payment_failed(data):
         statsd.incr('news.tasks.process_subhub_event.payment_failed.user_not_found')
         raise RetryTask('Could not find user. Try again.')
 
+    nickname = data['nickname']
+    if isinstance(nickname, list):
+        nickname = nickname[0]
+
     sfdc.opportunity.create({
         'Amount': cents_to_dollars(data['amount_due']),
         'CloseDate': iso_format_unix_timestamp(data['created']),
@@ -888,7 +927,7 @@ def process_subhub_event_payment_failed(data):
         'PMT_Transaction_ID__c': data['charge_id'],
         'Payment_Source__c': 'Stripe',
         'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE,
-        'Service_Plan__c': data['nickname'],
+        'Service_Plan__c': nickname,
         'StageName': 'Payment Failed',
         'currency__c': data['currency'],
     })
diff --git a/basket/news/views.py b/basket/news/views.py
--- a/basket/news/views.py
+++ b/basket/news/views.py
@@ -43,6 +43,7 @@
     process_subhub_event_payment_failed,
     process_subhub_event_subscription_cancel,
     process_subhub_event_subscription_charge,
+    process_subhub_event_subscription_reactivated,
 )
 from basket.news.utils import (
     SET,
@@ -82,6 +83,7 @@
     'customer.source.expiring': process_subhub_event_credit_card_expiring,
     'customer.subscription.created': process_subhub_event_subscription_charge,  # subscriptioin creations & recurring charges
     'customer.subscription_cancelled': process_subhub_event_subscription_cancel,
+    'customer.subscription.reactivated': process_subhub_event_subscription_reactivated,
     'customer.deleted': process_subhub_event_subscription_cancel,
     'invoice.payment_failed': process_subhub_event_payment_failed,
 }


Test patch: