Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
vekotov committed Aug 30, 2021
1 parent 3c0f752 commit f9679e3
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 35 deletions.
89 changes: 70 additions & 19 deletions bill.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,95 @@
package qiwiP2P

import (
"encoding/json"
"fmt"
"time"
)

type Bill struct {
Currency string
Value float32
Comment string
ExpirationDateTime time.Time
CustomerPhone string
CustomerEmail string
CustomerAccount string
CustomFields map[string]string
Amount Amount `json:"amount"`
Comment string `json:"comment"`
ExpirationDateTime string `json:"expirationDateTime"`
Customer Customer `json:"customer"`
CustomFields map[string]string `json:"customFields"`
}

type Amount struct {
Currency string `json:"currency"`
Value string `json:"value"`
}

type Status struct {
Value string `json:"value"`
ChangedDateTime string `json:"changedDateTime"`
}

type Customer struct {
Phone string `json:"phone"`
Email string `json:"email"`
Account string `json:"account"`
}

func CreateBill() *Bill {
return &Bill{
Currency: "RUB",
Value: 1,
ExpirationDateTime: time.Now().UTC().Add(time.Hour * 3),
Amount: Amount{},
Customer: Customer{},
ExpirationDateTime: time.Now().UTC().Add(time.Hour * 3).Format("2006-01-02T15:01:05+00:00"),
CustomFields: make(map[string]string),
}
}

func (b *Bill) SetTheme(theme string) *Bill {
b.CustomFields["themeCode"] = theme
return b
}

func (b *Bill) SetPaySourcesFilter(filter string) *Bill {
b.CustomFields["paySourcesFilter"] = filter
return b
}

func (b *Bill) SetCurrency(currency string) *Bill {
b.Currency = currency
b.Amount.Currency = currency
return b
}

func (b *Bill) SetValue(value float32) *Bill {
b.Value = value
b.Amount.Value = fmt.Sprintf("%.2f", value)
return b
}

func (b *Bill) SetComment(comment string) *Bill {
b.Comment = comment
return b
}

func (b *Bill) SetExpirationDateTime(time time.Time) *Bill {
b.ExpirationDateTime = time.UTC().Format("2006-01-02T15:01:05+00:00")
return b
}

func (b *Bill) SetCustomerPhone(phone string) *Bill {
b.Customer.Phone = phone
return b
}

func (b *Bill) SetCustomerEmail(email string) *Bill {
b.Customer.Email = email
return b
}

func (b *Bill) SetCustomerAccount(account string) *Bill {
b.Customer.Account = account
return b
}

func (b *Bill) SetCustomField(field string, value string) *Bill {
b.CustomFields[field] = value
return b
}

func (b *Bill) toJSON() string {
return fmt.Sprintf(
"{\"amount\":{\"currency\":\"%s\",\"value\": \"%.2f\"}," +
"\"expirationDateTime\": \"%s\"}",
b.Currency,
b.Value,
b.ExpirationDateTime.Format("2006-01-02T15:01:05+00:00"))
arr, _ := json.Marshal(b)
return string(arr)
}
23 changes: 23 additions & 0 deletions billResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package qiwiP2P

type BillResponse struct {
SiteId string `json:"siteId"`
BillId string `json:"billId"`
Amount Amount `json:"amount"`
Status Status `json:"status"`
Customer Customer `json:"customer"`
CustomFields map[string]string `json:"customFields"`
Comment string `json:"comment"`
CreationDateTime string `json:"creationDateTime"`
ExpirationDateTime string `json:"expirationDateTime"`
PayUrl string `json:"payUrl"`
}

type RequestError struct {
ServiceName string `json:"serviceName"`
ErrorCode string `json:"errorCode"`
Description string `json:"description"`
UserMessage string `json:"userMessage"`
DateTime string `json:"dateTime"`
TraceId string `json:"traceId"`
}
72 changes: 59 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qiwiP2P

import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -22,24 +23,69 @@ func (c *Client) SetSecretKey(key string) *Client {
return c
}

func (c *Client) PutBill(b *Bill) (json string, retErr error) {
func (c *Client) PutBill(b *Bill) (result *BillResponse, error *RequestError) {
billId := pseudoUUID()
req, err := http.NewRequest(
"PUT", "https://api.qiwi.com/partner/bill/v1/bills/"+billId, strings.NewReader(b.toJSON()),
)
if err != nil {
return "", err
res, code := c.makeRequest("https://api.qiwi.com/partner/bill/v1/bills/"+billId, "PUT", b.toJSON())

if code == 401 {
return nil, &RequestError{ErrorCode: "bad_token", Description: "Bad token"}
}
req.Header.Add("Authorization", "Bearer "+c.token)
req.Header.Add("User-Agent", "GoQiwiP2P")
res, err := c.client.Do(req)
if err != nil {
return "", err

return parseResponse(res)
}

func (c *Client) GetBill(id string) (result *BillResponse, error *RequestError) {
res, code := c.makeRequest("https://api.qiwi.com/partner/bill/v1/bills/"+id, "GET", "")

if code == 401 {
return nil, &RequestError{ErrorCode: "bad_token", Description: "Bad token"}
}
if code == 404 {
return nil, &RequestError{ErrorCode: "bad_id", Description: "No such bill found"}
}

return parseResponse(res)
}

func (c *Client) RejectBill(id string) (result *BillResponse, error *RequestError) {
res, code := c.makeRequest("https://api.qiwi.com/partner/bill/v1/bills/"+id+"/reject", "POST", "")

if code == 401 {
return nil, &RequestError{ErrorCode: "bad_token", Description: "Bad token"}
}
if code == 404 {
return nil, &RequestError{ErrorCode: "bad_id", Description: "No such bill found"}
}

return parseResponse(res)
}

func parseResponse(jsonResponse string) (result *BillResponse, error *RequestError) {
var re RequestError
json.Unmarshal([]byte(jsonResponse), &error)
if re.ErrorCode != "" {
return nil, &re
}

var response BillResponse
json.Unmarshal([]byte(jsonResponse), &response)

return &response, nil
}

func (c *Client) makeRequest(url string, method string, data string) (json string, code int) {
req, _ := http.NewRequest(
method, url, strings.NewReader(data),
)

req.Header.Add("Authorization", "Bearer "+c.token)
req.Header.Add("content-type", "application/json")
res, _ := c.client.Do(req)

defer res.Body.Close()
buf := new(strings.Builder)
io.Copy(buf, res.Body)

return buf.String(), nil
return buf.String(), res.StatusCode
}

func pseudoUUID() (uuid string) {
Expand Down
3 changes: 0 additions & 3 deletions createBillResponse.go

This file was deleted.

0 comments on commit f9679e3

Please sign in to comment.