Skip to content

Commit

Permalink
Unmarshal error on payment creation in production environment (#35)
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
matm authored Jan 18, 2023
1 parent d99bbfc commit 070eded
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v 1.0.3
- Unmarshal error on payment creation in production environment. #34

v 1.0.2
- sandbox: add support for optional case for new payments. #32
- payment: missing PayAmount and PayCurrency fields. #30
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Topic|Endpoint|Package.Method|Implemented
## Installation

```bash
$ go get github.com/matm/[email protected].2
$ go get github.com/matm/[email protected].3
```

## CLI Tool
Expand Down
51 changes: 49 additions & 2 deletions payments/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package payments
import (
"encoding/json"
"errors"
"strconv"
"strings"

"github.com/matm/go-nowpayments/config"
"github.com/matm/go-nowpayments/core"
"github.com/rotisserie/eris"
)
Expand Down Expand Up @@ -75,6 +77,14 @@ type Payment struct {
UpdatedAt string `json:"updated_at"`
}

// PaymentProd is an ugly hack. This is because the production env returns a string for `pay_amount`
// whereas the sandbox env returns a float64 :(
// Hopefully they will fix this soon.
type PaymentProd struct {
Payment
PayAmount string `json:"pay_amount"`
}

// New creates a payment.
func New(pa *PaymentArgs) (*Payment, error) {
if pa == nil {
Expand All @@ -84,7 +94,13 @@ func New(pa *PaymentArgs) (*Payment, error) {
if err != nil {
return nil, eris.Wrap(err, "payment args")
}
p := &Payment{}
var p interface{}
// Ugly hack but required at the moment :(
if config.Server() == string(core.ProductionBaseURL) {
p = &PaymentProd{}
} else {
p = &Payment{}
}
par := &core.SendParams{
RouteName: "payment-create",
Into: &p,
Expand All @@ -94,7 +110,38 @@ func New(pa *PaymentArgs) (*Payment, error) {
if err != nil {
return nil, err
}
return p, nil
// Ugly hack continuing ...
var pv *Payment
switch p.(type) {
case *Payment:
pv = p.(*Payment)
case *PaymentProd:
j := p.(*PaymentProd)
pv = &Payment{
ID: j.ID,
AmountReceived: j.AmountReceived,
BurningPercent: j.BurningPercent,
CreatedAt: j.CreatedAt,
ExpirationEstimateDate: j.ExpirationEstimateDate,
Network: j.Network,
NetworkPrecision: j.NetworkPrecision,
PayAddress: j.PayAddress,
PayCurrency: j.PayCurrency,
PayinExtraID: j.PayinExtraID,
PurchaseID: j.PurchaseID,
SmartContract: j.SmartContract,
Status: j.Status,
TimeLimit: j.TimeLimit,
UpdatedAt: j.UpdatedAt,
}
// Now convert the `pay_amount`.
pm, err := strconv.ParseFloat(j.PayAmount, 64)
if err != nil {
return nil, eris.Wrap(err, "pay_amount hack convert")
}
pv.PayAmount = pm
}
return pv, nil
}

type InvoicePaymentArgs struct {
Expand Down
43 changes: 42 additions & 1 deletion payments/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package payments
import (
"errors"
"net/http"
"strings"
"testing"

"github.com/matm/go-nowpayments/mocks"
"github.com/matm/go-nowpayments/config"
"github.com/matm/go-nowpayments/core"
"github.com/matm/go-nowpayments/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
tests := []struct {
name string
pa *PaymentArgs
Expand Down Expand Up @@ -47,6 +51,43 @@ func TestNew(t *testing.T) {
assert.Equal("1234", p.ID)
},
},
{"hack check", &PaymentArgs{
PurchaseID: "1234",
PaymentAmount: PaymentAmount{PriceAmount: 10.0},
},
func(c *mocks.HTTPClient) {
resp := newResponseOK(`{"payment_id":"1234","pay_amount":"3.5"}`)
c.EXPECT().Do(mock.Anything).Return(resp, nil)
// Forces the detection of the production environment.
err := config.Load(strings.NewReader(`{"server":"https://api.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
require.NoError(err)
}, func(p *Payment, err error) {
assert.NoError(err)
assert.NotNil(p)
assert.Equal("1234", p.ID)
assert.Equal(3.5, p.PayAmount)
// Restore env.
err = config.Load(strings.NewReader(`{"server":"https://api-sandbox.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
require.NoError(err)
},
},
{"hack check, empty pay amount", &PaymentArgs{
PurchaseID: "1234",
PaymentAmount: PaymentAmount{PriceAmount: 10.0},
},
func(c *mocks.HTTPClient) {
resp := newResponseOK(`{"payment_id":"1234"}`)
c.EXPECT().Do(mock.Anything).Return(resp, nil)
// Forces the detection of the production environment.
err := config.Load(strings.NewReader(`{"server":"https://api.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
require.NoError(err)
}, func(p *Payment, err error) {
assert.Error(err)
// Restore env.
err = config.Load(strings.NewReader(`{"server":"https://api-sandbox.nowpayments.io/v1","apiKey":"a","login":"l","password":"p"}`))
require.NoError(err)
},
},
{"route check", &PaymentArgs{},
func(c *mocks.HTTPClient) {
resp := newResponseOK(`{"payment_id":"1234"}`)
Expand Down

0 comments on commit 070eded

Please sign in to comment.