From 930783ebea4fb46db32743de3ca951eeaa58c44b Mon Sep 17 00:00:00 2001 From: ricky-julianjatsono Date: Thu, 18 May 2023 13:33:30 +0700 Subject: [PATCH] fix naming and package structure --- event.go | 28 ------ event_test.go | 67 -------------- invoice/event.go | 30 +++++++ invoice/event_test.go | 199 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+), 95 deletions(-) delete mode 100644 event.go delete mode 100644 event_test.go create mode 100644 invoice/event.go create mode 100644 invoice/event_test.go diff --git a/event.go b/event.go deleted file mode 100644 index a8b543c2..00000000 --- a/event.go +++ /dev/null @@ -1,28 +0,0 @@ -package xendit - -type EventInvoicePaid struct { - ID string `json:"id"` - Fees []InvoiceFee `json:"fees"` - Amount int `json:"amount"` - Status string `json:"status"` - Created string `json:"created"` - IsHigh bool `json:"is_high"` - PaidAt string `json:"paid_at"` - Updated string `json:"updated"` - UserID string `json:"user_id"` - Currency string `json:"currency"` - PaymentID string `json:"payment_id"` - ExternalID string `json:"external_id"` - PaidAmount int `json:"paid_amount"` - MerchantName string `json:"merchant_name"` - PaymentMethod string `json:"payment_method"` - PaymentChannel string `json:"payment_channel"` - PaymentDetails InvoicePaymentDetail `json:"payment_details"` - PaymentMethodID string `json:"payment_method_id"` - BankCode string `json:"bank_code,omitempty"` - PayerEmail string `json:"payer_email,omitempty"` - Description string `json:"description,omitempty"` - PaymentDestination string `json:"payment_destination,omitempty"` - SuccessRedirectURL string `json:"success_redirect_url,omitempty"` - FailureRedirectURL string `json:"failure_redirect_url,omitempty"` -} diff --git a/event_test.go b/event_test.go deleted file mode 100644 index d9156631..00000000 --- a/event_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package xendit_test - -import ( - "encoding/json" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/xendit/xendit-go" -) - -func TestEventInvoicePaid_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 := xendit.EventInvoicePaid{ - ID: "12345", - Fees: []xendit.InvoiceFee{{Type: "example fee", Value: 100}}, - Amount: 1000, - Status: "PAID", - Created: "2023-05-14T07:32:42.646Z", - IsHigh: false, - PaidAt: "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 xendit.EventInvoicePaid - err := json.Unmarshal([]byte(testPayload), &actualPayload) - assert.NoError(t, err) - assert.Equal(t, expectedPayload, actualPayload) -} diff --git a/invoice/event.go b/invoice/event.go new file mode 100644 index 00000000..970308f4 --- /dev/null +++ b/invoice/event.go @@ -0,0 +1,30 @@ +package invoice + +import "github.com/xendit/xendit-go" + +type InvoiceCallback struct { + ID string `json:"id"` + Fees []xendit.InvoiceFee `json:"fees"` + Amount int `json:"amount"` + Status string `json:"status"` + Created string `json:"created"` + IsHigh bool `json:"is_high"` + PaidAt string `json:"paid_at"` + Updated string `json:"updated"` + UserID string `json:"user_id"` + Currency string `json:"currency"` + PaymentID string `json:"payment_id"` + ExternalID string `json:"external_id"` + PaidAmount int `json:"paid_amount"` + MerchantName string `json:"merchant_name"` + PaymentMethod string `json:"payment_method"` + PaymentChannel string `json:"payment_channel"` + PaymentDetails *xendit.InvoicePaymentDetail `json:"payment_details"` + PaymentMethodID string `json:"payment_method_id"` + BankCode string `json:"bank_code,omitempty"` + PayerEmail string `json:"payer_email,omitempty"` + Description string `json:"description,omitempty"` + PaymentDestination string `json:"payment_destination,omitempty"` + SuccessRedirectURL string `json:"success_redirect_url,omitempty"` + FailureRedirectURL string `json:"failure_redirect_url,omitempty"` +} diff --git a/invoice/event_test.go b/invoice/event_test.go new file mode 100644 index 00000000..df144731 --- /dev/null +++ b/invoice/event_test.go @@ -0,0 +1,199 @@ +package invoice_test + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/xendit/xendit-go" + "github.com/xendit/xendit-go/invoice" +) + +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: "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": "test@xendit.co", + "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: "2020-01-14T02:32:50.912Z", + PayerEmail: "test@xendit.co", + 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": "test@xendit.co", + "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: "2020-01-14T02:32:50.912Z", + PayerEmail: "test@xendit.co", + 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) +}