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

Add FetchIncomingPaymentsByInvoice to the client interface #72

Merged
merged 1 commit into from
Feb 23, 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
8 changes: 8 additions & 0 deletions examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ func main() {
fmt.Printf("Invoice paid with a simulated payment %v\n", testPayment.Id)
fmt.Println()

// Fetch the payment by invoice id:
paymentResponse, err := client.FetchIncomingPaymentsByInvoice(invoice.Id, nil)
if err != nil {
fmt.Printf("fetch incoming payments by invoice failed: %v", err)
return
}
fmt.Printf("We found %v incoming payments for the invoice.\n", len(paymentResponse.Payments))

// Create and cancel an invoice
fmt.Println("Creating an invoice...")
invoiceToCancel, err := client.CreateInvoice(nodeId, 100000, nil, nil, nil)
Expand Down
18 changes: 18 additions & 0 deletions scripts/incoming_payments_for_invoice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package scripts

import "github.com/lightsparkdev/go-sdk/objects"

const INCOMING_PAYMENTS_FOR_INVOICE_QUERY = `
query IncomingPaymentsForInvoice(
$invoice_id: ID!
$statuses: [TransactionStatus!]
) {
incoming_payments_for_invoice(input: {
invoice_id: $invoice_id
statuses: $statuses
}) {
...IncomingPaymentsForInvoiceQueryOutputFragment
}
}

` + objects.IncomingPaymentsForInvoiceQueryOutputFragment
25 changes: 25 additions & 0 deletions services/lightspark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,31 @@ func (client *LightsparkClient) FetchOutgoingPaymentsByInvoice(encodedInvoice st
return &payments, nil
}

func (client *LightsparkClient) FetchIncomingPaymentsByInvoice(invoiceId string,
statuses *[]objects.TransactionStatus) (*objects.IncomingPaymentsForInvoiceQueryOutput, error) {
variables := map[string]interface{}{
"invoice_id": invoiceId,
"statuses": statuses,
}

response, err := client.Requester.ExecuteGraphql(scripts.INCOMING_PAYMENTS_FOR_INVOICE_QUERY, variables, nil)
if err != nil {
return nil, err
}

output := response["incoming_payments_for_invoice"].(map[string]interface{})
var payments objects.IncomingPaymentsForInvoiceQueryOutput
paymentsJson, err := json.Marshal(output)
if err != nil {
return nil, errors.New("error parsing payments")
}
err = json.Unmarshal(paymentsJson, &payments)
if err != nil {
return nil, err
}
return &payments, nil
}

func hashPhoneNumber(e614PhoneNumber string) (*string, error) {
e164PhoneRegex, err := regexp.Compile(`^\+?[1-9]\d{1,14}$`)
if err != nil {
Expand Down
Loading