Class: Workarea::Paypal::CreateOrder

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/paypal/create_order.rb

Instance Method Summary collapse

Constructor Details

#initialize(checkout) ⇒ CreateOrder

Returns a new instance of CreateOrder.



6
7
8
# File 'app/services/workarea/paypal/create_order.rb', line 6

def initialize(checkout)
  @checkout = checkout
end

Instance Method Details

#amountObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/workarea/paypal/create_order.rb', line 67

def amount
  {
    currency_code: currency_code,
    value: (order.total_price - other_tenders_total).to_s,
    breakdown: {
      item_total: {
        currency_code: currency_code,
        value: order.subtotal_price.to_s
      },
      shipping: {
        currency_code: currency_code,
        value: order.shipping_total.to_s
      },
      tax_total: {
        currency_code: currency_code,
        value: order.tax_total.to_s
      },
      discount: {
        currency_code: currency_code,
        value: (discount_total.abs + other_tenders_total).to_s
      },
      shipping_discount: {
        currency_code: currency_code,
        value: shipping_discount_total.abs.to_s
      }
    }
  }
end

#itemsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/services/workarea/paypal/create_order.rb', line 96

def items
  order.items.map do |item|
    view_model = Storefront::OrderItemViewModel.wrap(item)

    {
      name: view_model.product_name,
      description: view_model.details.values.join(' '),
      sku: item.sku,
      unit_amount: {
        currency_code: currency_code,
        value: item.current_unit_price.to_s
      },
      quantity: item.quantity,
      category: item.fulfilled_by?(:shipping) ? 'PHYSICAL_GOODS' : 'DIGITAL_GOODS'
    }
  end
end

#payerObject



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
# File 'app/services/workarea/paypal/create_order.rb', line 35

def payer
  return {} unless order.email.present?

  info = { email_address: order.email }.compact
  return info unless payment.address&.persisted?

  info[:name] = {
    given_name: payment.address.first_name,
    surname: payment.address.last_name,
  }

  info[:address] = {
    address_line_1: payment.address.street,
    address_line_2: payment.address.street_2,
    admin_area_2: payment.address.city,
    admin_area_1: payment.address.region,
    postal_code: payment.address.postal_code,
    country_code: payment.address.country.alpha2
  }

  return info unless payment.address.phone_number.present?

  info['phone'] = {
    phone_type: 'HOME',
    phone_number: {
      national_number: payment.address.phone_number
    }
  }

  info
end

#performObject



10
11
12
13
# File 'app/services/workarea/paypal/create_order.rb', line 10

def perform
  Pricing.perform(order, shippings)
  Paypal.gateway.create_order(body: request_body)
end

#request_bodyObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/workarea/paypal/create_order.rb', line 15

def request_body
  {
    intent: 'CAPTURE',
    application_context: {
      brand_name: Workarea.config.site_name,
      locale: I18n.locale,
      landing_page: 'BILLING',
      shipping_preference: shipping_preference,
      user_action: 'CONTINUE'
    },
    payer: payer,
    purchase_units: [{
      custom_id: order.id,
      amount: amount,
      items: items,
      shipping: shipping_info
    }]
  }
end

#send_shipping_info?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
# File 'app/services/workarea/paypal/create_order.rb', line 136

def send_shipping_info?
  order.requires_shipping? &&
    shippings.one? &&
    shipping.shippable?
end

#shipping_infoObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/services/workarea/paypal/create_order.rb', line 114

def shipping_info
  return {} unless send_shipping_info?

  {
    method: shipping.shipping_service&.name,
    name: {
      full_name: [
        shipping.address.first_name,
        shipping.address.last_name
      ].compact.join(' ')
    },
    address: {
      address_line_1: shipping.address.street,
      address_line_2: shipping.address.street_2,
      admin_area_2: shipping.address.city,
      admin_area_1: shipping.address.region,
      postal_code: shipping.address.postal_code,
      country_code: shipping.address.country.alpha2
    }
  }
end