Skip to content

Commit 070eded

Browse files
author
Mathias M
authored
Unmarshal error on payment creation in production environment (#35)
Fixes #34
1 parent d99bbfc commit 070eded

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v 1.0.3
2+
- Unmarshal error on payment creation in production environment. #34
3+
14
v 1.0.2
25
- sandbox: add support for optional case for new payments. #32
36
- payment: missing PayAmount and PayCurrency fields. #30

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Topic|Endpoint|Package.Method|Implemented
3333
## Installation
3434

3535
```bash
36-
$ go get github.com/matm/[email protected].2
36+
$ go get github.com/matm/[email protected].3
3737
```
3838

3939
## CLI Tool

payments/payment.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package payments
33
import (
44
"encoding/json"
55
"errors"
6+
"strconv"
67
"strings"
78

9+
"github.com/matm/go-nowpayments/config"
810
"github.com/matm/go-nowpayments/core"
911
"github.com/rotisserie/eris"
1012
)
@@ -75,6 +77,14 @@ type Payment struct {
7577
UpdatedAt string `json:"updated_at"`
7678
}
7779

80+
// PaymentProd is an ugly hack. This is because the production env returns a string for `pay_amount`
81+
// whereas the sandbox env returns a float64 :(
82+
// Hopefully they will fix this soon.
83+
type PaymentProd struct {
84+
Payment
85+
PayAmount string `json:"pay_amount"`
86+
}
87+
7888
// New creates a payment.
7989
func New(pa *PaymentArgs) (*Payment, error) {
8090
if pa == nil {
@@ -84,7 +94,13 @@ func New(pa *PaymentArgs) (*Payment, error) {
8494
if err != nil {
8595
return nil, eris.Wrap(err, "payment args")
8696
}
87-
p := &Payment{}
97+
var p interface{}
98+
// Ugly hack but required at the moment :(
99+
if config.Server() == string(core.ProductionBaseURL) {
100+
p = &PaymentProd{}
101+
} else {
102+
p = &Payment{}
103+
}
88104
par := &core.SendParams{
89105
RouteName: "payment-create",
90106
Into: &p,
@@ -94,7 +110,38 @@ func New(pa *PaymentArgs) (*Payment, error) {
94110
if err != nil {
95111
return nil, err
96112
}
97-
return p, nil
113+
// Ugly hack continuing ...
114+
var pv *Payment
115+
switch p.(type) {
116+
case *Payment:
117+
pv = p.(*Payment)
118+
case *PaymentProd:
119+
j := p.(*PaymentProd)
120+
pv = &Payment{
121+
ID: j.ID,
122+
AmountReceived: j.AmountReceived,
123+
BurningPercent: j.BurningPercent,
124+
CreatedAt: j.CreatedAt,
125+
ExpirationEstimateDate: j.ExpirationEstimateDate,
126+
Network: j.Network,
127+
NetworkPrecision: j.NetworkPrecision,
128+
PayAddress: j.PayAddress,
129+
PayCurrency: j.PayCurrency,
130+
PayinExtraID: j.PayinExtraID,
131+
PurchaseID: j.PurchaseID,
132+
SmartContract: j.SmartContract,
133+
Status: j.Status,
134+
TimeLimit: j.TimeLimit,
135+
UpdatedAt: j.UpdatedAt,
136+
}
137+
// Now convert the `pay_amount`.
138+
pm, err := strconv.ParseFloat(j.PayAmount, 64)
139+
if err != nil {
140+
return nil, eris.Wrap(err, "pay_amount hack convert")
141+
}
142+
pv.PayAmount = pm
143+
}
144+
return pv, nil
98145
}
99146

100147
type InvoicePaymentArgs struct {

payments/payment_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package payments
33
import (
44
"errors"
55
"net/http"
6+
"strings"
67
"testing"
78

8-
"github.com/matm/go-nowpayments/mocks"
9+
"github.com/matm/go-nowpayments/config"
910
"github.com/matm/go-nowpayments/core"
11+
"github.com/matm/go-nowpayments/mocks"
1012
"github.com/stretchr/testify/assert"
1113
"github.com/stretchr/testify/mock"
14+
"github.com/stretchr/testify/require"
1215
)
1316

1417
func TestNew(t *testing.T) {
1518
assert := assert.New(t)
19+
require := require.New(t)
1620
tests := []struct {
1721
name string
1822
pa *PaymentArgs
@@ -47,6 +51,43 @@ func TestNew(t *testing.T) {
4751
assert.Equal("1234", p.ID)
4852
},
4953
},
54+
{"hack check", &PaymentArgs{
55+
PurchaseID: "1234",
56+
PaymentAmount: PaymentAmount{PriceAmount: 10.0},
57+
},
58+
func(c *mocks.HTTPClient) {
59+
resp := newResponseOK(`{"payment_id":"1234","pay_amount":"3.5"}`)
60+
c.EXPECT().Do(mock.Anything).Return(resp, nil)
61+
// Forces the detection of the production environment.
62+
err := config.Load(strings.NewReader(`{"server":"https://api.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
63+
require.NoError(err)
64+
}, func(p *Payment, err error) {
65+
assert.NoError(err)
66+
assert.NotNil(p)
67+
assert.Equal("1234", p.ID)
68+
assert.Equal(3.5, p.PayAmount)
69+
// Restore env.
70+
err = config.Load(strings.NewReader(`{"server":"https://api-sandbox.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
71+
require.NoError(err)
72+
},
73+
},
74+
{"hack check, empty pay amount", &PaymentArgs{
75+
PurchaseID: "1234",
76+
PaymentAmount: PaymentAmount{PriceAmount: 10.0},
77+
},
78+
func(c *mocks.HTTPClient) {
79+
resp := newResponseOK(`{"payment_id":"1234"}`)
80+
c.EXPECT().Do(mock.Anything).Return(resp, nil)
81+
// Forces the detection of the production environment.
82+
err := config.Load(strings.NewReader(`{"server":"https://api.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
83+
require.NoError(err)
84+
}, func(p *Payment, err error) {
85+
assert.Error(err)
86+
// Restore env.
87+
err = config.Load(strings.NewReader(`{"server":"https://api-sandbox.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
88+
require.NoError(err)
89+
},
90+
},
5091
{"route check", &PaymentArgs{},
5192
func(c *mocks.HTTPClient) {
5293
resp := newResponseOK(`{"payment_id":"1234"}`)

0 commit comments

Comments
 (0)