Go POST request to Third-Party API

287 views Asked by At

I am stuck wanting to do a simple POST request from my go microservice to Rapyd API. I searched for many solutions on the web but none worked... If I try to make the request, I either got a 400 Bad Request or an error containing "Network unreachable" I tried making the body payload with Go structs, with JSON from string but nothing seems to work, here is my code : (Sorry I had to insert the structs with snippet because it wasn't working with code sample)

type CardRequest struct {
    Amount        float64 `json:"amount"`
    Currency      string  `json:"currency"`
    PaymentMethod PaymentMethod `json:"payment_method"`
    ErrorPaymentURL string `json:"error_payment_url"`
    Capture         bool   `json:"capture"`
}

type PaymentMethod struct {
    Type   string `json:"type"`
    Fields Fields `json:"fields"`
}

type Fields struct {
    Number          string `json:"number"`
    ExpirationMonth string `json:"expiration_month"`
    ExpirationYear  string `json:"expiration_year"`
    Cvv             string `json:"cvv"`
}

    hc := http.Client{}
    form, _ := json.Marshal(CardRequest{
       Amount: 53.5,
       Currency: "THB",
       PaymentMethod: PaymentMethod{
           Type: "th_visa_card",
           Fields: Fields{
               Number: "4111111111111111",
               ExpirationMonth: "10",
               ExpirationYear: "21",
               Cvv: "123",
           },
       },
      ErrorPaymentURL: "https://error_example.net",
      Capture: true})
    http_method := "post"
    url_path := "/v1/payments"
    salt := strconv.Itoa(10000000 + rand.Intn(99999999-10000000))
    timestamp := strconv.Itoa(int(time.Now().Unix()))
    access_key := "my_access_key"
    secret_key := "my_secret_key"
    sign_str := http_method + url_path + salt + timestamp + access_key + secret_key + string(form[:])
    fmt.Println(form)
    h := hmac.New(sha256.New, []byte(secret_key))
    h.Write([]byte(sign_str))
    buf := h.Sum(nil)
    signature := base64.RawURLEncoding.EncodeToString(buf)
    httpReq, err := http.NewRequest("POST", "https://sandboxapi.rapyd.net/v1/payments", bytes.NewBuffer(form))
    // req.PostForm = form
    httpReq.Header.Set("Content-Type", "application/json")
    httpReq.Header.Set("access_key", access_key)
    httpReq.Header.Set("salt", salt)
    httpReq.Header.Set("signature", signature)
    httpReq.Header.Set("timestamp", timestamp)

    resp, err := hc.Do(httpReq)
    if err != nil {
        fmt.Println(err)
        return errors.New(fmt.Sprintf("payment request failed... %v", err))
    }
    bytes, err := httputil.DumpRequest(httpReq, true)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(string(bytes))
    data, err2 := io.ReadAll(resp.Body)
    if err2 != nil {
        fmt.Println(err2)
        return errors.New(fmt.Sprintf("payment body lecture failed... %v", err2))
    }
    fmt.Println(string(data))
    resp.Body.Close()
    // if data.status != "SUCCESS" || data.data.status != "CLO" || data.data.amount != req.Order.Total {
    //  return errors.New(fmt.Sprintf("payment failed... %v", data.status.message))
    // }

Thank you very much for reading me, I don't what to do... Don't hesitate to ask me for more informations and sorry if my english is bad!

EDIT

With my code I get this error : Post "https://sandboxapi.rapyd.net/v1/payments": dial tcp: lookup sandboxapi.rapyd.net on [::1]:53: dial udp [::1]:53: connect: cannot assign requested address

And with curl doing curl --header "Content-Type: application/json" --request POST --data '{"amount":9.99,"currency":"USD","payment_method":{"type":"us_visa_card","fields":{"number":"4111111111111111","expiration_month":"10","expiration_year":"21","cvv":"123"}},"error_payment_url":"https://error_example.net","capture":true}' -v https://sandboxapi.rapyd.net/v1/payments in verbose mode I receive :

*   Trying 23.21.18.233...
* TCP_NODELAY set
* Connected to sandboxapi.rapyd.net (23.21.18.233) port 443 (#0)
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 191 bytes...
* schannel: sent initial handshake data: sent 191 bytes
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 2/3)
* schannel: encrypted data got 4096
* schannel: encrypted data buffer: offset 4096 length 4096
* schannel: encrypted data length: 4030
* schannel: encrypted data buffer: offset 4030 length 4096
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 2/3)
* schannel: encrypted data got 1024
* schannel: encrypted data buffer: offset 5054 length 5054
* schannel: encrypted data length: 232
* schannel: encrypted data buffer: offset 232 length 5054
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 2/3)
* schannel: encrypted data got 115
* schannel: encrypted data buffer: offset 347 length 5054
* schannel: sending next handshake data: sending 126 bytes...
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 2/3)
* schannel: encrypted data got 51
* schannel: encrypted data buffer: offset 51 length 5054
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with sandboxapi.rapyd.net port 443 (step 3/3)
* schannel: stored credential handle in session cache
> POST /v1/payments HTTP/1.1
> Host: sandboxapi.rapyd.net
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 200
>
* upload completely sent off: 200 out of 200 bytes
* schannel: client wants to read 102400 bytes
* schannel: encdata_buffer resized 103424
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 226
* schannel: encrypted data buffer: offset 226 length 103424
* schannel: decrypted data length: 197
* schannel: decrypted data added: 197
* schannel: decrypted data cached: offset 197 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 197 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 197
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 400 Bad Request
< Date: Wed, 14 Apr 2021 06:16:04 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 11
< Connection: keep-alive
< ETag: W/"b-glZE90e6qywA5CDbvDnksw"
<
Bad Request* Connection #0 to host sandboxapi.rapyd.net left intact
0

There are 0 answers