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 invoice paid event callback struct #124

Merged
merged 3 commits into from
May 19, 2023
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
44 changes: 44 additions & 0 deletions invoice/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package invoice

import "github.com/xendit/xendit-go"

type InvoiceCallback struct {
ID string `json:"id"`
UserID string `json:"user_id"`
ExternalID string `json:"external_id"`
IsHigh bool `json:"is_high"`
Status string `json:"status"`
MerchantName string `json:"merchant_name"`
Amount float64 `json:"amount"`
Created string `json:"created"`
Updated string `json:"updated"`
PayerEmail string `json:"payer_email,omitempty"`
Description string `json:"description,omitempty"`
PaymentID string `json:"payment_id,omitempty"`
PaidAmount float64 `json:"paid_amount,omitempty"`
PaymentMethod string `json:"payment_method,omitempty"`
BankCode string `json:"bank_code,omitempty"`
EwalletType string `json:"ewallet_type,omitempty"`
CallbackAuthenticationToken string `json:"callback_authentication_token,omitempty"`
CallbackURL string `json:"callback_url,omitempty"`
BusinessID string `json:"business_id,omitempty"`
BusinessName string `json:"business_name,omitempty"`
OnDemandLink string `json:"on_demand_link,omitempty"`
RecurringPaymentID string `json:"recurring_payment_id,omitempty"`
CreditCardChargeID string `json:"credit_card_charge_id,omitempty"`
Currency string `json:"currency,omitempty"`
InitialCurrency string `json:"initial_currency,omitempty"`
InitialAmount float64 `json:"initial_amount,omitempty"`
PaidAt *string `json:"paid_at,omitempty"`
MidLabel string `json:"mid_label,omitempty"`
PaymentChannel string `json:"payment_channel,omitempty"`
PaymentDestination string `json:"payment_destination,omitempty"`
SuccessRedirectURL string `json:"success_redirect_url,omitempty"`
FailureRedirectURL string `json:"failure_redirect_url,omitempty"`
CreditCardToken string `json:"credit_card_token,omitempty"`
PaymentMethodID string `json:"payment_method_id,omitempty"`
ShouldAuthenticateCreditCard bool `json:"should_authenticate_credit_card,omitempty"`
PaymentDetails *xendit.InvoicePaymentDetail `json:"payment_details,omitempty"`
Fees *[]xendit.InvoiceFee `json:"fees,omitempty"`
Items *[]xendit.InvoiceItem `json:"items,omitempty"`
}
203 changes: 203 additions & 0 deletions invoice/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package invoice_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/xendit/xendit-go"
"github.com/xendit/xendit-go/invoice"
)

func toPointerString(v string) *string {
return &v
}

func TestInvoiceCallback_EWallet(t *testing.T) {
testPayload := `{
"id": "12345",
"fees": [
{
"type": "example fee",
"value": 100
}
],
"amount": 1000,
"status": "PAID",
"created": "2023-05-14T07:32:42.646Z",
"is_high": false,
"paid_at": "2023-05-14T07:35:14.000Z",
"updated": "2023-05-14T07:35:15.810Z",
"user_id": "example",
"currency": "IDR",
"payment_id": "qrpy_example",
"external_id": "example",
"paid_amount": 1000,
"merchant_name": "Example Merchant",
"payment_method": "QR_CODE",
"payment_channel": "QRIS",
"payment_details": {
"source": "Ovo",
"receipt_id": "example"
},
"payment_method_id": "pm-example-123"
}`
expectedPayload := invoice.InvoiceCallback{
ID: "12345",
Fees: &[]xendit.InvoiceFee{{Type: "example fee", Value: 100}},
Amount: 1000,
Status: "PAID",
Created: "2023-05-14T07:32:42.646Z",
IsHigh: false,
PaidAt: toPointerString("2023-05-14T07:35:14.000Z"),
Updated: "2023-05-14T07:35:15.810Z",
UserID: "example",
Currency: "IDR",
PaymentID: "qrpy_example",
ExternalID: "example",
PaidAmount: 1000,
MerchantName: "Example Merchant",
PaymentMethod: "QR_CODE",
PaymentChannel: "QRIS",
PaymentDetails: &xendit.InvoicePaymentDetail{
Source: "Ovo",
ReceiptID: "example",
},
PaymentMethodID: "pm-example-123",
}
var actualPayload invoice.InvoiceCallback
err := json.Unmarshal([]byte(testPayload), &actualPayload)
assert.NoError(t, err)
assert.Equal(t, expectedPayload, actualPayload)
}

