Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dearjohndoe authored and arterialist committed Apr 3, 2023
1 parent cfc0810 commit 7600f3d
Show file tree
Hide file tree
Showing 8 changed files with 568 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/check-is-invoice-paid/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"fmt"
"log"
tonpaygo "tonpay-go-sdk/src"

"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/ton"
)

func CheckIsInvoicePaid(addr *address.Address) bool {
// First of all, we need to create connection pool
client := liteclient.NewConnectionPool()

// Choose testnet or mainnet
// configUrl := "https://ton-blockchain.github.io/global-config.json" // mainnet
configUrl := "https://ton-blockchain.github.io/testnet-global.config.json" // testnet
err := client.AddConnectionsFromConfigUrl(context.Background(), configUrl)
if err != nil {
log.Println(err)
return false
}

api := ton.NewAPIClient(client)

// Get block
block, err := api.CurrentMasterchainInfo(context.Background())
if err != nil {
log.Println(err)
return false
}

isPaid, err := tonpaygo.IsPaid(api, block, addr)
if err != nil {
fmt.Println(err)
return false
}

return isPaid
}

func main() {
// This one paid
addr := address.MustParseAddr("EQCPibUxBk7xwwcKBja6SbC5Pm1BCihGDA6SY5wTmFhrmYSh")
isPaid := CheckIsInvoicePaid(addr)
fmt.Println(isPaid)

// This one not
addr2 := address.MustParseAddr("EQAO4tDMxk1NX6BXJt_LpzHSxfOGwX45M3VfL5Yqu-exS7S6")
isPaid2 := CheckIsInvoicePaid(addr2)
fmt.Println(isPaid2)
}
24 changes: 24 additions & 0 deletions examples/create-and-pay-invoice-url/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
tonpaygo "tonpay-go-sdk/src"

"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
)

