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 10, 2022
1 parent 28bf922 commit 15c6f65
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/decorators/models/solidus_bolt/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,43 @@ def bolt_user_identity
}
end

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 cents(float)
(float * 100).to_i
end

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

Spree::Order.prepend(self)
end
end
48 changes: 48 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,48 @@
# 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
order = shipment.order
{
body: {
transaction_reference: transaction_reference,
tracking_number: shipment.tracking,
carrier: shipment.shipping_method.name,
items: order.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
12 changes: 12 additions & 0 deletions spec/decorators/models/solidus_bolt/order_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@
expect(order.bolt_user_identity).to eq(result)
end
end

describe '#bolt_items' do
subject(:items) { order.bolt_items }

it 'returns an array' do
expect(items).to be_a(Array)
end

it 'lists all line_items' do
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.

39 changes: 39 additions & 0 deletions spec/services/solidus_bolt/orders/track_shipment_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidusBolt::Orders::TrackShipmentService, :vcr, :bolt_configuration do
subject(:api) do
described_class.new(transaction_reference: reference, shipment: shipment)
end

let(:transaction) do
SolidusBolt::Transactions::AuthorizeService.call(
order: order, credit_card: credit_card_payload, create_bolt_account: false, payment_method: payment_method
)
end
let(:credit_card_payload) do
tokenize_credit_card(credit_card_number: '4111111111111004', cvv: '111').merge(
expiration: (Time.current + 1.year).strftime('%Y-%m'),
token_type: 'bolt'
)
end
let(:order) { create(:order_with_line_items) }
let(:reference) { transaction['transaction']['reference'] }
let(:payment_method) { create(:bolt_payment_method, preference_source: 'bolt_config_credentials') }
let(:shipping_method) { create(:shipping_method, name: 'MockBoltShipping') }
let(:shipment) { order.shipments.last }

describe '#call', vcr: true do
before do
shipment.select_shipping_method(shipping_method)
shipment.update(tracking: "MockBolt#{rand(1000..9999)}")
end

it 'makes the API call' do
response = api.call

expect(response).to be_nil
end
end
end

0 comments on commit 15c6f65

Please sign in to comment.