func TestInvoiceCallback_Bank(t *testing.T) {
testPayload := `{
"id": "593f4ed1c3d3bb7f39733d83",
"external_id": "testing-invoice",
"user_id": "5848fdf860053555135587e7",
"is_high": false,
"payment_method": "BANK_TRANSFER",
"status": "PAID",
"merchant_name": "Xendit",
"amount": 2000000,
"paid_amount": 2000000,
"bank_code": "TESTBANK",
"paid_at": "2020-01-14T02:32:50.912Z",
"payer_email": "[email protected]",
"description": "Invoice callback test",
"created": "2020-01-13T02:32:49.827Z",
"updated": "2020-01-13T02:32:50.912Z",
"currency": "IDR",
"payment_channel": "TEST",
"payment_destination": "8458478548758748"
}`
expectedPayload := invoice.InvoiceCallback{
ID: "593f4ed1c3d3bb7f39733d83",
ExternalID: "testing-invoice",
UserID: "5848fdf860053555135587e7",
IsHigh: false,
PaymentMethod: "BANK_TRANSFER",
Status: "PAID",
MerchantName: "Xendit",
Amount: 2000000,
PaidAmount: 2000000,
BankCode: "TESTBANK",
PaidAt: toPointerString("2020-01-14T02:32:50.912Z"),
PayerEmail: "[email protected]",
Description: "Invoice callback test",
Created: "2020-01-13T02:32:49.827Z",
Updated: "2020-01-13T02:32:50.912Z",
Currency: "IDR",
PaymentChannel: "TEST",
PaymentDestination: "8458478548758748",
}
var actualPayload invoice.InvoiceCallback
err := json.Unmarshal([]byte(testPayload), &actualPayload)
assert.NoError(t, err)
assert.Equal(t, expectedPayload, actualPayload)
}

func TestInvoiceCallback_RetailOutlet(t *testing.T) {
testPayload := `{
"id": "593f4ed1c3d3bb7f39733d83",
"external_id": "testing-invoice",
"user_id": "5848fdf860053555135587e7",
"is_high": false,
"payment_method": "RETAIL_OUTLET",
"status": "PAID",
"merchant_name": "Xendit",
"amount": 2000000,
"paid_amount": 2000000,
"paid_at": "2020-01-14T02:32:50.912Z",
"payer_email": "[email protected]",
"description": "Invoice callback test",
"created": "2020-01-13T02:32:49.827Z",
"updated": "2020-01-13T02:32:50.912Z",
"currency": "IDR",
"payment_channel": "TESTMART",
"payment_destination": "TEST815"
}`
expectedPayload := invoice.InvoiceCallback{
ID: "593f4ed1c3d3bb7f39733d83",
ExternalID: "testing-invoice",
UserID: "5848fdf860053555135587e7",
IsHigh: false,
PaymentMethod: "RETAIL_OUTLET",
Status: "PAID",
MerchantName: "Xendit",
Amount: 2000000,
PaidAmount: 2000000,
PaidAt: toPointerString("2020-01-14T02:32:50.912Z"),
PayerEmail: "[email protected]",
Description: "Invoice callback test",
Created: "2020-01-13T02:32:49.827Z",
Updated: "2020-01-13T02:32:50.912Z",
Currency: "IDR",
PaymentChannel: "TESTMART",
PaymentDestination: "TEST815",
}

var actualPayload invoice.InvoiceCallback
err := json.Unmarshal([]byte(testPayload), &actualPayload)
assert.NoError(t, err)
assert.Equal(t, expectedPayload, actualPayload)
}

func TestInvoiceCallback_Expired(t *testing.T) {
testPayload := `{
"id": "621887f17d9cdaa199d6e787",
"user_id": "61d3c21692594a88b0dad56b",
"external_id": "yumin-invoice-1645774832",
"is_high": false,
"status": "EXPIRED",
"merchant_name": "fintech",
"amount": 1000,
"created": "2022-02-25T07:40:33.922Z",
"updated": "2022-02-25T07:42:43.872Z",
"description": "Invoice Demo #123",
"currency": "IDR",
"success_redirect_url": "https://www.example.com",
"failure_redirect_url": "https://www.example.com"
}`
expectedPayload := invoice.InvoiceCallback{
ID: "621887f17d9cdaa199d6e787",
UserID: "61d3c21692594a88b0dad56b",
ExternalID: "yumin-invoice-1645774832",
IsHigh: false,
Status: "EXPIRED",
MerchantName: "fintech",
Amount: 1000,
Created: "2022-02-25T07:40:33.922Z",
Updated: "2022-02-25T07:42:43.872Z",
Description: "Invoice Demo #123",
Currency: "IDR",
SuccessRedirectURL: "https://www.example.com",
FailureRedirectURL: "https://www.example.com",
}

var actualPayload invoice.InvoiceCallback
err := json.Unmarshal([]byte(testPayload), &actualPayload)
assert.NoError(t, err)
assert.Equal(t, expectedPayload, actualPayload)
}