From 704c2208b822c74d7d86fa458567aa39276e91e5 Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Thu, 21 Sep 2023 15:53:58 +0700 Subject: [PATCH 1/5] Support partial capture --- authorization_types.go | 10 ++++++++++ charge.go | 11 +++++++---- operations/charge.go | 28 +++++++++++++++------------- 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 authorization_types.go diff --git a/authorization_types.go b/authorization_types.go new file mode 100644 index 0000000..d95b164 --- /dev/null +++ b/authorization_types.go @@ -0,0 +1,10 @@ +package omise + +// AuthorizationType represents an enumeration of a possible authorization type of a Charge object. +type AuthorizationType string + +// AuthorizationType can be one of the following list of constants: +const ( + PreAuth AuthorizationType = "pre_auth" + FinalAuth AuthorizationType = "final_auth" +) diff --git a/charge.go b/charge.go index 0432ac9..3eeb6e0 100644 --- a/charge.go +++ b/charge.go @@ -6,10 +6,13 @@ import "time" // See https://www.omise.co/charges-api for more information. type Charge struct { Base - Status ChargeStatus `json:"status"` - Amount int64 `json:"amount"` - Currency string `json:"currency"` - Description *string `json:"description"` + Status ChargeStatus `json:"status"` + Amount int64 `json:"amount"` + AuthorizationType *AuthorizationType `json:"authorization_type"` + AuthorizedAmount *int64 `json:"authorized_amount"` + CapturedAmount *int64 `json:"captured_amount"` + Currency string `json:"currency"` + Description *string `json:"description"` Capture bool `json:"capture"` Authorized bool `json:"authorized"` diff --git a/operations/charge.go b/operations/charge.go index d24cd84..ab03427 100644 --- a/operations/charge.go +++ b/operations/charge.go @@ -51,18 +51,19 @@ func (req *ListCharges) Describe() *internal.Description { // // fmt.Println("created charge:", charge.ID) type CreateCharge struct { - Customer string `json:"customer,omitempty"` - Card string `json:"card,omitempty"` - Source string `json:"source,omitempty"` - Amount int64 `json:"amount"` - Currency string `json:"currency"` - Offsite omise.OffsiteTypes `json:"offsite,omitempty"` - Description string `json:"description,omitempty"` - DontCapture bool `json:"-"` // inverse, since `capture` defaults to true - ReturnURI string `json:"return_uri,omitempty"` - Metadata map[string]interface{} `json:"metadata,omitempty"` - ExpiresAt *time.Time `json:"expires_at,omitempty"` - ZeroInterestInstallments *bool `json:"zero_interest_installments,omitempty"` + Customer string `json:"customer,omitempty"` + Card string `json:"card,omitempty"` + Source string `json:"source,omitempty"` + Amount int64 `json:"amount"` + Currency string `json:"currency"` + Offsite omise.OffsiteTypes `json:"offsite,omitempty"` + Description string `json:"description,omitempty"` + DontCapture bool `json:"-"` // inverse, since `capture` defaults to true + ReturnURI string `json:"return_uri,omitempty"` + Metadata map[string]interface{} `json:"metadata,omitempty"` + ExpiresAt *time.Time `json:"expires_at,omitempty"` + ZeroInterestInstallments *bool `json:"zero_interest_installments,omitempty"` + AuthorizationType *omise.AuthorizationType `json:"authorization_type,omitempty"` } func (req *CreateCharge) MarshalJSON() ([]byte, error) { @@ -148,7 +149,8 @@ func (req *RetrieveCharge) Describe() *internal.Description { // // fmt.Println("captured:", charge.Captured) type CaptureCharge struct { - ChargeID string `json:"-"` + ChargeID string `json:"-"` + CaptureAmount *int64 `json:"capture_amount,omitempty"` } func (req *CaptureCharge) Describe() *internal.Description { From 414ebdf7393efbd2a5ee37ffdb7eadbfd9580dbe Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Thu, 21 Sep 2023 16:05:15 +0700 Subject: [PATCH 2/5] Remove pointer --- charge.go | 6 +++--- operations/charge.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/charge.go b/charge.go index 3eeb6e0..deafb18 100644 --- a/charge.go +++ b/charge.go @@ -8,9 +8,9 @@ type Charge struct { Base Status ChargeStatus `json:"status"` Amount int64 `json:"amount"` - AuthorizationType *AuthorizationType `json:"authorization_type"` - AuthorizedAmount *int64 `json:"authorized_amount"` - CapturedAmount *int64 `json:"captured_amount"` + AuthorizationType AuthorizationType `json:"authorization_type"` + AuthorizedAmount int64 `json:"authorized_amount"` + CapturedAmount int64 `json:"captured_amount"` Currency string `json:"currency"` Description *string `json:"description"` diff --git a/operations/charge.go b/operations/charge.go index ab03427..5f21373 100644 --- a/operations/charge.go +++ b/operations/charge.go @@ -63,7 +63,7 @@ type CreateCharge struct { Metadata map[string]interface{} `json:"metadata,omitempty"` ExpiresAt *time.Time `json:"expires_at,omitempty"` ZeroInterestInstallments *bool `json:"zero_interest_installments,omitempty"` - AuthorizationType *omise.AuthorizationType `json:"authorization_type,omitempty"` + AuthorizationType omise.AuthorizationType `json:"authorization_type,omitempty"` } func (req *CreateCharge) MarshalJSON() ([]byte, error) { From 6c4630973faab9ecd42bdc177640953f73681e3c Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Thu, 21 Sep 2023 16:13:03 +0700 Subject: [PATCH 3/5] Remove pointer from capture --- operations/charge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations/charge.go b/operations/charge.go index 5f21373..dfcf82e 100644 --- a/operations/charge.go +++ b/operations/charge.go @@ -150,7 +150,7 @@ func (req *RetrieveCharge) Describe() *internal.Description { // fmt.Println("captured:", charge.Captured) type CaptureCharge struct { ChargeID string `json:"-"` - CaptureAmount *int64 `json:"capture_amount,omitempty"` + CaptureAmount int64 `json:"capture_amount,omitempty"` } func (req *CaptureCharge) Describe() *internal.Description { From d7151eed4586b7f35dc2a106eecd32a7f7bc707e Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Mon, 25 Sep 2023 09:12:25 +0700 Subject: [PATCH 4/5] Added tests --- operations/charge_test.go | 42 +++++++- .../chrg_test_5x1753iuub61dfe41q4-get.json | 99 +++++++++++++++++++ 2 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 testdata/fixtures/api.omise.co/charges/chrg_test_5x1753iuub61dfe41q4-get.json diff --git a/operations/charge_test.go b/operations/charge_test.go index dcd9b7d..1ffe4b7 100644 --- a/operations/charge_test.go +++ b/operations/charge_test.go @@ -31,6 +31,24 @@ func TestCreateChargeMarshal(t *testing.T) { }, `{"amount":10000,"currency":"thb","capture":false}`, }, + { + &CreateCharge{ + Amount: 10000, + Currency: "thb", + DontCapture: true, + AuthorizationType: omise.PreAuth, + }, + `{"amount":10000,"currency":"thb","authorization_type":"pre_auth","capture":false}`, + }, + { + &CreateCharge{ + Amount: 10000, + Currency: "thb", + DontCapture: true, + AuthorizationType: omise.FinalAuth, + }, + `{"amount":10000,"currency":"thb","authorization_type":"final_auth","capture":false}`, + }, } for _, td := range testdata { b, err := json.Marshal(td.req) @@ -67,10 +85,11 @@ func TestRetrieveCharge_BillPayment(t *testing.T) { func TestCharge(t *testing.T) { const ( - ChargeID = "chrg_test_4yq7duw15p9hdrjp8oq" - TransactionID = "trxn_test_4yq7duwb9jts1vxgqua" - CardID = "card_test_4yq6tuucl9h4erukfl0" - RefundID = "rfnd_test_4yqmv79ahghsiz23y3c" + ChargeID = "chrg_test_4yq7duw15p9hdrjp8oq" + ChargeIdPartialCapture = "chrg_test_5x1753iuub61dfe41q4" + TransactionID = "trxn_test_4yq7duwb9jts1vxgqua" + CardID = "card_test_4yq6tuucl9h4erukfl0" + RefundID = "rfnd_test_4yqmv79ahghsiz23y3c" ) client := testutil.NewFixedClient(t) @@ -78,6 +97,21 @@ func TestCharge(t *testing.T) { charge := &omise.Charge{} client.MustDo(charge, &CreateCharge{}) r.Equal(t, ChargeID, charge.ID) + r.Equal(t, true, charge.Capture) + r.Equal(t, omise.AuthorizationType(""), charge.AuthorizationType) + r.Equal(t, int64(0), charge.CapturedAmount) + r.Equal(t, int64(0), charge.AuthorizedAmount) + + //partial capture + charge = &omise.Charge{} + client.MustDo(charge, &RetrieveCharge{ChargeIdPartialCapture}) + r.Equal(t, ChargeIdPartialCapture, charge.ID) + r.Equal(t, false, charge.Capture) + r.Equal(t, omise.PreAuth, charge.AuthorizationType) + r.Equal(t, int64(5000), charge.CapturedAmount) + r.Equal(t, int64(10000), charge.AuthorizedAmount) + r.Equal(t, int64(10000), charge.Amount) + charge = &omise.Charge{} client.MustDo(charge, &RetrieveCharge{ChargeID}) diff --git a/testdata/fixtures/api.omise.co/charges/chrg_test_5x1753iuub61dfe41q4-get.json b/testdata/fixtures/api.omise.co/charges/chrg_test_5x1753iuub61dfe41q4-get.json new file mode 100644 index 0000000..079a4f5 --- /dev/null +++ b/testdata/fixtures/api.omise.co/charges/chrg_test_5x1753iuub61dfe41q4-get.json @@ -0,0 +1,99 @@ +{ + "object": "charge", + "id": "chrg_test_5x1753iuub61dfe41q4", + "location": "/charges/chrg_test_5x1753iuub61dfe41q4", + "amount": 10000, + "authorization_type": "pre_auth", + "authorized_amount": 10000, + "captured_amount": 5000, + "net": 4804, + "fee": 183, + "fee_vat": 13, + "interest": 0, + "interest_vat": 0, + "funding_amount": 5000, + "refunded_amount": 0, + "transaction_fees": { + "fee_flat": "0.0", + "fee_rate": "3.65", + "vat_rate": "7.0" + }, + "platform_fee": { + "fixed": null, + "amount": null, + "percentage": null + }, + "currency": "THB", + "funding_currency": "THB", + "ip": null, + "refunds": { + "object": "list", + "data": [], + "limit": 20, + "offset": 0, + "total": 0, + "location": "/charges/chrg_test_5x1753iuub61dfe41q4/refunds", + "order": "chronological", + "from": "1970-01-01T00:00:00Z", + "to": "2023-09-24T16:13:56Z" + }, + "link": null, + "description": null, + "metadata": {}, + "card": { + "object": "card", + "id": "card_test_5x1753cc9r2wkffypp0", + "livemode": false, + "location": "/customers/cust_test_5x1753g3i8l4tt93k8e/cards/card_test_5x1753cc9r2wkffypp0", + "deleted": false, + "street1": null, + "street2": null, + "city": "Bangkok", + "state": null, + "phone_number": null, + "postal_code": "10320", + "country": "us", + "financing": "credit", + "bank": "JPMORGAN CHASE BANK N.A.", + "brand": "Visa", + "fingerprint": "ZmYHTfxzcB4xH5GTzXaPWRRpScr5nGNEDaPJ2XYJQfw=", + "first_digits": null, + "last_digits": "4242", + "name": "JOHN DOE", + "expiration_month": 2, + "expiration_year": 2024, + "security_code_check": true, + "tokenization_method": null, + "created_at": "2023-09-07T16:36:42Z" + }, + "source": null, + "schedule": null, + "customer": "cust_test_5x1753g3i8l4tt93k8e", + "dispute": null, + "transaction": "trxn_test_5x1753qdm0bdrsbrugd", + "failure_code": null, + "failure_message": null, + "status": "successful", + "authorize_uri": null, + "return_uri": null, + "created_at": "2023-09-07T16:36:43Z", + "paid_at": "2023-09-07T16:36:43Z", + "expires_at": "2023-09-14T16:36:43Z", + "expired_at": "2023-09-14T16:36:43Z", + "reversed_at": null, + "zero_interest_installments": false, + "branch": null, + "terminal": null, + "device": null, + "authorized": true, + "capturable": false, + "capture": false, + "disputable": true, + "livemode": false, + "refundable": true, + "reversed": false, + "reversible": false, + "voided": false, + "paid": true, + "expired": false +} From d62359f5416f34860387c220e3f56b1450a09068 Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Mon, 25 Sep 2023 09:51:17 +0700 Subject: [PATCH 5/5] Fixed indentation --- charge.go | 14 +++++++------- operations/charge.go | 28 ++++++++++++++-------------- operations/charge_test.go | 8 ++++---- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/charge.go b/charge.go index deafb18..d0fa42e 100644 --- a/charge.go +++ b/charge.go @@ -6,13 +6,13 @@ import "time" // See https://www.omise.co/charges-api for more information. type Charge struct { Base - Status ChargeStatus `json:"status"` - Amount int64 `json:"amount"` - AuthorizationType AuthorizationType `json:"authorization_type"` - AuthorizedAmount int64 `json:"authorized_amount"` - CapturedAmount int64 `json:"captured_amount"` - Currency string `json:"currency"` - Description *string `json:"description"` + Status ChargeStatus `json:"status"` + Amount int64 `json:"amount"` + AuthorizationType AuthorizationType `json:"authorization_type"` + AuthorizedAmount int64 `json:"authorized_amount"` + CapturedAmount int64 `json:"captured_amount"` + Currency string `json:"currency"` + Description *string `json:"description"` Capture bool `json:"capture"` Authorized bool `json:"authorized"` diff --git a/operations/charge.go b/operations/charge.go index dfcf82e..018ffb7 100644 --- a/operations/charge.go +++ b/operations/charge.go @@ -51,19 +51,19 @@ func (req *ListCharges) Describe() *internal.Description { // // fmt.Println("created charge:", charge.ID) type CreateCharge struct { - Customer string `json:"customer,omitempty"` - Card string `json:"card,omitempty"` - Source string `json:"source,omitempty"` - Amount int64 `json:"amount"` - Currency string `json:"currency"` - Offsite omise.OffsiteTypes `json:"offsite,omitempty"` - Description string `json:"description,omitempty"` - DontCapture bool `json:"-"` // inverse, since `capture` defaults to true - ReturnURI string `json:"return_uri,omitempty"` - Metadata map[string]interface{} `json:"metadata,omitempty"` - ExpiresAt *time.Time `json:"expires_at,omitempty"` - ZeroInterestInstallments *bool `json:"zero_interest_installments,omitempty"` - AuthorizationType omise.AuthorizationType `json:"authorization_type,omitempty"` + Customer string `json:"customer,omitempty"` + Card string `json:"card,omitempty"` + Source string `json:"source,omitempty"` + Amount int64 `json:"amount"` + Currency string `json:"currency"` + Offsite omise.OffsiteTypes `json:"offsite,omitempty"` + Description string `json:"description,omitempty"` + DontCapture bool `json:"-"` // inverse, since `capture` defaults to true + ReturnURI string `json:"return_uri,omitempty"` + Metadata map[string]interface{} `json:"metadata,omitempty"` + ExpiresAt *time.Time `json:"expires_at,omitempty"` + ZeroInterestInstallments *bool `json:"zero_interest_installments,omitempty"` + AuthorizationType omise.AuthorizationType `json:"authorization_type,omitempty"` } func (req *CreateCharge) MarshalJSON() ([]byte, error) { @@ -150,7 +150,7 @@ func (req *RetrieveCharge) Describe() *internal.Description { // fmt.Println("captured:", charge.Captured) type CaptureCharge struct { ChargeID string `json:"-"` - CaptureAmount int64 `json:"capture_amount,omitempty"` + CaptureAmount int64 `json:"capture_amount,omitempty"` } func (req *CaptureCharge) Describe() *internal.Description { diff --git a/operations/charge_test.go b/operations/charge_test.go index 1ffe4b7..1ae0898 100644 --- a/operations/charge_test.go +++ b/operations/charge_test.go @@ -85,11 +85,11 @@ func TestRetrieveCharge_BillPayment(t *testing.T) { func TestCharge(t *testing.T) { const ( - ChargeID = "chrg_test_4yq7duw15p9hdrjp8oq" + ChargeID = "chrg_test_4yq7duw15p9hdrjp8oq" ChargeIdPartialCapture = "chrg_test_5x1753iuub61dfe41q4" - TransactionID = "trxn_test_4yq7duwb9jts1vxgqua" - CardID = "card_test_4yq6tuucl9h4erukfl0" - RefundID = "rfnd_test_4yqmv79ahghsiz23y3c" + TransactionID = "trxn_test_4yq7duwb9jts1vxgqua" + CardID = "card_test_4yq6tuucl9h4erukfl0" + RefundID = "rfnd_test_4yqmv79ahghsiz23y3c" ) client := testutil.NewFixedClient(t)