Created
October 11, 2019 05:25
-
-
Save iyusa/8c51c182baf7c79d6b2c71e764691c09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jssx | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"../../common" | |
"google.golang.org/grpc/codes" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/status" | |
"github.com/iyusa/shared/tool" | |
"github.com/jinzhu/copier" | |
) | |
// Biller implement common.IBIller | |
type Biller struct { | |
port int | |
ip string | |
rawRequest string | |
rawResponse string | |
basePrice float64 | |
sellPrice float64 | |
} | |
// ReadConfig from owner for setup internal field | |
func (b *Biller) ReadConfig() { | |
b.port = common.Config.JSS.Port | |
b.ip = common.Config.JSS.IP | |
} | |
// Execute implement IBiller. in: api.TransactionRequest, out: api.TransactionResponse | |
func (b *Biller) Execute(in interface{}, out interface{}) error { | |
address := fmt.Sprintf("%s:%d", b.ip, b.port) | |
// 1. Set up a connection to the server. | |
conn, err := grpc.Dial(address, grpc.WithInsecure()) | |
if err != nil { | |
return status.Error(codes.FailedPrecondition, err.Error()) | |
} | |
defer conn.Close() | |
// 2. create client | |
client := NewJssxClient(conn) | |
// 3. setup request parameter | |
var req TransactionRequest | |
copier.Copy(&req, in) | |
b.rawRequest = tool.AsJSON(req) | |
fmt.Printf("Receiving Input from mikropay: \n%s\n", tool.AsJSON(in)) | |
fmt.Printf("Creating request to gateway (jss): \n%s\n", b.rawRequest) | |
// 4. create context | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60) | |
defer cancel() | |
// 5. execute | |
resp, err := client.Execute(ctx, &req) | |
if err != nil { | |
st, ok := status.FromError(err) | |
if ok && st.Code() == codes.DeadlineExceeded { | |
log.Println("Execute to JSS Timeout ...") | |
return err | |
} | |
return err | |
} | |
b.rawResponse = resp.RawResponse | |
fmt.Printf("Receiving response from gateway: \n%s\n", tool.AsJSON(resp)) | |
copier.Copy(out, resp) | |
return nil | |
} | |
// GetPrice get base price and sell pricr | |
func (b *Biller) GetPrice() (float64, float64) { | |
return b.basePrice, b.sellPrice | |
} | |
// GetRawResponse from biller | |
func (b *Biller) GetRawResponse() string { | |
return b.rawResponse | |
} | |
// GetRawRequest get raw response from biller | |
func (b *Biller) GetRawRequest() string { | |
return b.rawRequest | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment