Skip to content

Commit

Permalink
Order Tracking Service
Browse files Browse the repository at this point in the history
This commit adds the order tracking service for Bolt API.

This service calls the bolt order_tracking endpoint with updated data for Order Tracking.

Refer: https://help.bolt.com/api-bolt/#tag/Orders/operation/trackOrder
  • Loading branch information
piyushswain committed Jun 14, 2022
1 parent 28bf922 commit 1f4c78d
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/decorators/models/solidus_bolt/shipment_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module SolidusBolt
module ShipmentDecorator
def bolt_items
line_items.map do |line_item|
{
reference: line_item.sku,
name: line_item.name,
description: line_item.description,
total_amount: {
amount: line_item.total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
unit_price: {
amount: line_item.price,
currency: line_item.currency,
currency_symbol: currency_symbol
},
tax_amount: {
amount: line_item.additional_tax_total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
quantity: line_item.quantity,
sku: line_item.sku
}
end
end

private

def currency_symbol
Monetize.from_string(order.total, order.currency).symbol
end

Spree::Shipment.prepend(self)
end
end
47 changes: 47 additions & 0 deletions app/services/solidus_bolt/orders/track_shipment_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module SolidusBolt
module Orders
class TrackShipmentService < SolidusBolt::BaseService
attr_reader :transaction_reference, :shipment

def initialize(transaction_reference:, shipment:)
@transaction_reference = transaction_reference
@shipment = shipment
super
end

def call
track_shipment
end

private

def track_shipment
options = build_options
handle_result(
HTTParty.post(
"#{api_base_url}/#{api_version}/merchant/track_shipment",
options
)
)
end

def build_options
{
body: {
transaction_reference: transaction_reference,
tracking_number: shipment.tracking,
carrier: shipment.shipping_method.name,
items: shipment.bolt_items,
is_non_bolt_order: false
}.to_json,
headers: {
'X-Nonce' => generate_nonce,
'Content-Type' => 'application/json'
}.merge(authentication_header)
}
end
end
end
end
37 changes: 37 additions & 0 deletions spec/decorators/models/solidus_bolt/shipment_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

RSpec.describe SolidusBolt::ShipmentDecorator do
let(:payment_method) { create(:bolt_payment_method) }
let(:payment_source) { create(:bolt_payment_source) }
let(:payment) do
create(
:payment,
state: 'completed',
source_id: payment_source.id,
source_type: SolidusBolt::PaymentSource,
payment_method_id: payment_method.id,
response_code: 'Doctor Strange'
)
end
let(:order) { payment.order }

describe '#bolt_items' do
it 'returns an array' do
create(:shipment, order: order, id: rand(1..10), tracking: 'MockBolt1678')
order.shipments.reload
shipment = order.shipments.last
items = shipment.bolt_items

expect(items).to be_a(Array)
end

it 'lists all line_items' do
create(:shipment, order: order, id: rand(1..10), tracking: 'MockBolt1678')
order.shipments.reload
shipment = order.shipments.last
items = shipment.bolt_items

expect(items.count).to eq(order.line_items.count)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f4c78d

Please sign in to comment.