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..d0fa42e 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..018ffb7 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 { diff --git a/operations/charge_test.go b/operations/charge_test.go index dcd9b7d..1ae0898 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 +}