def update_donation_from_invoice(donation, invoice): mode = 'live' is_test = invoice['livemode'] is False if invoice['livemode'] == False: mode = 'test' # For recurring invoices, there should only be one line item line_item = invoice['lines']['data'][0] plan = line_item['plan'] # The full charge is needed to determine if a refund was issued mode = 'live' if invoice['livemode'] == False: mode = 'test' stripe_util = getUtility(IStripeUtility) stripe_api = stripe_util.get_stripe_api(mode=mode) charge = stripe_api.Charge.retrieve(invoice['charge']) # Stripe handles amounts as cents amount = (plan['amount'] * line_item['quantity']) / 100 # Stripe amounts are in cents donation.amount = amount donation.stripe_customer_id = invoice['customer'] donation.stripe_plan_id = plan['id'] donation.transaction_id = invoice['charge'] donation.is_test = is_test if charge['paid'] and not charge['refunded']: donation.stage = 'Posted' else: donation.stage = 'Withdrawn' donation.payment_method = 'Stripe' donation.payment_date = stripe_timestamp_to_date(invoice['date']) # Ensure no emails get set out from the update donation.is_receipt_sent = True donation.is_notification_sent = True donation.reindexObject() return donation
def update_donation_from_invoice(donation, invoice): mode = 'live' is_test = invoice['livemode'] is False if invoice['livemode'] == False: mode = 'test' line_item = get_subscription_line_item(invoice) plan = line_item['plan'] # The full charge is needed to determine if a refund was issued mode = 'live' if invoice['livemode'] == False: mode = 'test' stripe_util = getUtility(IStripeUtility) stripe_api = stripe_util.get_stripe_api(mode=mode) charge = stripe_api.Charge.retrieve(invoice['charge']) # Stripe handles amounts as cents amount = (plan['amount'] * line_item['quantity']) / 100 # Stripe amounts are in cents donation.amount = amount donation.stripe_customer_id = invoice['customer'] donation.stripe_plan_id = plan['id'] donation.transaction_id = invoice['charge'] donation.is_test = is_test if charge['paid'] and not charge['refunded']: donation.stage = 'Posted' else: donation.stage = 'Withdrawn' donation.payment_method = 'Stripe' donation.payment_date = stripe_timestamp_to_date(invoice['date']) # Ensure no emails get set out from the update donation.is_receipt_sent = True donation.is_notification_sent = True donation.reindexObject() return donation
def make_donation_from_invoice(invoice, container): last_donation = get_last_donation_for_invoice(invoice) mode = 'live' is_test = invoice['livemode'] is False if invoice['livemode'] == False: mode = 'test' line_item = get_subscription_line_item(invoice) plan = line_item['plan'] # Stripe handles amounts as cents amount = (plan['amount'] * line_item['quantity']) / 100 # In theory, there should never be a case where a previous donation # for the same customer and plan does not already exist. In practice, # it happens so try to handle the exception by parsing contact data from # card info if last_donation is not None: # Found last donation, get contact data from it data = { 'title': '%s %s - $%i per %s' % ( last_donation.first_name, last_donation.last_name, amount, plan['interval'], ), 'first_name': last_donation.first_name, 'last_name': last_donation.last_name, 'email': last_donation.email, 'email_opt_in': last_donation.email_opt_in, 'phone': last_donation.phone, 'address_street': last_donation.address_street, 'address_city': last_donation.address_city, 'address_state': last_donation.address_state, 'address_zip': last_donation.address_zip, 'address_country': last_donation.address_country, 'source_url': last_donation.source_url, 'source_campaign_sf_id': last_donation.source_campaign_sf_id, } else: # No last donation found, fetch charge data and attempt to parse stripe_util = getUtility(IStripeUtility) stripe_api = stripe_util.get_stripe_api(mode=mode) charge = stripe_api.Charge.retrieve(invoice['charge'], expand=['card', 'customer']) customer = charge['customer'] card = charge['card'] description_parts = customer['description'].split('|') first_name = '' last_name = '' campaign_sf_id = None if len(description_parts) >= 1: first_name = description_parts[0] if len(description_parts) >= 2: last_name = description_parts[1] if len(description_parts) >= 3: campaign_sf_id = description_parts[2] address_parts = [] if card['address_line1']: address_parts.append(card['address_line1']) if card['address_line2']: address_parts.append(card['address_line2']) data = { 'title': '%s %s - $%i per %s' % ( first_name, last_name, amount, plan['interval'], ), 'first_name': first_name, 'last_name': last_name, 'email': customer.email, 'address_street': '\n'.join(address_parts), 'address_city': card['address_city'], 'address_state': card['address_state'], 'address_zip': card['address_zip'], 'address_country': card['address_country'], } # Stripe amounts are in cents data['amount'] = amount data['stripe_customer_id'] = invoice['customer'] data['stripe_plan_id'] = plan['id'] data['transaction_id'] = invoice['charge'] data['is_test'] = is_test data['campaign_sf_id'] = container.sf_object_id data['secret_key'] = build_secret_key() data['stage'] = 'Posted' data['payment_method'] = 'Stripe' data['payment_date'] = stripe_timestamp_to_date(invoice['date']) # Suppress sending an email receipt data['is_receipt_sent'] = True # Suppress sending a notification of the donation to a personal fundraiser data['is_notification_sent'] = True donation = createContentInContainer( container, 'collective.salesforce.fundraising.donation', checkConstraints=False, **data) # Send the Stripe monthly email receipt donation.send_email_recurring_receipt() return donation
def make_donation_from_invoice(invoice, container): last_donation = get_last_donation_for_invoice(invoice) mode = 'live' is_test = invoice['livemode'] is False if invoice['livemode'] == False: mode = 'test' line_item = get_subscription_line_item(invoice) plan = line_item['plan'] # Stripe handles amounts as cents amount = (plan['amount'] * line_item['quantity']) / 100 # In theory, there should never be a case where a previous donation # for the same customer and plan does not already exist. In practice, # it happens so try to handle the exception by parsing contact data from # card info if last_donation is not None: # Found last donation, get contact data from it data = { 'title': '%s %s - $%i per %s' % ( last_donation.first_name, last_donation.last_name, amount, plan['interval'], ), 'first_name': last_donation.first_name, 'last_name': last_donation.last_name, 'email': last_donation.email, 'email_opt_in': last_donation.email_opt_in, 'phone': last_donation.phone, 'address_street': last_donation.address_street, 'address_city': last_donation.address_city, 'address_state': last_donation.address_state, 'address_zip': last_donation.address_zip, 'address_country': last_donation.address_country, 'source_url': last_donation.source_url, 'source_campaign_sf_id': last_donation.source_campaign_sf_id, } else: # No last donation found, fetch charge data and attempt to parse stripe_util = getUtility(IStripeUtility) stripe_api = stripe_util.get_stripe_api(mode=mode) charge = stripe_api.Charge.retrieve(invoice['charge'], expand=['card','customer']) customer = charge['customer'] card = charge['card'] description_parts = customer['description'].split('|') first_name = '' last_name = '' campaign_sf_id = None if len(description_parts) >= 1: first_name = description_parts[0] if len(description_parts) >= 2: last_name = description_parts[1] if len(description_parts) >= 3: campaign_sf_id = description_parts[2] address_parts = [] if card['address_line1']: address_parts.append(card['address_line1']) if card['address_line2']: address_parts.append(card['address_line2']) data = { 'title': '%s %s - $%i per %s' % ( first_name, last_name, amount, plan['interval'], ), 'first_name': first_name, 'last_name': last_name, 'email': customer.email, 'address_street': '\n'.join(address_parts), 'address_city': card['address_city'], 'address_state': card['address_state'], 'address_zip': card['address_zip'], 'address_country': card['address_country'], } # Stripe amounts are in cents data['amount'] = amount data['stripe_customer_id'] = invoice['customer'] data['stripe_plan_id'] = plan['id'] data['transaction_id'] = invoice['charge'] data['is_test'] = is_test data['campaign_sf_id'] = container.sf_object_id data['secret_key'] = build_secret_key() data['stage'] = 'Posted' data['payment_method'] = 'Stripe' data['payment_date'] = stripe_timestamp_to_date(invoice['date']) # Suppress sending an email receipt data['is_receipt_sent'] = True # Suppress sending a notification of the donation to a personal fundraiser data['is_notification_sent'] = True donation = createContentInContainer( container, 'collective.salesforce.fundraising.donation', checkConstraints=False, **data ) # Send the Stripe monthly email receipt donation.send_email_recurring_receipt() return donation