This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for listing payment methods for an org. Fixes: TIE-118
- Loading branch information
Showing
8 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package payment | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
type Method struct { | ||
raw json.RawMessage // hidden so we can add fields to Method later, and replace if neccessary | ||
info struct { | ||
ID string | ||
Created int | ||
} | ||
} | ||
|
||
func (m *Method) UnmarshalJSON(data []byte) error { | ||
if err := json.Unmarshal(data, &m.raw); err != nil { | ||
return err | ||
} | ||
return json.Unmarshal(data, &m.info) | ||
} | ||
|
||
func (m Method) MarshalJSON() ([]byte, error) { | ||
return m.raw.MarshalJSON() | ||
} | ||
|
||
func (m Method) Created() time.Time { | ||
return time.Unix(int64(m.info.Created), 0) | ||
} | ||
|
||
func (m Method) ProviderID() string { | ||
return m.info.ID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package payment | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"kr.dev/diff" | ||
) | ||
|
||
const testPaymentMethodJSON = `{ | ||
"id": "pm_1MdiJIIhUsqiXPckNAfPOQlt", | ||
"object": "payment_method", | ||
"billing_details": { | ||
"address": { | ||
"city": "San Francisco", | ||
"country": "US", | ||
"line1": "1234 Fake Street", | ||
"line2": null, | ||
"postal_code": "94102", | ||
"state": "CA" | ||
}, | ||
"email": "jenny@example.com", | ||
"name": null, | ||
"phone": "+15555555555" | ||
}, | ||
"card": { | ||
"brand": "visa", | ||
"checks": { | ||
"address_line1_check": null, | ||
"address_postal_code_check": null, | ||
"cvc_check": "pass" | ||
}, | ||
"country": "US", | ||
"exp_month": 8, | ||
"exp_year": 2024, | ||
"fingerprint": "noAKwq37KQONloUT", | ||
"funding": "credit", | ||
"generated_from": null, | ||
"last4": "4242", | ||
"networks": { | ||
"available": [ | ||
"visa" | ||
], | ||
"preferred": null | ||
}, | ||
"three_d_secure_usage": { | ||
"supported": true | ||
}, | ||
"wallet": null | ||
}, | ||
"created": 123456789, | ||
"customer": null, | ||
"livemode": false, | ||
"metadata": { | ||
"order_id": "123456789" | ||
}, | ||
"type": "card" | ||
}` | ||
|
||
func TestPayment(t *testing.T) { | ||
var pm Method | ||
err := json.Unmarshal([]byte(testPaymentMethodJSON), &pm) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !pm.Created().Equal(time.Unix(123456789, 0)) { | ||
t.Fatalf("Created = %v; want 123456789", pm.Created().Unix()) | ||
} | ||
|
||
if pm.ProviderID() != "pm_1MdiJIIhUsqiXPckNAfPOQlt" { | ||
t.Fatalf("ID = %q; want pm_1MdiJIIhUsqiXPckNAfPOQlt", pm.ProviderID()) | ||
} | ||
} | ||
|
||
func TestMethodRoundTrip(t *testing.T) { | ||
var pm Method | ||
err := json.Unmarshal([]byte(testPaymentMethodJSON), &pm) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var ma map[string]any | ||
if err := json.Unmarshal([]byte(testPaymentMethodJSON), &ma); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
data, err := json.Marshal(pm) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var mb map[string]any | ||
if err = json.Unmarshal(data, &mb); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
diff.Test(t, t.Errorf, ma, mb) | ||
} |