Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Start removing use of deprecated content fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lildude committed Jan 11, 2019
1 parent 2c3ece9 commit 81081a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,30 @@ func TxnHandler(w http.ResponseWriter, r *http.Request) {

// Store the webhook uid in an environment variable and use to try catch duplicate deliveries
ltu, _ := os.LookupEnv("LAST_TRANSACTION_UID")
if ltu != "" && ltu == wh.Content.TransactionUID {
if ltu != "" && ltu == wh.WebhookNotificationUID {
log.Println("INFO: ignoring duplicate webhook delivery")
return
}

os.Setenv("LAST_TRANSACTION_UID", wh.Content.TransactionUID)
os.Setenv("LAST_TRANSACTION_UID", wh.WebhookNotificationUID)

log.Println("INFO: type:", wh.Content.Type)
log.Println("INFO: type:", wh.WebhookType)
log.Printf("INFO: amount: %.2f", wh.Content.Amount)

// Ignore anything other than card transactions or specific inbound transactions likely to be large payments like salary etc
if wh.Content.Type != "TRANSACTION_CARD" &&
wh.Content.Type != "TRANSACTION_MOBILE_WALLET" &&
wh.Content.Type != "FASTER_PAYMENTS_IN" &&
wh.Content.Type != "TRANSACTION_NOSTRO_DEPOSIT" {
log.Printf("INFO: ignoring %s transaction\n", wh.Content.Type)
if wh.WebhookType != "TRANSACTION_CARD" &&
wh.WebhookType != "TRANSACTION_MOBILE_WALLET" &&
wh.WebhookType != "TRANSACTION_FASTER_PAYMENTS_IN" &&
wh.WebhookType != "TRANSACTION_NOSTRO_DEPOSIT" {
log.Printf("INFO: ignoring %s transaction\n", wh.WebhookType)
return
}

var ra int64
var prettyRa float64
var destGoal string

switch wh.Content.Type {
switch wh.WebhookType {
case "TRANSACTION_CARD", "TRANSACTION_MOBILE_WALLET":
// Return early if no savings goal
if s.SavingGoal == "" {
Expand All @@ -105,7 +105,7 @@ func TxnHandler(w http.ResponseWriter, r *http.Request) {
}
destGoal = s.SavingGoal
if wh.Content.Amount >= 0.0 {
log.Printf("INFO: ignoring inbound %s transaction\n", wh.Content.Type)
log.Printf("INFO: ignoring inbound %s transaction\n", wh.WebhookType)
return
}
// Round up to the nearest major unit
Expand All @@ -114,7 +114,7 @@ func TxnHandler(w http.ResponseWriter, r *http.Request) {
prettyRa = float64(ra) / 100
log.Println("INFO: round-up yields:", ra)

case "FASTER_PAYMENTS_IN", "TRANSACTION_NOSTRO_DEPOSIT":
case "TRANSACTION_FASTER_PAYMENTS_IN", "TRANSACTION_NOSTRO_DEPOSIT":
// Return early if no savings goal
if s.SweepSavingGoal == "" {
log.Println("INFO: no sweep savings goal set. Nothing to do.")
Expand Down
32 changes: 16 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ func TestTxnHandler(t *testing.T) {
{
name: "non-card outgoing transaction",
method: http.MethodPost,
body: `{"content":{"type":"DIRECT_DEBIT"}}`,
body: `{"webhookType":"TRANSACTION_DIRECT_DEBIT"}`,
goal: "round",
message: "INFO: ignoring DIRECT_DEBIT transaction",
message: "INFO: ignoring TRANSACTION_DIRECT_DEBIT transaction",
mockresp: []byte{},
signature: "",
},
{
name: "card outbound transaction",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -24.99}}`,
body: `{"webhookType":"TRANSACTION_CARD","content":{"amount": -24.99}}`,
goal: "round",
message: "INFO: transfer successful (Txn: 12345-67890 | 0.01)",
mockresp: []byte(`{"transferUid":"12345-67890","success":true,"errors":[]}`),
Expand All @@ -144,7 +144,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "no roundup goal set",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -24.99}}`,
body: `{"webhookType":"TRANSACTION_CARD","content":{"amount": -24.99}}`,
goal: "",
message: "INFO: no roundup savings goal set. Nothing to do.",
mockresp: []byte(`{"transferUid":"12345-67890","success":true,"errors":[]}`),
Expand All @@ -153,7 +153,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "card inbound transaction",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": 24.99}}`,
body: `{"webhookType":"TRANSACTION_CARD","content":{"amount": 24.99}}`,
goal: "round",
message: "INFO: ignoring inbound TRANSACTION_CARD transaction",
mockresp: []byte{},
Expand All @@ -162,7 +162,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "card nothing to roundup",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -1.00}}`,
body: `{"webhookType":"TRANSACTION_CARD","content":{"amount": -1.00}}`,
goal: "round",
message: "INFO: nothing to transfer",
mockresp: []byte{},
Expand All @@ -171,7 +171,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "mobile wallet outbound transaction",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_MOBILE_WALLET","amount": -24.99}}`,
body: `{"webhookType":"TRANSACTION_MOBILE_WALLET","content":{"amount": -24.99}}`,
goal: "round",
message: "INFO: transfer successful (Txn: 12345-67890 | 0.01)",
mockresp: []byte(`{"transferUid":"12345-67890","success":true,"errors":[]}`),
Expand All @@ -180,7 +180,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "mobile wallet inbound transaction",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_MOBILE_WALLET","amount": 24.99}}`,
body: `{"webhookType":"TRANSACTION_MOBILE_WALLET","content":{"amount": 24.99}}`,
goal: "round",
message: "INFO: ignoring inbound TRANSACTION_MOBILE_WALLET transaction",
mockresp: []byte{},
Expand All @@ -189,7 +189,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "mobile wallet nothing to roundup",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_MOBILE_WALLET","amount": -1.00}}`,
body: `{"webhookType":"TRANSACTION_MOBILE_WALLET","content":{"amount": -1.00}}`,
goal: "round",
message: "INFO: nothing to transfer",
mockresp: []byte{},
Expand All @@ -198,7 +198,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "non-card inbound above threshold",
method: http.MethodPost,
body: `{"content":{"type":"FASTER_PAYMENTS_IN","amount": 2500.00}}`,
body: `{"webhookType":"TRANSACTION_FASTER_PAYMENTS_IN","content":{"amount": 2500.00}}`,
goal: "sweep",
message: "INFO: transfer successful (Txn: | 254.12)",
mockresp: []byte(`{"effectiveBalance": 2754.12}`),
Expand All @@ -207,7 +207,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "no sweep goal set",
method: http.MethodPost,
body: `{"content":{"type":"FASTER_PAYMENTS_IN","amount": 2500.00}}`,
body: `{"webhookType":"TRANSACTION_FASTER_PAYMENTS_IN","content":{"amount": 2500.00}}`,
goal: "",
message: "INFO: no sweep savings goal set. Nothing to do.",
mockresp: []byte(`{"effectiveBalance": 2754.12}`),
Expand All @@ -216,7 +216,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "non-card inbound below threshold",
method: http.MethodPost,
body: `{"content":{"type":"FASTER_PAYMENTS_IN","amount": 500.00}}`,
body: `{"webhookType":"TRANSACTION_FASTER_PAYMENTS_IN","content":{"amount": 500.00}}`,
goal: "sweep",
message: "INFO: ignoring inbound transaction below sweep threshold",
mockresp: []byte(`{"amount": 500.00, "balance": 754.12}`),
Expand All @@ -225,7 +225,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "card duplicate webhook delivery 1",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -24.99,"transactionUid":"test-trans-uid"}}`,
body: `{"webhookType":"TRANSACTION_CARD","webhookNotificationUid":"test-trans-uid","content":{"amount": -24.99}}`,
goal: "round",
message: "INFO: transfer successful",
mockresp: []byte{},
Expand All @@ -234,7 +234,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "card duplicate webhook delivery 2",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -24.99,"transactionUid":"test-trans-uid"}}`,
body: `{"webhookType":"TRANSACTION_CARD","webhookNotificationUid":"test-trans-uid","content":{"amount": -24.99}}`,
goal: "round",
message: "INFO: ignoring duplicate webhook delivery",
mockresp: []byte{},
Expand All @@ -243,7 +243,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "bad signature",
method: http.MethodPost,
body: `{"content":{"type":"TRANSACTION_CARD","amount": -24.99,"transactionUid":"test-trans-uid"}}`,
body: `{"webhookType":"TRANSACTION_CARD","webhookNotificationUid":"test-trans-uid","content":{"amount": -24.99}}`,
goal: "round",
message: "ERROR: invalid request signature receive",
mockresp: []byte{},
Expand All @@ -252,7 +252,7 @@ func TestTxnHandler(t *testing.T) {
{
name: "forced failure to get balance",
method: http.MethodPost,
body: `{"content":{"type":"FASTER_PAYMENTS_IN","amount": 2500.00}}`,
body: `{"webhookType":"TRANSACTION_FASTER_PAYMENTS_IN","content":{"amount": 2500.00}}`,
goal: "sweep",
message: "ERROR: problem getting balance",
mockresp: []byte(`{"broken": "json`),
Expand Down

0 comments on commit 81081a6

Please sign in to comment.