Skip to content

Commit

Permalink
new PrimeTrust api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmode committed Oct 5, 2020
1 parent 64d9c6e commit 187bb4d
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 19 deletions.
14 changes: 9 additions & 5 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func CreateNewAccount(account *models.AccountForm) (*models.Account, error) {

func GetAgreement(agreementId string) (*models.Agreement, error) {
apiUrl := fmt.Sprintf("%s/agreements/%s", _apiPrefix, agreementId)
color.Green("GetAgreement apiURL:%v", apiUrl)
req, err := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
Expand All @@ -154,9 +155,9 @@ func GetAgreementPreview(inputs *models.AgreementForm) (*models.Agreement, error
jsonData := new(bytes.Buffer)
json.NewEncoder(jsonData).Encode(inputs)

apiUrl := fmt.Sprintf("%s/agreement-previews", _apiPrefix)
apiURL := fmt.Sprintf("%s/agreement-previews", _apiPrefix)

req, err := http.NewRequest("POST", apiUrl, jsonData)
req, err := http.NewRequest("POST", apiURL, jsonData)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", _jwt)

Expand All @@ -170,7 +171,7 @@ func GetAgreementPreview(inputs *models.AgreementForm) (*models.Agreement, error
body, _ := ioutil.ReadAll(res.Body)

if res.StatusCode != http.StatusCreated {
return nil, errors.New(fmt.Sprintf("%s: %s", res.Status, string(body)))
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
}

response := models.Agreement{}
Expand All @@ -186,18 +187,21 @@ func GetAgreementPreview(inputs *models.AgreementForm) (*models.Agreement, error
}

func SandboxAccountOpen(accountId string) (*models.AccountData, error) {
apiUrl := fmt.Sprintf("%s/accounts/%s/sandbox/open", _apiPrefix, accountId)
req, err := http.NewRequest("POST", apiUrl, nil)
apiURL := fmt.Sprintf("%s/accounts/%s/sandbox/open", _apiPrefix, accountId)
req, err := http.NewRequest("POST", apiURL, nil)
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
color.Red("SandboxAccountOpen: error:%v %v", apiURL, err)
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
color.Red("SandboxAccountOpen: error:%v %v", apiURL, errors.New(res.Status))

return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
Expand Down
69 changes: 65 additions & 4 deletions cash-transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,50 @@ func GetCashTransaction(transactionID string) (*models.CashTransaction, error) {
return &response, nil
}

// GetCashTransactionsPage returns a single page of transactions using the USD filter
func GetCashTransactionsPage(accountID string, number, size int64) (*models.CashTransactionsResponse, error) {
filter := "filter[currency-type eq]=USD"
apiURL := fmt.Sprintf("%s/cash-transactions?%s&page[number]=1&page[size]=20&include=account-cash-transfer-from,account-cash-transfer-to&sort=-created-at&account.id=%s",
_apiPrefix, url.PathEscape(filter), accountID)
color.Red("GetCashTransactionsPage:%v", apiURL)

req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
color.Red("error getting accountID:%s:%s", filter)
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
color.Red("wetf:%v", res.Status)
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
//color.Red("body:%s", body)

transactions := models.CashTransactionsResponse{}
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, errors.New("unmarshal error")
}
color.Red("GetCashTransactionsPage:%v", PrettyPrint(transactions))
return &transactions, nil
}

// GetCashTransactions returns all cash transactions between from and to
func GetCashTransactions(accountID string, from, to time.Time) (*models.CashTransactionsResponse, error) {
filter := fmt.Sprintf("filter[created-at gte]=%s&filter[created-at lte]=%s",
from.Format(time.RFC3339), to.Format(time.RFC3339))
color.Blue("filter:%s", filter)
apiUrl := fmt.Sprintf("%s/cash-transactions?%s&page[number]=1&page[size]=100&include=account-cash-transfer-from,account-cash-transfer-to&sort=-created-at&account.id=%s",
apiURL := fmt.Sprintf("%s/cash-transactions?%s&page[number]=1&page[size]=1000&include=account-cash-transfer-from,account-cash-transfer-to&sort=-created-at&account.id=%s",
_apiPrefix, url.PathEscape(filter), accountID)

//apiUrl = url.QueryEscape(apiUrl)
//apiURL = url.QueryEscape(apiURL)

req, err := http.NewRequest("GET", apiUrl, nil)
req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
Expand All @@ -75,7 +109,7 @@ func GetCashTransactions(accountID string, from, to time.Time) (*models.CashTran
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, errors.New("unmarshal error")
}

color.Red("GetTransactions:%v", PrettyPrint(transactions))
return &transactions, nil
}

Expand All @@ -87,6 +121,33 @@ func NewFundsTransfer(from, to, reference string, amount float64) *models.FundTr
return &ft
}

// GetFundsTransfer from funds-transfer-id
func GetFundsTransfer(fundsTransferID string) (*models.FundsTransfer, error) {
apiUrl := fmt.Sprintf("%s/funds-transfers/%s", _apiPrefix, fundsTransferID)
req, err := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)

response := models.FundsTransfer{}
if err := json.Unmarshal(body, &response); err != nil {
color.Red("GetFundsTransfer unmarshal error:%v", err)
return nil, errors.New("unmarshal error")
}

return &response, nil
}

// FundsTransfer ...
// trying a JSON parsing trick from https://stackoverflow.com/questions/35583735/unmarshaling-into-an-interface-and-then-performing-type-assertion
// because Included is an array of CashTransfer and CashTransaction
Expand Down
82 changes: 82 additions & 0 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/cloudmode/go-primetrust/models"
"github.com/fatih/color"
)

func GetContacts() (*models.ContactsResponse, error) {
Expand Down Expand Up @@ -36,6 +38,37 @@ func GetContacts() (*models.ContactsResponse, error) {
return &response, nil
}

func GetContactByEmail(email string) (*models.Contacts, error) {
//email = url.QueryEscape(email)
filter := url.QueryEscape("filter[email eq]")
email = url.QueryEscape(email)
apiURL := fmt.Sprintf("%s/contacts?include=account&%s=%s", _apiPrefix, filter, email)
//apiURL := fmt.Sprintf("%s/contacts?include=account&filter%5Bemail+eq%5D=stan%40f.co")
color.Green("GetContactByEmail apiURL:%v", apiURL)
req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)

response := models.Contacts{}
if err := json.Unmarshal(body, &response); err != nil {
color.Red("GetContactByEmail unmarshall error:%v", string(body))
return nil, errors.New("unmarshal error")
}

return &response, nil
}

func GetContact(contactId string) (*models.Contact, error) {
apiUrl := fmt.Sprintf("%s/contacts/%s", _apiPrefix, contactId)
req, err := http.NewRequest("GET", apiUrl, nil)
Expand Down Expand Up @@ -90,3 +123,52 @@ func CreateNewContact(contact *models.Contact) (*models.Contact, error) {

return &response, nil
}

func toContact(contactID string, rcd *models.RelatedContactData) *models.Contact {
contact := models.Contact{}
contact.Data.Type = "contacts"
contact.Data.Attributes = models.ContactAttributes{
ContactType: rcd.ContactType,
DateOfBirth: rcd.DateOfBirth,
Email: rcd.Email,
Name: rcd.Name,
Sex: rcd.Sex,
TaxIDNumber: rcd.TaxIDNumber,
TaxCountry: rcd.TaxCountry,
TaxState: rcd.TaxState,
}
contact.Data.Attributes.PrimaryPhoneNumber = rcd.PrimaryPhoneNumber
contact.Data.Attributes.PrimaryAddress = rcd.PrimaryAddress
return &contact
}

func UpdateContact(contactID string, contact *models.RelatedContactData) (*models.Contact, error) {
cData := toContact(contactID, contact)
jsonData := new(bytes.Buffer)
json.NewEncoder(jsonData).Encode(cData)

apiURL := fmt.Sprintf("%s/contacts/%s", _apiPrefix, contactID)
req, err := http.NewRequest("PATCH", apiURL, jsonData)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", _jwt)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
color.Green("UpdateContact: statusCode:%v", res.StatusCode)
color.Red("UpdateContact error response for %v: %v", apiURL, string(body))
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
}

response := models.Contact{}
if err := json.Unmarshal(body, &response); err != nil {
return nil, errors.New("unmarshal error")
}

return &response, nil
}
2 changes: 2 additions & 0 deletions documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path"

"github.com/cloudmode/go-primetrust/models"
"github.com/fatih/color"
"github.com/globalsign/mgo"
)

Expand Down Expand Up @@ -41,6 +42,7 @@ func UploadDocument(file *mgo.GridFile, fileHeader multipart.FileHeader, contact
"mime_type": contentType,
}

color.Green("UploadDocument data:%v", PrettyPrint(data))
for key, val := range data {
_ = writer.WriteField(key, val.(string))
}
Expand Down
21 changes: 15 additions & 6 deletions models/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,31 @@ type ContactData struct {

type RelatedContactData struct {
Type string `json:"type"`
ContactType string `json:"contact-type,omitempty"`
DateOfBirth string `json:"date-of-birth"`
Email string `json:"email"`
Name string `json:"name"`
Sex string `json:"sex"`
Label string `json:"label"`
TaxIDNumber string `json:"tax-id-number"`
TaxCountry string `json:"tax-country"`
PrimaryAddress Address `json:"primary-address"`
PrimaryPhoneNumber PhoneNumber `json:"primary-phone-number"`
ContactType string `json:"contact-type,omitempty"`
RegionOfFormation string `json:"region-of-formation,omitempty"`
DateOfBirth string `json:"date-of-birth,omitempty"`
Sex string `json:"sex,omitempty"`
TaxCountry string `json:"tax-country"`
TaxIDNumber string `json:"tax-id-number"`
TaxState string `json:"tax-state"`
Label string `json:"label"`
}

type Contact struct {
Data ContactData `json:"data"`
}

type Contacts struct {
Data []ContactData `json:"data"`
Included []AccountData `json:"included"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}

func NewNaturalPersonContact(accountId string) *Contact {
contact := Contact{
Data: ContactData{
Expand Down
19 changes: 19 additions & 0 deletions models/deposits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package models

type ContactFundsTransferReferences struct {
Data ContactFundsTransferReferenceData `json:"data"`
}

type ContactFundsTransferReferenceData struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Attributes ContactFundsTransferAttributes `json:"attributes"`
Links Links `json:"links"`
Relationships Relationships `json:"relationships"`
}

type ContactFundsTransferAttributes struct {
Reference string `json:"reference"`
AccountID string `json:"account-id,omitempty"`
ContactID string `json:"contact-id,omitempty"`
}
Loading

0 comments on commit 187bb4d

Please sign in to comment.