1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| package main
import ( "bytes" "encoding/json" "fmt" "gopkg.in/yaml.v2" "io" "io/ioutil" "log" "net/http" "text/template" "time" )
var ( client = &http.Client{ Timeout: 10 * time.Second, } templates = template.Must(template.ParseFiles("index.html")) config Config )
type Config struct { PartnerKey string `yaml:"partner_key"` MerchantID string `yaml:"merchant_id"` }
type Payload struct { PartnerKey string `json:"partner_key"` Prime string `json:"prime"` Amount int `json:"amount"` MerchantID string `json:"merchant_id"` Details string `json:"details"` Cardholder struct { PhoneNumber string `json:"phone_number"` Name string `json:"name"` Email string `json:"email"` ZipCode string `json:"zip_code"` Address string `json:"address"` NationalID string `json:"national_id"` } `json:"cardholder"` }
type Result struct { Status int `json:"status"` Msg string `json:"msg"` Amount int `json:"amount"` Acquirer string `json:"acquirer"` Currency string `json:"currency"` RecTradeID string `json:"rec_trade_id"` BankTransactionID string `json:"bank_transaction_id"` OrderNumber string `json:"order_number"` AuthCode string `json:"auth_code"` CardInfo struct { Issuer string `json:"issuer"` Funding int `json:"funding"` Type int `json:"type"` Level string `json:"level"` Country string `json:"country"` LastFour string `json:"last_four"` BinCode string `json:"bin_code"` IssuerZhTw string `json:"issuer_zh_tw"` BankID string `json:"bank_id"` CountryCode string `json:"country_code"` } `json:"card_info"` TransactionTimeMillis int64 `json:"transaction_time_millis"` BankTransactionTime struct { StartTimeMillis string `json:"start_time_millis"` EndTimeMillis string `json:"end_time_millis"` } `json:"bank_transaction_time"` BankResultCode string `json:"bank_result_code"` BankResultMsg string `json:"bank_result_msg"` CardIdentifier string `json:"card_identifier"` MerchantID string `json:"merchant_id"` IsRbaVerified bool `json:"is_rba_verified"` TransactionMethodDetails struct { TransactionMethodReference string `json:"transaction_method_reference"` TransactionMethod string `json:"transaction_method"` } `json:"transaction_method_details"` }
func init() { if err := parseConfig(); err != nil { log.Fatal(err) } }
func main() { http.HandleFunc("/", Index) http.HandleFunc("/api/pay", Pay) http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) log.Fatal(http.ListenAndServe(":8080", nil)) }
func parseConfig() error { file := "config.yaml" b, err := ioutil.ReadFile(file) if err != nil { return err } return yaml.Unmarshal(b, &config) }
func Index(w http.ResponseWriter, r *http.Request) { if err := templates.ExecuteTemplate(w, "index.html", nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
func Pay(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodOptions { response(w, http.StatusOK, nil) return } payload := Payload{ PartnerKey: config.PartnerKey, Amount: 1, MerchantID: config.MerchantID, } err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { response(w, http.StatusBadRequest, nil) return } b, err := json.Marshal(payload) if err != nil { response(w, http.StatusInternalServerError, nil) return } resp, err := payByPrime(bytes.NewBuffer(b)) if err != nil { response(w, http.StatusInternalServerError, nil) return } result := Result{} if err := json.Unmarshal(resp, &result); err != nil { response(w, http.StatusInternalServerError, nil) return } response(w, http.StatusOK, result) }
func payByPrime(body io.Reader) (b []byte, err error) { url := "https://sandbox.tappaysdk.com/tpc/payment/pay-by-prime" req, _ := http.NewRequest(http.MethodPost, url, body) req.Header.Set("x-api-key", config.PartnerKey) resp, err := client.Do(req) if err != nil { return } if resp.StatusCode != http.StatusOK { err = fmt.Errorf("unexpected response code: %v", resp.StatusCode) return } defer closeBody(resp.Body) b, err = ioutil.ReadAll(resp.Body) if err != nil { return } return }
func closeBody(reader io.ReadCloser) { if err := reader.Close(); err != nil { log.Fatal(err) } }
func response(w http.ResponseWriter, code int, v interface{}) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Headers", "*") w.WriteHeader(code) if v == nil { return } if err := json.NewEncoder(w).Encode(v); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
|