You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.1 KiB
138 lines
3.1 KiB
package alipay
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Consult(info *ConsultPay) (error, ConsultResult) {
|
|
var (
|
|
err error
|
|
result ConsultResult
|
|
)
|
|
reqBytes, _ := json.Marshal(&info)
|
|
err, resBody := post(prefix+consult, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func CreateOrder(info *CreatePay) (error, OrderDetail) {
|
|
var (
|
|
err error
|
|
result OrderDetail
|
|
)
|
|
reqBytes, _ := json.Marshal(&info)
|
|
err, resBody := post(prefix+pay, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func CaptureOrder(info *CapturePay) (error, CaptureDetail) {
|
|
var (
|
|
err error
|
|
result CaptureDetail
|
|
)
|
|
reqBytes, _ := json.Marshal(&info)
|
|
err, resBody := post(prefix+capture, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
fmt.Println("AlipayCaptureOrder Result:", string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func InquiryPayment(paymentId string) (error, OrderDetail) {
|
|
var (
|
|
err error
|
|
result OrderDetail
|
|
)
|
|
reqBytes, _ := json.Marshal(&map[string]string{"paymentId": paymentId})
|
|
err, resBody := post(prefix+inquiry, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func Cancel(paymentId string) (error, CancelDetail) {
|
|
var (
|
|
err error
|
|
result CancelDetail
|
|
)
|
|
reqBytes, _ := json.Marshal(&map[string]string{"paymentRequestId": paymentId})
|
|
err, resBody := post(prefix+cancel, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func Refund(info *RefundRequest) (error, RefundResult) {
|
|
var (
|
|
err error
|
|
result RefundResult
|
|
)
|
|
reqBytes, _ := json.Marshal(&info)
|
|
err, resBody := post(prefix+refund, reqBytes)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
fmt.Println(string(resBody))
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
return err, result
|
|
}
|
|
func post(api string, reqBody []byte) (error, []byte) {
|
|
var (
|
|
err error
|
|
result []byte
|
|
)
|
|
req, err := http.NewRequest(http.MethodPost, baseurl+api, bytes.NewBuffer(reqBody))
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
requestTime := time.Now().Format(time.RFC3339)
|
|
signStr := sign(api, ClientId, requestTime, string(reqBody))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Client-Id", ClientId)
|
|
req.Header.Add("Request-Time", requestTime)
|
|
req.Header.Add("Signature", "algorithm=RSA256,keyVersion=1,signature="+signStr)
|
|
httpClient := &http.Client{}
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
return err, result
|
|
}
|
|
defer resp.Body.Close()
|
|
result, err = io.ReadAll(resp.Body)
|
|
return err, result
|
|
}
|
|
|