mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-12 00:32:26 +08:00
add refund
This commit is contained in:
@@ -1,16 +1,9 @@
|
|||||||
package pay
|
package pay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/tls"
|
|
||||||
"encoding/pem"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/akikistyle/wechat/util"
|
"github.com/akikistyle/wechat/util"
|
||||||
"golang.org/x/crypto/pkcs12"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var refundGateway = "https://api.mch.weixin.qq.com/secapi/pay/refund"
|
var refundGateway = "https://api.mch.weixin.qq.com/secapi/pay/refund"
|
||||||
@@ -93,7 +86,7 @@ func (pcf *Pay) Refund(p *RefundParams) (rsp RefundResponse, err error) {
|
|||||||
RefundFee: p.RefundFee,
|
RefundFee: p.RefundFee,
|
||||||
RefundDesc: p.RefundDesc,
|
RefundDesc: p.RefundDesc,
|
||||||
}
|
}
|
||||||
rawRet, err := postXMLWithTLS(refundGateway, request, p.RootCa, pcf.PayMchID)
|
rawRet, err := util.PostXMLWithTLS(refundGateway, request, p.RootCa, pcf.PayMchID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -113,68 +106,3 @@ func (pcf *Pay) Refund(p *RefundParams) (rsp RefundResponse, err error) {
|
|||||||
string(rawRet), str, sign)
|
string(rawRet), str, sign)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//http TLS
|
|
||||||
func httpWithTLS(rootCa, key string) (*http.Client, error) {
|
|
||||||
var client *http.Client
|
|
||||||
certData, err := ioutil.ReadFile(rootCa)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to find cert path=%s, error=%v", rootCa, err)
|
|
||||||
}
|
|
||||||
cert := pkcs12ToPem(certData, key)
|
|
||||||
config := &tls.Config{
|
|
||||||
Certificates: []tls.Certificate{cert},
|
|
||||||
}
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: config,
|
|
||||||
DisableCompression: true,
|
|
||||||
}
|
|
||||||
client = &http.Client{Transport: tr}
|
|
||||||
return client, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//将Pkcs12转成Pem
|
|
||||||
func pkcs12ToPem(p12 []byte, password string) tls.Certificate {
|
|
||||||
blocks, err := pkcs12.ToPEM(p12, password)
|
|
||||||
defer func() {
|
|
||||||
if x := recover(); x != nil {
|
|
||||||
log.Print(x)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
var pemData []byte
|
|
||||||
for _, b := range blocks {
|
|
||||||
pemData = append(pemData, pem.EncodeToMemory(b)...)
|
|
||||||
}
|
|
||||||
cert, err := tls.X509KeyPair(pemData, pemData)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return cert
|
|
||||||
}
|
|
||||||
|
|
||||||
//Post XML with TLS
|
|
||||||
func postXMLWithTLS(uri string, obj interface{}, ca, key string) ([]byte, error) {
|
|
||||||
xmlData, err := xml.Marshal(obj)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
body := bytes.NewBuffer(xmlData)
|
|
||||||
client, err := httpWithTLS(ca, key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
response, err := client.Post(uri, "application/xml;charset=utf-8", body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, response.StatusCode)
|
|
||||||
}
|
|
||||||
return ioutil.ReadAll(response.Body)
|
|
||||||
}
|
|
||||||
|
|||||||
69
util/http.go
69
util/http.go
@@ -2,11 +2,15 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"golang.org/x/crypto/pkcs12"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -141,3 +145,68 @@ func PostXML(uri string, obj interface{}) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return ioutil.ReadAll(response.Body)
|
return ioutil.ReadAll(response.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//http TLS
|
||||||
|
func httpWithTLS(rootCa, key string) (*http.Client, error) {
|
||||||
|
var client *http.Client
|
||||||
|
certData, err := ioutil.ReadFile(rootCa)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to find cert path=%s, error=%v", rootCa, err)
|
||||||
|
}
|
||||||
|
cert := pkcs12ToPem(certData, key)
|
||||||
|
config := &tls.Config{
|
||||||
|
Certificates: []tls.Certificate{cert},
|
||||||
|
}
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: config,
|
||||||
|
DisableCompression: true,
|
||||||
|
}
|
||||||
|
client = &http.Client{Transport: tr}
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//将Pkcs12转成Pem
|
||||||
|
func pkcs12ToPem(p12 []byte, password string) tls.Certificate {
|
||||||
|
blocks, err := pkcs12.ToPEM(p12, password)
|
||||||
|
defer func() {
|
||||||
|
if x := recover(); x != nil {
|
||||||
|
log.Print(x)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var pemData []byte
|
||||||
|
for _, b := range blocks {
|
||||||
|
pemData = append(pemData, pem.EncodeToMemory(b)...)
|
||||||
|
}
|
||||||
|
cert, err := tls.X509KeyPair(pemData, pemData)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return cert
|
||||||
|
}
|
||||||
|
|
||||||
|
//Post XML with TLS
|
||||||
|
func PostXMLWithTLS(uri string, obj interface{}, ca, key string) ([]byte, error) {
|
||||||
|
xmlData, err := xml.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
body := bytes.NewBuffer(xmlData)
|
||||||
|
client, err := httpWithTLS(ca, key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
response, err := client.Post(uri, "application/xml;charset=utf-8", body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, response.StatusCode)
|
||||||
|
}
|
||||||
|
return ioutil.ReadAll(response.Body)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user