Skip to content

Commit

Permalink
Merge pull request #1 from iamport/feature/subscribe
Browse files Browse the repository at this point in the history
Implemented subscribe & subscribe.customer
  • Loading branch information
psy2848048 authored Nov 2, 2020
2 parents 0c17687 + 4da21b9 commit 5e00208
Show file tree
Hide file tree
Showing 21 changed files with 1,777 additions and 97 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go
go:
- 1.15.x
dist: focal
osx_image: xcode12.2
script:
- bash ./scripts/tests_with_cover.sh
os:
- linux
- osx
after_success:
- bash <(curl -s https://codecov.io/bash)
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# go-iamport

[![Build Status](https://travis-ci.com/iamport/go-iamport.svg?branch=master)](https://travis-ci.com/iamport/go-iamport) [![codecov](https://codecov.io/gh/iamport/go-iamport/branch/master/graph/badge.svg?token=TS1W2W9UO0)](https://codecov.io/gh/iamport/go-iamport)

Go Language 아임포트 Rest API Client
https://api.iamport.kr

Expand Down Expand Up @@ -44,17 +46,23 @@ https://api.iamport.kr
- escrows
- POST /escrows/logis/{imp_uid}
- PUT /escrows/logis/{imp_uid}

### TODO
- subscribe
- POST /subscribe/payments/ontime
- POST /subscribe/payments/again
- POST /subscribe/payments/schedule
- POST /subscribe/payments/unschedule
- GET /subscribe/payments/schedule/{merchant_uid}
- GET /subscribe/payments/schedule/customers/{customer_uid}
- subscribe.customer
- GET /subscribe/customers
- DELETE /subscribe/customers/{customer_uid}
- GET /subscribe/customers/{customer_uid}
- POST /subscribe/customers/{customer_uid}
- GET /subscribe/customers/{customer_uid}/payments
- GET /subscribe/customers/{customer_uid}/schedules

### TODO

- vbanks
- customers
- payco
Expand All @@ -64,4 +72,4 @@ https://api.iamport.kr
- external
- certifications
- cards
- banks
- banks
15 changes: 7 additions & 8 deletions authenticate/authenticate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package authenticate

import (
"bytes"
"errors"
"net/http"
"net/url"
"strings"
"time"

"github.com/iamport/interface/gen_src/go/authenticate"
"github.com/iamport/interface/gen_src/go/v1/authenticate"
"github.com/iamport/go-iamport/util"
"google.golang.org/protobuf/encoding/protojson"
)
Expand All @@ -26,7 +26,7 @@ const (
type Authenticate struct {
APIUrl string
Client *http.Client
RestAPIKeyAndSecret url.Values
RestAPIKeyAndSecret []byte
Token string
Expired time.Time
}
Expand All @@ -46,13 +46,12 @@ func NewAuthenticate(apiURL string, cli *http.Client, restAPIKey string, restAPI
return nil, errors.New(ErrRestAPISecretMissing)
}

form := url.Values{}
form.Set(IMPKey, restAPIKey)
form.Set(IMPSecret, restAPISecret)
keysStrs := []string{`{"`, IMPKey, `":"`, restAPIKey, `", "`, IMPSecret, `":"`, restAPISecret, `"}`}

auth := &Authenticate{
APIUrl: apiURL,
Client: cli,
RestAPIKeyAndSecret: form,
RestAPIKeyAndSecret: bytes.NewBufferString(strings.Join(keysStrs, "")).Bytes(),
}

err := auth.RequestToken()
Expand Down Expand Up @@ -84,7 +83,7 @@ func (a *Authenticate) RequestToken() error {
urls := []string{a.APIUrl, URLGetToken}
urlGetToken := strings.Join(urls, "")

res, err := util.CallWithForm(a.Client, "", urlGetToken, util.POST, &a.RestAPIKeyAndSecret)
res, err := util.CallWithForm(a.Client, "", urlGetToken, util.POST, a.RestAPIKeyAndSecret)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion authenticate/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ const (
BaseURL = "https://api.iamport.kr"
RestApiKey = "2737886909191347"
RestApiSecret = "DNZ25OYnAk9qaRobLy9SWEBGyJKP1PsEDrHIfF6QZfha6FmSevKa9mRI6Cx7s0L5rsOH8Ux8aPihvE9J"

BingBongRestApiKey = "2421367378124191"
BingBongRestApiSecret = "TuL0mVHDmZMFDz3mncCQiA2idqkHxuLrZwrms9ZO8MuMmx8JKLz3lBlz2Fgza10aysw0BwPKijwEQFoA"
)

func GetMockBaseAuthenticate() *Authenticate {
return getMockAuthenticate(RestApiKey, RestApiSecret)
}

func GetMockBingBongAuthenticate() *Authenticate {
return getMockAuthenticate(BingBongRestApiKey, BingBongRestApiSecret)
}

func getMockAuthenticate(key string, secret string) *Authenticate {
cli := &http.Client{}

auth, _ := NewAuthenticate(BaseURL, cli, RestApiKey, RestApiSecret)
auth, _ := NewAuthenticate(BaseURL, cli, key, secret)

return auth
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ go 1.15

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/iamport/interface v0.0.0-20201005060715-38b463e382fe
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.0 // indirect
github.com/iamport/interface v0.0.4
github.com/stretchr/testify v1.6.1
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
Expand Down
313 changes: 311 additions & 2 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions iamport/iamport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ErrMustExistImpUID = "iamport: imp_uid must be exist"
ErrMustExistMerchantUID = "iamport: Merchant UID must be exist"
ErrMustExistImpUIDorMerchantUID = "iamport: imp_uid or Merchant UID must be exist"
ErrMustExistCustomerUID = "iamport: customer_uid must be exist"
ErrInvalidStatusParam = "iamport: status parmeter is invalid. must be all, ready, paid, failed and cancelled"
ErrInvalidSortParam = "iamport: sort parmeter is invalid. must be -started, started, -paid, paid, -updated and updated"
ErrInvalidPage = "iamport: page is more than 1"
Expand Down
6 changes: 3 additions & 3 deletions iamport/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"time"

TypePayment "github.com/iamport/interface/gen_src/go/payment"
TypePayment "github.com/iamport/interface/gen_src/go/v1/payment"
"github.com/iamport/go-iamport/payment"
"github.com/iamport/go-iamport/util"
)
Expand Down Expand Up @@ -216,7 +216,7 @@ func (iamport *Iamport) CancelPaymentImpUID(iuid string, merchantUID string, amo
return nil, errors.New(ErrInvalidAmount)
}

req := &TypePayment.PaymentCancleRequest{
req := &TypePayment.PaymentCancelRequest{
ImpUid: iuid,
MerchantUid: merchantUID,
Amount: amount,
Expand All @@ -228,7 +228,7 @@ func (iamport *Iamport) CancelPaymentImpUID(iuid string, merchantUID string, amo
RefundAccount: refundAccount,
}

res, err := payment.Cancle(iamport.Authenticate, req)
res, err := payment.Cancel(iamport.Authenticate, req)
if err != nil {
return nil, err
}
Expand Down
29 changes: 17 additions & 12 deletions iamport/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package iamport

import (
"math/rand"
"strings"
"testing"
"time"

"github.com/iamport/go-iamport/authenticate"
"github.com/iamport/go-iamport/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -193,28 +193,33 @@ func TestGetPaymentsStatusWithMoreThan3Month(t *testing.T) {
assert.Nil(t, payment)
}

// TODO 테스트 데이터 필요 (KCP or Payco)
func xTestGetPaymentBalanceImpUID(t *testing.T) {
func TestGetPaymentBalanceImpUID(t *testing.T) {
iamport, err := NewIamport(authenticate.BaseURL, authenticate.RestApiKey, authenticate.RestApiSecret)
assert.NoError(t, err)

payment, err := iamport.GetPaymentBalanceImpUID("imp_088621754304")
assert.Error(t, err)
assert.Nil(t, payment)
}

func TestCancelPaymentImpUID(t *testing.T) {
iamport, err := NewIamport(authenticate.BaseURL, authenticate.RestApiKey, authenticate.RestApiSecret)
assert.NoError(t, err)
assert.NotNil(t, payment)

payment, err := iamport.CancelPaymentImpUID(
"imp_088621754304", TMerchantUID, float64(1000), float64(0), float64(0),
"just test", "just test", "", "",
)

assert.Error(t, err)
assert.Nil(t, payment)
}

func TestPreparePayment(t *testing.T) {
iamport, err := NewIamport(authenticate.BaseURL, authenticate.RestApiKey, authenticate.RestApiSecret)
assert.NoError(t, err)

rand.Seed(time.Now().UnixNano())
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVEWXYZabcdefghijklmnopqrstuvewxyz0123456789")
var merchantBytes strings.Builder
for i := 0; i < 20; i++ {
merchantBytes.WriteRune(chars[rand.Intn(len(chars))])
}

merchantUID := merchantBytes.String()
merchantUID := util.GetRandomString(20)
amount := rand.Intn(10000)

payment, err := iamport.PreparePayment(merchantUID, float64(amount))
Expand Down
Loading

0 comments on commit 5e00208

Please sign in to comment.