Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(netsuite): Fix netsuite payment payload #2790

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/payments/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CreateJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 5
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100

def perform(payment:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def call
raise e
rescue Integrations::Aggregator::BasePayload::Failure => e
deliver_error_webhook(customer:, code: e.code, message: e.code.humanize)
result
end

def call_async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(integration:, payment:)
def body
[
{
'invoice_id' => integration_invoice&.external_id,
'invoice_id' => integration_invoice.external_id,
'account_code' => account_item&.external_account_code,
'date' => payment.created_at.utc.iso8601,
'amount_cents' => payment.amount_cents
Expand All @@ -31,7 +31,12 @@ def invoice
end

def integration_invoice
invoice.integration_resources.where(resource_type: 'invoice', syncable_type: 'Invoice').first
integration_resource =
invoice.integration_resources.where(resource_type: 'invoice', syncable_type: 'Invoice').first

raise Integrations::Aggregator::BasePayload::Failure.new(nil, code: 'invoice_missing') unless integration_resource

integration_resource
end

def integration_customer
Expand Down
22 changes: 11 additions & 11 deletions app/services/integrations/aggregator/payments/payloads/netsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ module Payloads
class Netsuite < BasePayload
def body
{
'type' => 'customerpayment',
'isDynamic' => true,
'columns' => {
'customer' => integration_customer.external_customer_id
'customer' => integration_customer.external_customer_id,
'payment' => amount(payment.amount_cents, resource: invoice)
},
'options' => {
'ignoreMandatoryFields' => false
},
'type' => 'customerpayment',
'lines' => [
{
'sublistId' => 'apply',
'lineItems' => [
{
# If the invoice is not synced yet, lets raise an error and retry. (doc: nil is an invalid request)
'doc' => integration_invoice&.external_id,
'amount' => amount(payment.amount_cents, resource: invoice),
'apply' => true,
'amount' => amount(payment.amount_cents, resource: invoice)
'doc' => integration_invoice.external_id
}
]
],
'sublistId' => 'apply'
}
],
'options' => {
'ignoreMandatoryFields' => false
}
]
}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@
it 'sends error webhook' do
expect { service_call }.to have_enqueued_job(SendWebhookJob)
end

it 'returns result' do
expect(service_call).to be_a(BaseService::Result)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,27 @@

let(:params) do
{
'type' => 'customerpayment',
'isDynamic' => true,
'columns' => {
'customer' => integration_customer.external_customer_id
'customer' => integration_customer.external_customer_id,
'payment' => payment.amount_cents.div(100).to_f
},
'options' => {
'ignoreMandatoryFields' => false
},
'type' => 'customerpayment',
'lines' => [
{
'sublistId' => 'apply',
'lineItems' => [
{
'doc' => integration_invoice.external_id,
'amount' => payment.amount_cents.div(100).to_f,
'apply' => true,
'amount' => payment.amount_cents.div(100).to_f
'doc' => integration_invoice.external_id
}
]
],
'sublistId' => 'apply'
}
],
'options' => {
'ignoreMandatoryFields' => false
}
]
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
end

describe '#body' do
let(:integration_invoice) { create(:integration_resource, syncable: invoice, integration:) }

before { integration_invoice }

it 'returns correct body' do
expect(payload.body).to eq(
[
{
'invoice_id' => nil,
'invoice_id' => integration_invoice.external_id,
'account_code' => nil,
'date' => payment.created_at.utc.iso8601,
'amount_cents' => payment.amount_cents
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Integrations::Aggregator::Payments::Payloads::Netsuite do
let(:payload) { described_class.new(integration:, payment:) }
let(:integration_customer) { create(:netsuite_customer, integration:, customer:) }
let(:integration) { create(:netsuite_integration, organization:) }
let(:customer) { create(:customer, organization:) }
let(:organization) { create(:organization) }
let(:payment) { create(:payment, payable: invoice, amount_cents: 100) }
let(:integration_invoice) { create(:integration_resource, syncable: invoice) }

let(:invoice) do
create(
:invoice,
customer:,
organization:,
coupons_amount_cents: 2000,
prepaid_credit_amount_cents: 4000,
credit_notes_amount_cents: 6000,
taxes_amount_cents: 200,
issuing_date: DateTime.new(2024, 7, 8)
)
end

let(:body) do
{
'isDynamic' => true,
'columns' => {
'customer' => integration_customer.external_customer_id,
'payment' => payment.amount_cents.div(100).to_f
},
'options' => {
'ignoreMandatoryFields' => false
},
'type' => 'customerpayment',
'lines' => [
{
'lineItems' => [
{
'amount' => payment.amount_cents.div(100).to_f,
'apply' => true,
'doc' => integration_invoice.external_id
}
],
'sublistId' => 'apply'
}
]
}
end

before do
integration_customer
integration_invoice
end

describe '#body' do
subject(:body_call) { payload.body }

it 'returns payload body' do
expect(subject).to eq(body)
end
end
end