Class: SpreePaypalCheckout::OrderPresenter

Inherits:
Object
  • Object
show all
Includes:
PaypalServerSdk
Defined in:
app/presenters/spree_paypal_checkout/order_presenter.rb

Constant Summary collapse

PAYPAL_ITEM_NAME_MAX_LENGTH =
127

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderPresenter

Returns a new instance of OrderPresenter.



9
10
11
# File 'app/presenters/spree_paypal_checkout/order_presenter.rb', line 9

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



13
14
15
# File 'app/presenters/spree_paypal_checkout/order_presenter.rb', line 13

def order
  @order
end

Instance Method Details

#to_jsonObject



15
16
17
18
19
20
21
22
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
# File 'app/presenters/spree_paypal_checkout/order_presenter.rb', line 15

def to_json
  {
    'body' => OrderRequest.new(
      intent: CheckoutPaymentIntent::CAPTURE,
      purchase_units: [
        PurchaseUnitRequest.new(
          amount: AmountWithBreakdown.new(
            currency_code: order.currency.upcase,
            value: order.total.to_s,
            breakdown: AmountBreakdown.new(
              item_total: Money.new(
                currency_code: order.currency.upcase,
                value: order.item_total.to_s
              ),
              shipping: Money.new(
                currency_code: order.currency.upcase,
                value: order.ship_total.to_s
              ),
              tax_total: Money.new(
                currency_code: order.currency.upcase,
                value: order.tax_total.to_s
              ),
              discount: Money.new(
                currency_code: order.currency.upcase,
                value: (order.promo_total || 0).abs.to_s
              )
            )
          ),
          items: order.line_items.map do |line_item|
            Item.new(
              name: line_item.name.to_s[0...PAYPAL_ITEM_NAME_MAX_LENGTH],
              unit_amount: Money.new(
                currency_code: order.currency.upcase,
                value: line_item.price.to_s
              ),
              quantity: line_item.quantity.to_s,
              sku: line_item.sku,
              category: line_item.variant.digital? ? ItemCategory::DIGITAL_GOODS : ItemCategory::PHYSICAL_GOODS
            )
          end
        )
      ]
    )
  }
end