From 187bb4d7ec26db9c424cac3906586112229c3180 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 5 Oct 2020 10:19:53 -0700 Subject: [PATCH] new PrimeTrust api changes --- accounts.go | 14 ++-- cash-transactions.go | 69 ++++++++++++++++- contacts.go | 82 ++++++++++++++++++++ documents.go | 2 + models/contact.go | 21 ++++-- models/deposits.go | 19 +++++ models/disbursements.go | 156 +++++++++++++++++++++++++++++++++++++++ models/funds-transfer.go | 43 +++++++++++ models/relationships.go | 14 +++- sandbox.go | 1 + wires.go | 155 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 557 insertions(+), 19 deletions(-) create mode 100644 models/deposits.go create mode 100644 models/disbursements.go create mode 100644 models/funds-transfer.go create mode 100644 sandbox.go create mode 100644 wires.go diff --git a/accounts.go b/accounts.go index 9c35250..3bdab8e 100644 --- a/accounts.go +++ b/accounts.go @@ -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{} @@ -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) @@ -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{} @@ -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) diff --git a/cash-transactions.go b/cash-transactions.go index bd626fe..c1c4abd 100644 --- a/cash-transactions.go +++ b/cash-transactions.go @@ -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{} @@ -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 } @@ -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 diff --git a/contacts.go b/contacts.go index 1a43a90..ac1cff2 100644 --- a/contacts.go +++ b/contacts.go @@ -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) { @@ -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) @@ -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 +} diff --git a/documents.go b/documents.go index 1f95d91..05d39c9 100644 --- a/documents.go +++ b/documents.go @@ -13,6 +13,7 @@ import ( "path" "github.com/cloudmode/go-primetrust/models" + "github.com/fatih/color" "github.com/globalsign/mgo" ) @@ -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)) } diff --git a/models/contact.go b/models/contact.go index b802412..af8bd66 100644 --- a/models/contact.go +++ b/models/contact.go @@ -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{ diff --git a/models/deposits.go b/models/deposits.go new file mode 100644 index 0000000..dccc635 --- /dev/null +++ b/models/deposits.go @@ -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"` +} diff --git a/models/disbursements.go b/models/disbursements.go new file mode 100644 index 0000000..2b2027d --- /dev/null +++ b/models/disbursements.go @@ -0,0 +1,156 @@ +package models + +import ( + "strconv" + "time" + + "github.com/fatih/color" +) + +// Output +type Disbursements struct { + Data DisbursementsData `json:"data"` +} + +type DisbursementsData struct { + ID string `json:"id,omitempty"` + Type string `json:"type"` + Attributes DisbursementsAttributes `json:"attributes"` + Links Links `json:"links"` + Relationships Relationships `json:"relationships"` +} + +type DisbursementsAttributes struct { + Amount float64 `json:"amount"` + CreatedAt time.Time `json:"created-at" bson:"created-at"` + Description string `json:"description"` + CustomerReference string `json:"customer-reference" bson:"customer-reference"` + CurrencyType string `json:"currency-type"` + PaymentDetails string `json:"payment-details"` + ReferenceNumber string `json:"reference-number"` + SpecialType string `json:"special-type"` + Status string `json:"status"` + TransactionNumber string `json:"transaction-number"` +} + +// Input +type DisbursementsForm struct { + Data DisbursementsDefinition `json:"data"` +} + +type DisbursementsDefinition struct { + Type string `json:"type"` + Attributes DisbursementsFormAttributes `json:"attributes"` +} + +type DisbursementsFormAttributes struct { + AccountID string `json:"account-id"` + Amount float64 `json:"amount"` + Reference string `json:"reference,omitempty"` + CurrencyType string `json:"currencty-type"` + Description string `json:"description"` + FundsTransferMethodID string `json:"funds-transfer-method-id"` +} + +func NewDisbursementsForm(method *FundsTransferMethod, pargs *map[string]string) *DisbursementsForm { + args := *pargs + + f := DisbursementsForm{} + + var amount float64 + var err error + if amount, err = strconv.ParseFloat(args["amount"], 64); err != nil { + if amount, err = strconv.ParseFloat(args["Amount"], 64); err != nil { + color.Red("Invalid amount in pargs:%v", args["Amount"]) + return nil + } + } + + f.Data.Type = "disbursements" + f.Data.Attributes = DisbursementsFormAttributes{ + AccountID: args["accountID"], + Amount: amount, + Reference: args["reference"], + CurrencyType: "USD", + Description: args["Note"], + FundsTransferMethodID: method.Data.ID, + } + + return &f +} + +// Output +type FundsTransferMethod struct { + Data FundsTransferData `json:"data"` +} + +type FundsTransferData struct { + ID string `json:"id,omitempty"` + Type string `json:"type"` + Attributes FundsTransferAttributes `json:"attributes"` + Links Links `json:"links"` + Relationships Relationships `json:"relationships"` + Included interface{} `json:"included"` +} + +type FundsTransferAttributes struct { + BankAccountName string `json:"bank-account-name"` + BankAccountType string `json:"bank-account-type"` + BankName string `json:"bank-name"` + ContactEmail string `json:"contact-email"` + ContactName string `json:"contact-name"` + RoutingNumber string `json:"routing-number"` +} + +// Input +type FundsTransferForm struct { + Data FundsTransferDefinition `json:"data"` +} + +type FundsTransferDefinition struct { + Type string `json:"type"` + Attributes FundsTransferFormAttributes `json:"attributes"` +} + +type FundsTransferFormAttributes struct { + BankAccountName string `json:"bank-account-name"` + BankAccountNumber string `json:"bank-account-number"` + BankName string `json:"bank-name"` + ContactEmail string `json:"contact-email"` + ContactName string `json:"contact-name"` + RoutingNumber string `json:"routing-number"` + FundsTransferType string `json:"funds-transfer-type"` + FurtherCreditAccountName string `json:"further-credit-account-name"` + FurtherCreditAccountNumber string `json:"further-credit-account-number"` + BeneficiaryAddress Address `json:"beneficiary-address"` +} + +func NewFundsTransferForm(pargs *map[string]string) *FundsTransferForm { + f := FundsTransferForm{} + f.Data.Type = "funds-transfer-method" + args := *pargs + + color.Red("NewFundsTransferForm: \n\tpargs:%v \n\t args:%v", pargs, args) + + addr := Address{ + Street1: args["Street"], + City: args["City"], + Region: args["State"], + Country: "US", + PostalCode: args["Zipcode"], + } + + f.Data.Attributes = FundsTransferFormAttributes{ + BeneficiaryAddress: addr, + ContactEmail: args["email"], + ContactName: args["contactName"], + BankName: args["DepositoryBankName"], + BankAccountName: args["AccountName"], + BankAccountNumber: args["AccountNumber"], + RoutingNumber: args["RoutingNumber"], + FundsTransferType: "wire", + FurtherCreditAccountName: "intendedUse", + FurtherCreditAccountNumber: args["FurtherCredit"], + } + return &f +} diff --git a/models/funds-transfer.go b/models/funds-transfer.go new file mode 100644 index 0000000..7d27882 --- /dev/null +++ b/models/funds-transfer.go @@ -0,0 +1,43 @@ +package models + +import "time" + +type FundsTransfer struct { + Data FundsTransferData2 `json:"data"` + Included []ContactData `json:"included"` +} + +type FundsTransferData2 struct { + ID string `json:"id,omitempty"` + Type string `json:"type"` + Attributes FundsTransferAttributes2 `json:"attributes"` + Links Links `json:"links"` + Relationships Relationships `json:"relationships"` +} + +type FundsTransferAttributes2 struct { + Amount float64 `json:"amount"` + AmountExpected float64 `json:"amount-expected"` + CancelledAt string `json:"cancelled-at"` + ClearsOn string `json:"clears-on"` + CreatedAt time.Time `json:"created-at"` + ContingenciesClearedAt time.Time `json:"contingencies-cleared-at"` + ContingenciesClearedOn string `json:"contingencies-cleared-on"` + CurrencyType string `json:"currency-type,omitempty"` + EqualityHash string `json:"equality-hash"` + FundsSourceName string `json:"funds-source-name"` + FundsSourceType string `json:"funds-source-type"` + PrivateMemo string `json:"private-memo"` + Reference string `json:"reference"` + ReversedAmount float64 `json:"reversed-amount"` + ReversalDetails string `json:"reversal-details"` + ReversedAt time.Time `json:"reversed-at"` + SettledAt time.Time `json:"settled-at"` + SettlementDetails string `json:"settlement-details"` + SignetDepositAddress string `json:"signet-deposit-address"` + SpecialInstructions string `json:"special-instructions"` + SpecialType string `json:"special-type"` + Status string `json:"status"` + UpdatedAt time.Time `json:"updated-at"` + WireInstructions string `json:"wire-instructions"` +} diff --git a/models/relationships.go b/models/relationships.go index 3db6247..1168d90 100644 --- a/models/relationships.go +++ b/models/relationships.go @@ -17,7 +17,7 @@ type Relationship2 struct { type Relationships struct { Accounts Relationship `json:"accounts,omitempty"` - Account Relationship `json:"account,omitempty"` + Account Relationship2 `json:"account,omitempty"` Contacts Relationship `json:"contacts,omitempty"` Contact Relationship `json:"contact,omitempty"` AccountType Relationship2 `json:"account-type,omitempty"` @@ -26,9 +26,10 @@ type Relationships struct { CIPChecks Relationship `json:"cip-checks,omitempty"` Contributions Relationship `json:"contributions,omitempty"` Currency Relationship `json:"currency,omitempty"` - Disbursements Relationship `json:"disbursements,omitempty"` + Disbursement Relationship2 `json:"disbursement,omitempty"` + DisbursementAuthorization Relationship2 `json:"disbursement-authorization,omitempty"` FromContactRelationships Relationship `json:"from-contact-relationships,omitempty"` - PaymentMethods Relationship `json:"payment-methods,omitempty"` + PaymentMethod Relationship2 `json:"payment-method,omitempty"` PhoneNumbers Relationship `json:"phone-numbers,omitempty"` UploadedDocuments Relationship `json:"uploaded-documents,omitempty"` RelatedFromContacts Relationship `json:"related-from-contacts,omitempty"` @@ -53,9 +54,14 @@ type Relationships struct { Beneficiaries Relationship `json:"beneficiaries,omitempty"` Grantors Relationship `json:"grantors,omitempty"` OwnersAndGrantors Relationship `json:"owners-and-grantors,omitempty"` - FundsTransfers Relationship `json:"funds-transfers,omitempty"` + FundsTransfer Relationship2 `json:"funds-transfer,omitempty"` + FundsTransferMethod Relationship2 `json:"funds-transfer-method,omitempty"` Organization Relationship `json:"organization,omitempty"` FromCashTransaction Relationship2 `json:"from-cash-transaction,omitempty"` ToCashTransaction Relationship2 `json:"to-cash-transaction,omitempty"` WebhookConfig Relationship2 `json:"webhook-config,omitempty"` + SettledCashTransaction Relationship `json:"settled-cash-transaction,omitempty"` + ReversedCashTransaction Relationship `json:"reversed-cash-transaction,omitempty"` + Parent Relationship2 `json:"parent,omitempty"` + Refund Relationship2 `json:"refund,omitempty"` } diff --git a/sandbox.go b/sandbox.go new file mode 100644 index 0000000..bd8f0d5 --- /dev/null +++ b/sandbox.go @@ -0,0 +1 @@ +package primetrust diff --git a/wires.go b/wires.go new file mode 100644 index 0000000..4bdf76d --- /dev/null +++ b/wires.go @@ -0,0 +1,155 @@ +package primetrust + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net/http" + + "github.com/cloudmode/go-primetrust/models" + "github.com/fatih/color" +) + +// FundsTransferMethod ... +func FundsTransferMethod(method *models.FundsTransferForm) (*models.FundsTransferMethod, error) { + jsonData := new(bytes.Buffer) + json.NewEncoder(jsonData).Encode(method) + + apiURL := fmt.Sprintf("%s/funds-transfer-methods?include=bank", _apiPrefix) + + color.Red("FundsTransferMethod:apiUrl:%v", apiURL) + + req, err := http.NewRequest("POST", 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.StatusCreated { + return nil, fmt.Errorf("%s: %s", res.Status, string(body)) + } + + response := models.FundsTransferMethod{} + + if err := json.Unmarshal(body, &response); err != nil { + color.Black("%v", string(body)) + return nil, err + } + + color.Red("FundsTransferMethod:response:%+v", response) + + return &response, nil +} + +func GetDisbursement(disbursementID string) (*models.Disbursements, error) { + apiURL := fmt.Sprintf("%s/disbursements/%s?include=funds-transfer", _apiPrefix, disbursementID) + 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.Disbursements{} + if err := json.Unmarshal(body, &response); err != nil { + color.Red("GetDisbursement:error:%v", err) + return nil, errors.New("unmarshal error") + } + + return &response, nil +} + +func Disbursement(disburse *models.DisbursementsForm) (*models.Disbursements, error) { + jsonData := new(bytes.Buffer) + json.NewEncoder(jsonData).Encode(disburse) + + apiURL := fmt.Sprintf("%s/disbursements", _apiPrefix) + + color.Red("Disbursement:apiUrl:%v", apiURL) + + req, err := http.NewRequest("POST", 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.StatusCreated { + return nil, fmt.Errorf("%s: %s", res.Status, string(body)) + } + + response := models.Disbursements{} + + if err := json.Unmarshal(body, &response); err != nil { + color.Black("%v", string(body)) + return nil, err + } + + color.Red("Disbursement:response:%+v", response) + + return &response, nil +} + +func FundsTransferReference(accountID, contactID string) (*models.ContactFundsTransferReferences, error) { + ft := models.ContactFundsTransferReferences{} + ft.Data.Attributes.AccountID = accountID + ft.Data.Attributes.ContactID = contactID + ft.Data.Type = "contact-funds-transfer-references" + jsonData := new(bytes.Buffer) + json.NewEncoder(jsonData).Encode(ft) + + apiURL := fmt.Sprintf("%s/contact-funds-transfer-references", _apiPrefix) + + color.Red("FundsTransferReference:apiUrl:%v", apiURL) + + req, err := http.NewRequest("POST", 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.StatusCreated { + return nil, fmt.Errorf("%s: %s", res.Status, string(body)) + } + response := models.ContactFundsTransferReferences{} + if err := json.Unmarshal(body, &response); err != nil { + color.Black("%v", string(body)) + return nil, err + } + + color.Red("FundsTransferReference:response:%+v", response) + + return &response, nil + + return nil, nil +}