github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/pilvytis.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package contract
    19  
    20  import (
    21  	"encoding/json"
    22  
    23  	"github.com/mysteriumnetwork/node/identity"
    24  
    25  	"github.com/mysteriumnetwork/node/pilvytis"
    26  )
    27  
    28  // PaymentOrderOptions represents pilvytis payment order options
    29  // swagger:model PaymentOrderOptions
    30  type PaymentOrderOptions struct {
    31  	Minimum   float64   `json:"minimum"`
    32  	Suggested []float64 `json:"suggested"`
    33  }
    34  
    35  // ToPaymentOrderOptions - convert pilvytis.PaymentOrderOptions to contract.ToPaymentOrderOptions
    36  func ToPaymentOrderOptions(poo *pilvytis.PaymentOrderOptions) *PaymentOrderOptions {
    37  	return &PaymentOrderOptions{
    38  		Minimum:   poo.Minimum,
    39  		Suggested: poo.Suggested,
    40  	}
    41  }
    42  
    43  // PaymentOrderResponse holds payment gateway order details.
    44  // swagger:model PaymentOrderResponse
    45  type PaymentOrderResponse struct {
    46  	ID     string `json:"id"`
    47  	Status string `json:"status"`
    48  
    49  	Identity       string `json:"identity"`
    50  	ChainID        int64  `json:"chain_id"`
    51  	ChannelAddress string `json:"channel_address"`
    52  
    53  	GatewayName string `json:"gateway_name"`
    54  
    55  	ReceiveMYST string `json:"receive_myst"`
    56  	PayAmount   string `json:"pay_amount"`
    57  	PayCurrency string `json:"pay_currency"`
    58  	Country     string `json:"country"`
    59  	State       string `json:"state"`
    60  
    61  	Currency      string `json:"currency"`
    62  	ItemsSubTotal string `json:"items_sub_total"`
    63  	TaxRate       string `json:"tax_rate"`
    64  	TaxSubTotal   string `json:"tax_sub_total"`
    65  	OrderTotal    string `json:"order_total"`
    66  
    67  	PublicGatewayData json.RawMessage `json:"public_gateway_data"`
    68  }
    69  
    70  // NewPaymentOrderResponse creates an instance of PaymentOrderResponse
    71  func NewPaymentOrderResponse(r *pilvytis.GatewayOrderResponse) PaymentOrderResponse {
    72  	return PaymentOrderResponse{
    73  		ID:                r.ID,
    74  		Status:            string(r.Status),
    75  		Identity:          r.Identity,
    76  		ChainID:           r.ChainID,
    77  		ChannelAddress:    r.ChannelAddress,
    78  		GatewayName:       r.GatewayName,
    79  		ReceiveMYST:       r.ReceiveMYST,
    80  		PayAmount:         r.PayAmount,
    81  		PayCurrency:       r.PayCurrency,
    82  		Country:           r.Country,
    83  		State:             r.State,
    84  		Currency:          r.Currency,
    85  		ItemsSubTotal:     r.ItemsSubTotal,
    86  		TaxRate:           r.TaxRate,
    87  		TaxSubTotal:       r.TaxSubTotal,
    88  		OrderTotal:        r.OrderTotal,
    89  		PublicGatewayData: r.PublicGatewayData,
    90  	}
    91  }
    92  
    93  // NewPaymentOrdersResponse creates a slice of orders response
    94  func NewPaymentOrdersResponse(r []pilvytis.GatewayOrderResponse) []PaymentOrderResponse {
    95  	result := make([]PaymentOrderResponse, len(r))
    96  	for i := range r {
    97  		result[i] = NewPaymentOrderResponse(&r[i])
    98  	}
    99  	return result
   100  }
   101  
   102  // RegistrationPaymentResponse holds a registration payment order response.
   103  // swagger:model RegistrationPaymentResponse
   104  type RegistrationPaymentResponse struct {
   105  	Paid bool `json:"paid"`
   106  }
   107  
   108  // NewRegistrationPaymentResponse creates a registration order response
   109  func NewRegistrationPaymentResponse(r *pilvytis.RegistrationPaymentResponse) RegistrationPaymentResponse {
   110  	return RegistrationPaymentResponse{
   111  		Paid: r.Paid,
   112  	}
   113  }
   114  
   115  // GatewaysResponse holds payment gateway details.
   116  // swagger:model GatewaysResponse
   117  type GatewaysResponse struct {
   118  	Name         string              `json:"name"`
   119  	OrderOptions PaymentOrderOptions `json:"order_options"`
   120  	Currencies   []string            `json:"currencies"`
   121  }
   122  
   123  // ToGatewaysReponse converts a pilvytis gateway response to contract.
   124  func ToGatewaysReponse(g []pilvytis.GatewaysResponse) []GatewaysResponse {
   125  	result := make([]GatewaysResponse, len(g))
   126  	for i, v := range g {
   127  		entry := GatewaysResponse{
   128  			Name:         v.Name,
   129  			OrderOptions: *ToPaymentOrderOptions(&v.OrderOptions),
   130  			Currencies:   v.Currencies,
   131  		}
   132  		result[i] = entry
   133  	}
   134  	return result
   135  }
   136  
   137  // PaymentOrderRequest holds order request details
   138  // swagger:model PaymentOrderRequest
   139  type PaymentOrderRequest struct {
   140  	// example: 3.14
   141  	MystAmount string `json:"myst_amount"`
   142  
   143  	// example: 20
   144  	AmountUSD string `json:"amount_usd"`
   145  
   146  	// example: EUR
   147  	PayCurrency string `json:"pay_currency"`
   148  
   149  	// example: US
   150  	Country string `json:"country"`
   151  
   152  	// example: MO
   153  	State string `json:"state"`
   154  
   155  	// example: mysteriumvpn, mystnodes
   156  	ProjectID string `json:"project_id"`
   157  
   158  	// example: {}
   159  	CallerData json.RawMessage `json:"gateway_caller_data"`
   160  }
   161  
   162  // GatewayOrderRequest convenience mapper
   163  func (o *PaymentOrderRequest) GatewayOrderRequest(identity identity.Identity, gateway string) pilvytis.GatewayOrderRequest {
   164  	return pilvytis.GatewayOrderRequest{
   165  		Identity:    identity,
   166  		Gateway:     gateway,
   167  		MystAmount:  o.MystAmount,
   168  		AmountUSD:   o.AmountUSD,
   169  		PayCurrency: o.PayCurrency,
   170  		Country:     o.Country,
   171  		State:       o.State,
   172  		ProjectID:   o.ProjectID,
   173  		CallerData:  o.CallerData,
   174  	}
   175  }