Skip to content

Commit

Permalink
payments: implement createInvoiceLink
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Oct 5, 2022
1 parent adf6dc8 commit d9b2c62
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
15 changes: 15 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,21 @@ func (b *Bot) StopPoll(msg Editable, opts ...interface{}) (*Poll, error) {
return resp.Result, nil
}

func (b *Bot) CreateInvoice(i Invoice) (string, error) {
data, err := b.Raw("createInvoiceLink", i.params())
if err != nil {
return "", err
}

var resp struct {
Result string
}
if err := json.Unmarshal(data, &resp); err != nil {
return "", wrapError(err)
}
return resp.Result, nil
}

// InviteLink should be used to export chat's invite link.
func (b *Bot) InviteLink(chat *Chat) (string, error) {
params := map[string]string{
Expand Down
44 changes: 44 additions & 0 deletions payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package telebot
import (
"encoding/json"
"math"
"strconv"
"strings"
)

// ShippingQuery contains information about an incoming shipping query.
Expand Down Expand Up @@ -93,6 +95,48 @@ type Invoice struct {
Flexible bool `json:"is_flexible"`
}

func (i Invoice) params() map[string]string {
params := map[string]string{
"title": i.Title,
"description": i.Description,
"start_parameter": i.Start,
"payload": i.Payload,
"provider_token": i.Token,
"provider_data": i.Data,
"currency": i.Currency,
"max_tip_amount": strconv.Itoa(i.MaxTipAmount),
"need_name": strconv.FormatBool(i.NeedName),
"need_phone_number": strconv.FormatBool(i.NeedPhoneNumber),
"need_email": strconv.FormatBool(i.NeedEmail),
"need_shipping_address": strconv.FormatBool(i.NeedShippingAddress),
"send_phone_number_to_provider": strconv.FormatBool(i.SendPhoneNumber),
"send_email_to_provider": strconv.FormatBool(i.SendEmail),
"is_flexible": strconv.FormatBool(i.Flexible),
}
if i.Photo != nil {
if i.Photo.FileURL != "" {
params["photo_url"] = i.Photo.FileURL
}
if i.PhotoSize > 0 {
params["photo_size"] = strconv.Itoa(i.PhotoSize)
}
if i.Photo.Width > 0 {
params["photo_width"] = strconv.Itoa(i.Photo.Width)
}
if i.Photo.Height > 0 {
params["photo_height"] = strconv.Itoa(i.Photo.Height)
}
}
if len(i.Prices) > 0 {
data, _ := json.Marshal(i.Prices)
params["prices"] = string(data)
}
if len(i.SuggestedTipAmounts) > 0 {
params["suggested_tip_amounts"] = "[" + strings.Join(intsToStrs(i.SuggestedTipAmounts), ",") + "]"
}
return params
}

// Price represents a portion of the price for goods or services.
type Price struct {
Label string `json:"label"`
Expand Down
41 changes: 2 additions & 39 deletions sendable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"path/filepath"
"strconv"
"strings"
)

// Recipient is any possible endpoint you can send
Expand Down Expand Up @@ -319,44 +318,8 @@ func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {

// Send delivers invoice through bot b to recipient.
func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"title": i.Title,
"description": i.Description,
"start_parameter": i.Start,
"payload": i.Payload,
"provider_token": i.Token,
"currency": i.Currency,
"max_tip_amount": strconv.Itoa(i.MaxTipAmount),
"need_name": strconv.FormatBool(i.NeedName),
"need_phone_number": strconv.FormatBool(i.NeedPhoneNumber),
"need_email": strconv.FormatBool(i.NeedEmail),
"need_shipping_address": strconv.FormatBool(i.NeedShippingAddress),
"send_phone_number_to_provider": strconv.FormatBool(i.SendPhoneNumber),
"send_email_to_provider": strconv.FormatBool(i.SendEmail),
"is_flexible": strconv.FormatBool(i.Flexible),
}
if i.Photo != nil {
if i.Photo.FileURL != "" {
params["photo_url"] = i.Photo.FileURL
}
if i.PhotoSize > 0 {
params["photo_size"] = strconv.Itoa(i.PhotoSize)
}
if i.Photo.Width > 0 {
params["photo_width"] = strconv.Itoa(i.Photo.Width)
}
if i.Photo.Height > 0 {
params["photo_height"] = strconv.Itoa(i.Photo.Height)
}
}
if len(i.Prices) > 0 {
data, _ := json.Marshal(i.Prices)
params["prices"] = string(data)
}
if len(i.SuggestedTipAmounts) > 0 {
params["suggested_tip_amounts"] = "[" + strings.Join(intsToStrs(i.SuggestedTipAmounts), ",") + "]"
}
params := i.params()
params["chat_id"] = to.Recipient()
b.embedSendOptions(params, opt)

data, err := b.Raw("sendInvoice", params)
Expand Down

0 comments on commit d9b2c62

Please sign in to comment.