Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
class Integrations::Stripe::CustomerSummary
RECENT_LIMIT = 5
def initialize(connection:, contact:)
@connection = connection
@contact = contact
end
def perform(customer_id: nil)
return { customers: [], missing_email: true } if @contact.email.blank?
@options = { api_key: @connection.api_token }
@products = {}
escaped_email = @contact.email.gsub(/["\\]/) { |char| "\\#{char}" }
customers = ::Stripe::Customer.search({ query: "email:\"#{escaped_email}\"", limit: 100 }, @options)
matches = customers.data.select { |customer| customer.email&.casecmp?(@contact.email) }
.map { |customer| customer.to_hash.slice(:id, :name, :email, :phone) }
selected = select_customer(matches, customer_id)
result = { customers: matches, has_more_customers: customers.has_more }
selected ? result.merge(customer: selected, **billing_details(selected[:id])) : result
end
private
def select_customer(matches, customer_id)
selected = matches.find { |customer| customer[:id] == customer_id } if customer_id
raise ActiveRecord::RecordNotFound if customer_id && !selected
selected || (matches.first if matches.one?)
end
def billing_details(customer_id)
subscriptions = ::Stripe::Subscription.list({ customer: customer_id, status: 'all', limit: RECENT_LIMIT }, @options)
invoices = ::Stripe::Invoice.list({ customer: customer_id, limit: RECENT_LIMIT }, @options)
subscriptions = subscriptions.data.map(&:to_hash)
load_products(subscriptions)
{
subscriptions: subscriptions.map { |subscription| subscription_details(subscription) },
invoices: invoices.data.map { |invoice| invoice.to_hash.slice(:id, :number, :status, :total, :currency, :created) }
}
end
def subscription_details(subscription)
subscription.slice(:id, :status, :trial_end, :cancel_at, :cancel_at_period_end, :canceled_at).merge(
items: subscription.fetch(:items, {}).fetch(:data, []).map { |item| subscription_item(item, subscription) },
has_more_items: subscription.dig(:items, :has_more) || false
)
end
def subscription_item(item, subscription)
price = item.fetch(:price)
{
id: item[:id], name: @products[price[:product]] || price[:nickname], quantity: item[:quantity],
unit_amount: price[:unit_amount_decimal] || price[:unit_amount], currency: price[:currency],
recurring: price.fetch(:recurring).slice(:interval, :interval_count, :usage_type),
billing_scheme: price[:billing_scheme],
current_period_start: item[:current_period_start] || subscription[:current_period_start],
current_period_end: item[:current_period_end] || subscription[:current_period_end]
}
end
def load_products(subscriptions)
product_ids = subscriptions.flat_map { |subscription| subscription.fetch(:items, {}).fetch(:data, []) }
.map { |item| item.fetch(:price).fetch(:product) }.uniq
product_ids.each_slice(100) do |ids|
products = ::Stripe::Product.list({ ids: ids, limit: 100 }, @options)
products.data.each { |product| @products[product.id] = product.name }
end
rescue ::Stripe::PermissionError
# Existing installations may not yet have granted product_read.
# Stop the whole batch lookup rather than retrying each product.
nil
end
end