-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dunning): Add skeleton for creating a payment request (#2426)
## Context We want to be able to manually request payment of the overdue balance and send emails for reminders. https://getlago.canny.io/feature-requests/p/send-reminders-for-overdue-invoices ## Description The goal of this PR is to add the graphql mutation and the API endpoint for creating a payment request.
- Loading branch information
Showing
13 changed files
with
333 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,3 +86,4 @@ organization: | |
delete: | ||
payment_requests: | ||
view: true | ||
create: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,4 @@ organization: | |
delete: false | ||
payment_requests: | ||
view: true | ||
create: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,4 @@ organization: | |
delete: false | ||
payment_requests: | ||
view: true | ||
create: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module PaymentRequests | ||
class Create < BaseMutation | ||
include AuthenticableApiUser | ||
include RequiredOrganization | ||
|
||
REQUIRED_PERMISSION = "payment_requests:create" | ||
|
||
graphql_name "CreatePaymentRequest" | ||
description "Creates a payment request" | ||
|
||
input_object_class Types::PaymentRequests::CreateInput | ||
type Types::PaymentRequests::Object | ||
|
||
def resolve(**args) | ||
result = ::PaymentRequests::CreateService.call(organization: current_organization, params: args) | ||
result.success? ? result.payment_request : result_error(result) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module PaymentRequests | ||
class CreateInput < Types::BaseInputObject | ||
graphql_name "PaymentRequestCreateInput" | ||
|
||
argument :external_customer_id, String, required: true | ||
|
||
argument :email, String, required: false | ||
argument :lago_invoice_ids, [String], required: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentRequests | ||
class CreateService < BaseService | ||
def initialize(organization:, params:) | ||
@organization = organization | ||
@params = params | ||
|
||
super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: "customer") unless customer | ||
|
||
ActiveRecord::Base.transaction do | ||
# TODO: Create payable group for the overdue invoices | ||
|
||
# TODO: Create payment request for the payable group | ||
|
||
# TODO: Send payment_request.created webhook | ||
|
||
# TODO: When payment provider is set: Create payment intent for the overdue invoices | ||
# TODO: When payment provider is not set: Send email to the customer | ||
|
||
# result.payment_request = payment_request | ||
end | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :params | ||
|
||
def customer | ||
@customer ||= organization.customers.find_by(external_id: params[:external_customer_id]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Mutations::PaymentRequests::Create, type: :graphql do | ||
let(:required_permission) { "payment_requests:create" } | ||
let(:membership) { create(:membership) } | ||
let(:organization) { membership.organization } | ||
let(:customer) { create(:customer, organization:) } | ||
let(:invoice1) { create(:invoice, organization:) } | ||
let(:invoice2) { create(:invoice, organization:) } | ||
|
||
let(:input) do | ||
{ | ||
email: "john.doe@example.com", | ||
externalCustomerId: customer.external_id, | ||
lagoInvoiceIds: [invoice1.id, invoice2.id] | ||
} | ||
end | ||
|
||
let(:mutation) do | ||
<<-GQL | ||
mutation($input: PaymentRequestCreateInput!) { | ||
createPaymentRequest(input: $input) { | ||
id | ||
customer { id } | ||
invoices { id } | ||
} | ||
} | ||
GQL | ||
end | ||
|
||
it "creates a payment request" do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: membership.organization, | ||
permissions: required_permission, | ||
query: mutation, | ||
variables: {input:} | ||
) | ||
|
||
expect(result["data"]).to include( | ||
"createPaymentRequest" => nil | ||
) | ||
end | ||
end |
Oops, something went wrong.