Skip to content

Commit

Permalink
Merge pull request #66 from omise/add_partial_capture_support
Browse files Browse the repository at this point in the history
Add partial capture support
  • Loading branch information
AnasNaouchi authored Sep 25, 2023
2 parents 774eb20 + d62359f commit 2ef7be4
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 21 deletions.
10 changes: 10 additions & 0 deletions authorization_types.go
Original file line number Diff line number Diff line change
@@ -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"
)
11 changes: 7 additions & 4 deletions charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
28 changes: 15 additions & 13 deletions operations/charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 38 additions & 4 deletions operations/charge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -67,17 +85,33 @@ 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)

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})
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2ef7be4

Please sign in to comment.