telebot/payments.go

177 lines
5.7 KiB
Go
Raw Normal View History

2019-01-27 19:00:48 +00:00
package telebot
2019-01-28 00:11:10 +00:00
import (
"encoding/json"
"math"
2022-10-05 12:04:31 +00:00
"strconv"
"strings"
2019-01-28 00:11:10 +00:00
)
2020-05-20 20:40:42 +00:00
// ShippingQuery contains information about an incoming shipping query.
type ShippingQuery struct {
Sender *User `json:"from"`
ID string `json:"id"`
Payload string `json:"invoice_payload"`
Address ShippingAddress `json:"shipping_address"`
}
2019-01-28 00:11:10 +00:00
2020-05-20 20:40:42 +00:00
// ShippingAddress represents a shipping address.
type ShippingAddress struct {
CountryCode string `json:"country_code"`
State string `json:"state"`
City string `json:"city"`
StreetLine1 string `json:"street_line1"`
StreetLine2 string `json:"street_line2"`
PostCode string `json:"post_code"`
}
2019-01-28 00:11:10 +00:00
2020-05-20 20:40:42 +00:00
// ShippingOption represents one shipping option.
type ShippingOption struct {
ID string `json:"id"`
Title string `json:"title"`
Prices []Price `json:"prices"`
}
2019-01-28 00:11:10 +00:00
// Payment contains basic information about a successful payment.
type Payment struct {
Currency string `json:"currency"`
Total int `json:"total_amount"`
Payload string `json:"invoice_payload"`
OptionID string `json:"shipping_option_id"`
Order Order `json:"order_info"`
TelegramChargeID string `json:"telegram_payment_charge_id"`
ProviderChargeID string `json:"provider_payment_charge_id"`
}
2020-05-20 20:40:42 +00:00
// PreCheckoutQuery contains information about an incoming pre-checkout query.
type PreCheckoutQuery struct {
Sender *User `json:"from"`
ID string `json:"id"`
2019-01-28 00:11:10 +00:00
Currency string `json:"currency"`
2020-05-20 20:40:42 +00:00
Payload string `json:"invoice_payload"`
Total int `json:"total_amount"`
OptionID string `json:"shipping_option_id"`
Order Order `json:"order_info"`
}
2019-01-28 00:11:10 +00:00
2020-05-20 20:40:42 +00:00
// Order represents information about an order.
type Order struct {
Name string `json:"name"`
PhoneNumber string `json:"phone_number"`
Email string `json:"email"`
Address ShippingAddress `json:"shipping_address"`
}
2020-05-13 19:41:25 +00:00
2020-05-20 20:40:42 +00:00
// Invoice contains basic information about an invoice.
type Invoice struct {
Title string `json:"title"`
Description string `json:"description"`
Payload string `json:"payload"`
Currency string `json:"currency"`
Prices []Price `json:"prices"`
Token string `json:"provider_token"`
Data string `json:"provider_data"`
2020-05-20 20:40:42 +00:00
Photo *Photo `json:"photo"`
PhotoSize int `json:"photo_size"`
// Unique deep-linking parameter that can be used to
// generate this invoice when used as a start parameter (0).
2020-05-20 20:40:42 +00:00
Start string `json:"start_parameter"`
2020-05-13 19:41:25 +00:00
// Shows the total price in the smallest units of the currency.
2020-05-20 20:40:42 +00:00
// For example, for a price of US$ 1.45 pass amount = 145.
Total int `json:"total_amount"`
2020-05-13 19:41:25 +00:00
MaxTipAmount int `json:"max_tip_amount"`
SuggestedTipAmounts []int `json:"suggested_tip_amounts"`
2020-05-13 19:41:25 +00:00
NeedName bool `json:"need_name"`
NeedPhoneNumber bool `json:"need_phone_number"`
NeedEmail bool `json:"need_email"`
NeedShippingAddress bool `json:"need_shipping_address"`
2020-05-20 20:40:42 +00:00
SendPhoneNumber bool `json:"send_phone_number_to_provider"`
SendEmail bool `json:"send_email_to_provider"`
Flexible bool `json:"is_flexible"`
2019-01-28 00:11:10 +00:00
}
2022-10-05 12:04:31 +00:00
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
}
2020-05-20 20:40:42 +00:00
// Price represents a portion of the price for goods or services.
2019-01-28 00:11:10 +00:00
type Price struct {
Label string `json:"label"`
Amount int `json:"amount"`
}
2019-01-27 19:00:48 +00:00
2020-05-20 20:40:42 +00:00
// Currency contains information about supported currency for payments.
2019-01-27 19:00:48 +00:00
type Currency struct {
2019-01-28 00:11:10 +00:00
Code string `json:"code"`
Title string `json:"title"`
Symbol string `json:"symbol"`
Native string `json:"native"`
ThousandsSep string `json:"thousands_sep"`
DecimalSep string `json:"decimal_sep"`
SymbolLeft bool `json:"symbol_left"`
SpaceBetween bool `json:"space_between"`
Exp int `json:"exp"`
MinAmount interface{} `json:"min_amount"`
MaxAmount interface{} `json:"max_amount"`
2019-01-27 19:00:48 +00:00
}
2019-01-28 00:11:10 +00:00
func (c Currency) FromTotal(total int) float64 {
return float64(total) / math.Pow(10, float64(c.Exp))
}
func (c Currency) ToTotal(total float64) int {
return int(total) * int(math.Pow(10, float64(c.Exp)))
}
var SupportedCurrencies = make(map[string]Currency)
2019-01-27 19:00:48 +00:00
func init() {
2020-05-20 20:40:42 +00:00
err := json.Unmarshal([]byte(dataCurrencies), &SupportedCurrencies)
if err != nil {
panic(err)
}
2019-01-27 19:00:48 +00:00
}