func main() {
storeAddr := address.MustParseAddr("EQCA_bhKoqvxxG44_fBDC4VFQ36Sdzu6i8KaD2SMaZwWS_N9")
amount := tlb.MustFromTON("1").NanoTON().Uint64()

requestCell, err := tonpaygo.RequestPurchaseMessage("created using tonpaygo, paid by wallet", amount)
if err != nil {
fmt.Println(err)
return
}

url := tonpaygo.BuildURLForWallet(storeAddr.String(), requestCell, amount, tonpaygo.RequestPurchaseFee)

fmt.Printf("Send this url to the customer: %s", url)
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module tonpay-go-sdk

go 1.19

require (
github.com/oasisprotocol/curve25519-voi v0.0.0-20220328075252-7dd334e3daae // indirect
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 // indirect
github.com/xssnick/tonutils-go v1.6.2 // indirect
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
golang.org/x/sys v0.0.0-20220325203850-36772127a21f // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/oasisprotocol/curve25519-voi v0.0.0-20220328075252-7dd334e3daae h1:7smdlrfdcZic4VfsGKD2ulWL804a4GVphr4s7WZxGiY=
github.com/oasisprotocol/curve25519-voi v0.0.0-20220328075252-7dd334e3daae/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97McgDGT5zcMrQoi0EICZs8Pgchs=
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
github.com/xssnick/tonutils-go v1.6.2 h1:K8Kp2pQ9n8i+73gCepcdf0GJnTK826ZxGWjQk4l0i4I=
github.com/xssnick/tonutils-go v1.6.2/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20220325203850-36772127a21f h1:TrmogKRsSOxRMJbLYGrB4SBbW+LJcEllYBLME5Zk5pU=
golang.org/x/sys v0.0.0-20220325203850-36772127a21f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6 changes: 6 additions & 0 deletions src/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tonpaygo

const (
InvoiceCode = "te6cckECIAEAA6IAART/APSkE/S88sgLAQIBYhcCAgEgDgMCASAJBAIBIAYFADW0mZ2omh9IH0gfSBpAOpqaZ/pAOkAmAgcL4RACASAIBwA1s2o7UTQ+kD6QPpA0gHU1NM/0gHSATAQeF8IgADWypztRND6QPpA+kDSAdTU0z/SAdIBMBBIXwiACASANCgIBSAwLAC+vbXaiaH0gfSB9IGkA6mppn+kA6QCYOkAAB67COkAAMbZ/PaiaH0gfSB9IGkA6mppn+kA6QCYL4RACASAWDwIBIBMQAgEgEhEANbO0O1E0PpA+kD6QNIB1NTTP9IB0gEwEFhfCIAAxsSZ7UTQ+kD6QPpA0gHU1NM/0gHSATBsgYAIBIBUUADOygbtRND6QPpA+kDSAdTU0z/SAdIBMBhfCIAA1s9L7UTQ+kD6QPpA0gHU1NM/0gHSATAQaF8IgADW5BB7UTQ+kD6QPpA0gHU1NM/0gHSATAQKF8IgCAs4ZGAA3TIUAnPFlAHzxZQBc8WE8oBzMzLP8oBygHJ7VSAS7QzIscAjhxsEtDTAzH6QDBwgBDIywVYzxZY+gLLasmDBvsA4NDTAwFxsJJfA+D6QDAC0x/TPzEhghD1OgLTuuMCMiCCEEjFBPO64wIgghAcwLEeuuMCIIIQwoWVL7qB0cGxoA2o4qW+1E0PpA+kD6QNIB1NTTP9IB0gEwkl8J4FGGxwWzlYEPAPLw3lUGf/AC4IIQYb3fi7qOMe1E0PpA+kD6QNIB1NTTP9IB0gEwEGhfCBLHBbOVgQ8A8vDe1NIBAZTUMO1UkTDi+wTgW4QP8vAAVlvtRND6QPpA+kDSAdTU0z/SAdIBMLOSXwngUYbHBbOVgQ8A8vDeVQZw8AIAiDDtRND6QPpA+kDSAdTU0z/SAdIBMGxCILOWggDwAfLw3iGWggDwBfLw3lFjxwWzlYEPAPLw3gTSAdTU0z8wEGhVFfACAvxb7UTQ+kD6QPpA0gHU1NM/0gHSATABf7CWggDwAvLw3lOBuZaCAPAD8vDeBI4VItBSkMcFs1OXxwWzsJaCAPAE8vDe3iCAZKkEp2JwgBDIywUnzxZdofoCy2rJcfsAcCCAEMjLBSnPFlAD+gISy2rLHyLQzxbJU4G8koMG4w0fHgCc+wBTaMcFkgLQkjIn4lNyvI4lcCCAEMjLBVADzxZRlKEZ+gLLahfLH4tmNoYW5nZYzxbJgwb7AJIwNuJ/yFAIzxbJJxBoEFdQBAYFA/ACAAJwQXVxzA=="
StoreCode = "te6ccgEBCAEAhgABFP8A9KQT9LzyyAsBAgEgAgMCAUgEBQCW8oMI1xgg0x/TH9MfAvgju/Jj7UTQ0x/TH9P/0VEyuvKhUUS68qIE+QFUEFX5EPKj+ACTINdKltMH1AL7AOgwAaTIyx/LH8v/ye1UAATQMAIBSAYHABe7Oc7UTQ0z8x1wv/gAEbjJftRNDXCx+A=="
)
150 changes: 150 additions & 0 deletions src/invoice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package tonpaygo

import (
"context"

"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
)

var (
DeployInvoiceFee = tlb.MustFromTON("0.005").NanoTON().Uint64()
EditInvoiceFee = tlb.MustFromTON("0.005").NanoTON().Uint64()
ActiveInvoiceFee = tlb.MustFromTON("0.005").NanoTON().Uint64()
DeactiveInvoiceFee = tlb.MustFromTON("0.005").NanoTON().Uint64()
)

type InvoiceData struct {
Store string
Merchant string
Beneficiary string
HasCustomer bool
Customer string
InvoiceID string
Amount uint64
Paid bool
Active bool
Version int64
}

func GetStore(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (*address.Address, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_store")
if err != nil {
return nil, err
}

owner := res.MustSlice(0).MustLoadAddr()
return owner, nil
}

func GetMerchant(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (*address.Address, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_merchant")
if err != nil {
return nil, err
}

owner := res.MustSlice(0).MustLoadAddr()
return owner, nil
}

func GetCustomer(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (*address.Address, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_customer")
if err != nil {
return nil, err
}

customer := res.MustSlice(0).MustLoadAddr()
return customer, nil
}

func HasCustomer(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (bool, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_has_customer")
if err != nil {
return false, err
}

hasCustomer := res.MustInt(0).Int64()
return hasCustomer == -1, nil
}

func GetInvoiceID(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (string, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_id")
if err != nil {
return "", err
}

id := res.MustCell(0).BeginParse().MustLoadStringSnake()
return id, nil
}

func GetAmount(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (uint64, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_amount")
if err != nil {
return 0, err
}

amount := res.MustInt(0)
return amount.Uint64(), nil
}

func IsPaid(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (bool, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_paid")
if err != nil {
return false, err
}

isPaid := res.MustInt(0).Int64()
return isPaid == -1, nil
}

func IsActive(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (bool, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_active")
if err != nil {
return false, err
}

isActive := res.MustInt(0).Int64()
return isActive == -1, nil
}

func GetInvoiceVersion(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (uint64, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_version")
if err != nil {
return 0, err
}

version := res.MustInt(0)
return version.Uint64(), nil
}

func GetInvoiceData(api *ton.APIClient, block *ton.BlockIDExt, addr *address.Address) (InvoiceData, error) {
res, err := api.RunGetMethod(context.Background(), block, addr, "get_invoice_data")
if err != nil {
return InvoiceData{}, err
}

store := res.MustSlice(0).MustLoadAddr()
merchant := res.MustSlice(1).MustLoadAddr()
beneficiary := res.MustSlice(2).MustLoadAddr()
hasCustomer := res.MustInt(3)
customer := res.MustCell(4).BeginParse().MustLoadAddr()
invoiceID := res.MustCell(5).BeginParse().MustLoadStringSnake()
amount := res.MustInt(6)
paid := res.MustInt(7)
active := res.MustInt(8)
version := res.MustInt(9)

return InvoiceData{
Store: store.String(),
Merchant: merchant.String(),
Beneficiary: beneficiary.String(),
HasCustomer: hasCustomer.Int64() == -1,
Customer: customer.String(),
InvoiceID: invoiceID,
Amount: amount.Uint64(),
Paid: paid.Int64() == -1,
Active: active.Int64() == -1,
Version: version.Int64(),
}, nil
}
Loading

0 comments on commit 7600f3d

Please sign in to comment.