notify

package
v0.2.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package notify 微信支付 API v3 Go SDK 商户通知处理库

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CipherSuite

type CipherSuite struct {
	// contains filtered or unexported fields
}

CipherSuite 算法套件,包括验签和解密

type ContentMap

type ContentMap map[string]interface{}

ContentMap 将微信支付通知请求内容Json解密后的内容Map

type EncryptedResource

type EncryptedResource struct {
	Algorithm      string `json:"algorithm"`
	Ciphertext     string `json:"ciphertext"`
	AssociatedData string `json:"associated_data"`
	Nonce          string `json:"nonce"`
	OriginalType   string `json:"original_type"`

	Plaintext string // Ciphertext 解密后内容
}

EncryptedResource 微信支付通知请求中的内容

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler 通知处理器,使用前先设置验签和解密的算法套件

func NewEmptyHandler

func NewEmptyHandler() *Handler

NewEmptyHandler 创建一个不包含算法套件的空通知处理器

func NewNotifyHandler

func NewNotifyHandler(apiV3Key string, verifier auth.Verifier) *Handler

NewNotifyHandler 创建通知处理器 Deprecated: Use NewRSANotifyHandler instead

func NewRSANotifyHandler

func NewRSANotifyHandler(apiV3Key string, verifier auth.Verifier) (*Handler, error)

NewRSANotifyHandler 创建一个 RSA 的通知处理器,它包含 AES-GCM 解密能力

func (*Handler) AddCipherSuite

func (h *Handler) AddCipherSuite(cipherSuite CipherSuite) *Handler

AddCipherSuite 添加一个算法套件

func (*Handler) AddRSAWithAESGCM

func (h *Handler) AddRSAWithAESGCM(verifier auth.Verifier, aesgcm cipher.AEAD) *Handler

AddRSAWithAESGCM 添加一个 RSA + AES-GCM 的算法套件

func (*Handler) ParseNotifyRequest

func (h *Handler) ParseNotifyRequest(
	ctx context.Context,
	request *http.Request,
	content interface{},
) (*Request, error)

ParseNotifyRequest 从 HTTP 请求(http.Request) 中解析 微信支付通知(notify.Request)

Example (No_model)
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/charleybin/wechatpay-go/core/notify"
)

func main() {
	var handler notify.Handler
	var request *http.Request

	content := make(map[string]interface{})
	notifyReq, err := handler.ParseNotifyRequest(context.Background(), request, content)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 处理通知内容
	fmt.Println(notifyReq.Summary)
	fmt.Println(content)
}
Output:

Example (Transaction)
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/charleybin/wechatpay-go/core/notify"
	"github.com/charleybin/wechatpay-go/services/payments"
)

func main() {
	var handler notify.Handler
	var request *http.Request

	content := new(payments.Transaction)
	notifyReq, err := handler.ParseNotifyRequest(context.Background(), request, content)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 处理通知内容
	fmt.Println(notifyReq.Summary)
	fmt.Println(content)
}
Output:

type Request

type Request struct {
	ID           string             `json:"id"`
	CreateTime   *time.Time         `json:"create_time"`
	EventType    string             `json:"event_type"`
	ResourceType string             `json:"resource_type"`
	Resource     *EncryptedResource `json:"resource"`
	Summary      string             `json:"summary"`

	// 原始通知请求
	RawRequest *http.Request
}

Request 微信支付通知请求结构

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL