Skip to content

Commit 9eff60a

Browse files
authored
Add line items for paypal integration (spree-contrib#265)
1 parent 4e583fb commit 9eff60a

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

  • app/models/spree/gateway/braintree_vzero_base

app/models/spree/gateway/braintree_vzero_base/utils.rb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def get_customer
3939
def order_data(identifier, amount)
4040
identifier.merge(
4141
amount: amount,
42-
order_id: order.number
42+
order_id: order.number,
43+
line_items: collect_line_items,
44+
shipping_amount: order_shipping
4345
)
4446
end
4547

@@ -104,6 +106,49 @@ def map_payment_status(braintree_status)
104106
'failed'
105107
end
106108
end
109+
110+
private
111+
112+
PAYPAL_MAX_LINEITEMS = 249
113+
114+
# Because of strange PayPal behaviour with accepting discounts
115+
# the only solution to add needed discount (if it exist) to
116+
# create a virtual Line Item with discount amount and with 'credit'
117+
# and name it as 'discount' to be reflected in PayPal email to customer
118+
#
119+
def collect_line_items # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
120+
items =
121+
@order
122+
.line_items
123+
.reject { |li| li.price.zero? || li.quantity.zero? }
124+
.map do |li|
125+
{
126+
name: li.name.truncate(127, omission: ''),
127+
kind: 'debit',
128+
quantity: li.quantity.to_s,
129+
unit_amount: li.price.to_s,
130+
unit_of_measure: 'unit',
131+
product_code: li.sku,
132+
total_amount: (li.price * li.quantity).to_s,
133+
tax_amount: li.additional_tax_total.to_s
134+
}
135+
end.
136+
take(PAYPAL_MAX_LINEITEMS - 1)
137+
total = @order.adjustment_total.abs
138+
return items if total.zero?
139+
140+
items.append({
141+
name: 'discount',
142+
kind: 'credit',
143+
quantity: '1',
144+
unit_amount: total.to_s,
145+
total_amount: total.to_s
146+
})
147+
end
148+
149+
def order_shipping
150+
@order.shipment_total.to_s
151+
end
107152
end
108153
end
109154
end

0 commit comments

Comments
 (0)