Module: Pay::Receipts
- Defined in:
- lib/pay/receipts.rb
Instance Method Summary collapse
- #discount_description(total_discount_amount) ⇒ Object
- #invoice ⇒ Object
- #invoice_details ⇒ Object
- #invoice_filename ⇒ Object
- #invoice_number ⇒ Object
- #invoice_pdf(**options) ⇒ Object
- #pdf_line_items ⇒ Object
- #pdf_product_name ⇒ Object
- #receipt ⇒ Object
- #receipt_details ⇒ Object
- #receipt_filename ⇒ Object
-
#receipt_line_items ⇒ Object
def tax_description(tax_rate) percent = "#strip_insignificant_zeros: true)%" percent += " inclusive" if tax_rate.inclusive "#tax_ratetax_rate.display_name - #tax_ratetax_rate.jurisdiction (#percent)" end.
- #receipt_number ⇒ Object
- #receipt_pdf(**options) ⇒ Object
Instance Method Details
#discount_description(total_discount_amount) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pay/receipts.rb', line 72 def discount_description(total_discount_amount) coupon = total_discount_amount.discount.try(:source).try(:coupon) || total_discount_amount.discount.try(:coupon) name = coupon.name if (percent = coupon.percent_off) I18n.t("pay.line_items.percent_discount", name: name, percent: ActiveSupport::NumberHelper.number_to_rounded(percent, strip_insignificant_zeros: true)) else I18n.t("pay.line_items.amount_discount", name: name, amount: Pay::Currency.format(coupon.amount_off, currency: coupon.currency)) end end |
#invoice ⇒ Object
135 136 137 |
# File 'lib/pay/receipts.rb', line 135 def invoice invoice_pdf.render end |
#invoice_details ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/pay/receipts.rb', line 139 def invoice_details [ [I18n.t("pay.invoice.number"), invoice_number], [I18n.t("pay.invoice.date"), I18n.l(created_at, format: :long)], [I18n.t("pay.invoice.payment_method"), charged_to] ] end |
#invoice_filename ⇒ Object
131 132 133 |
# File 'lib/pay/receipts.rb', line 131 def invoice_filename "invoice-#{created_at.strftime("%Y-%m-%d")}.pdf" end |
#invoice_number ⇒ Object
167 168 169 |
# File 'lib/pay/receipts.rb', line 167 def invoice_number id end |
#invoice_pdf(**options) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pay/receipts.rb', line 147 def invoice_pdf(**) defaults = { details: invoice_details, recipient: [ customer.customer_name, customer.email, customer.owner.try(:extra_billing_info) ], company: { name: Pay.business_name, address: Pay.business_address, email: Pay.support_email&.address, logo: Pay.business_logo }, line_items: pdf_line_items } ::Receipts::Invoice.new(defaults.deep_merge()) end |
#pdf_line_items ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pay/receipts.rb', line 23 def pdf_line_items items = [ [ "<b>#{I18n.t("pay.line_items.description")}</b>", "<b>#{I18n.t("pay.line_items.quantity")}</b>", "<b>#{I18n.t("pay.line_items.unit_price")}</b>", "<b>#{I18n.t("pay.line_items.amount")}</b>" ] ] if try(:stripe_invoice) stripe_invoice.lines.auto_paging_each do |line| items << [line.description, line.quantity, Pay::Currency.format(line.pricing.unit_amount_decimal, currency: line.currency), Pay::Currency.format(line.amount, currency: line.currency)] line.discounts.each do |discount_id| discount = stripe_invoice.total_discount_amounts.find { |d| d.discount.id == discount_id } items << [discount_description(discount), nil, nil, Pay::Currency.format(-discount.amount, currency: currency)] end end else items << [pdf_product_name, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)] end # If no subtotal, we will display the total items << [nil, nil, I18n.t("pay.line_items.subtotal"), Pay::Currency.format(try(:stripe_invoice)&.subtotal || amount, currency: currency)] # Discounts on the invoice try(:stripe_invoice)&.discounts&.each do |discount_id| discount = stripe_invoice.total_discount_amounts.find { |d| d.discount.id == discount_id } items << [nil, nil, discount_description(discount), Pay::Currency.format(-discount.amount, currency: currency)] end # Total excluding tax if try(:stripe_invoice) items << [nil, nil, I18n.t("pay.line_items.total"), Pay::Currency.format(stripe_invoice.total_excluding_tax, currency: currency)] end # Tax rates try(:stripe_invoice)&.total_taxes&.each do |tax| next if tax.amount.zero? # tax_rate = ::Stripe::TaxRate.retrieve(tax.tax_rate_details.tax_rate) items << [nil, nil, I18n.t("pay.line_items.tax"), Pay::Currency.format(tax.amount, currency: currency)] end # Total items << [nil, nil, I18n.t("pay.line_items.total"), Pay::Currency.format(amount, currency: currency)] items end |
#pdf_product_name ⇒ Object
19 20 21 |
# File 'lib/pay/receipts.rb', line 19 def pdf_product_name Pay.application_name end |
#receipt ⇒ Object
7 8 9 |
# File 'lib/pay/receipts.rb', line 7 def receipt receipt_pdf.render end |
#receipt_details ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/pay/receipts.rb', line 11 def receipt_details [ [I18n.t("pay.receipt.number"), receipt_number], [I18n.t("pay.receipt.date"), I18n.l(created_at, format: :long)], [I18n.t("pay.receipt.payment_method"), charged_to] ] end |
#receipt_filename ⇒ Object
3 4 5 |
# File 'lib/pay/receipts.rb', line 3 def receipt_filename "receipt-#{created_at.strftime("%Y-%m-%d")}.pdf" end |
#receipt_line_items ⇒ Object
def tax_description(tax_rate) percent = "#strip_insignificant_zeros: true)%" percent += " inclusive" if tax_rate.inclusive "#Pay::Receipts.tax_ratetax_rate.display_name - #Pay::Receipts.tax_ratetax_rate.jurisdiction (#percent)" end
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pay/receipts.rb', line 89 def receipt_line_items line_items = pdf_line_items # Include total paid line_items << [nil, nil, I18n.t("pay.receipt.amount_paid"), Pay::Currency.format(amount, currency: currency)] if refunded? # If we have a list of individual refunds, add each entry # if refunds&.any? # refunds.each do |refund| # next unless refund["status"] == "succeeded" # refunded_at = Time.at(refund["created"]).to_date # line_items << [nil, nil, I18n.t("pay.receipt.refunded_on", date: I18n.l(refunded_at, format: :long)), Pay::Currency.format(refund["amount"], currency: refund["currency"])] # end # else line_items << [nil, nil, I18n.t("pay.receipt.refunded"), Pay::Currency.format(amount_refunded, currency: currency)] # end end line_items end |
#receipt_number ⇒ Object
171 172 173 |
# File 'lib/pay/receipts.rb', line 171 def receipt_number invoice_number end |
#receipt_pdf(**options) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pay/receipts.rb', line 111 def receipt_pdf(**) defaults = { details: receipt_details, recipient: [ customer.customer_name, customer.email, customer.owner.try(:extra_billing_info) ], company: { name: Pay.business_name, address: Pay.business_address, email: Pay.support_email&.address, logo: Pay.business_logo }, line_items: receipt_line_items } ::Receipts::Receipt.new(defaults.deep_merge()) end |