fileuploader

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ImageUploadResponse

type ImageUploadResponse struct {
	MediaId *string `json:"media_id"` // revive:disable-line:var-naming
}

ImageUploadResponse 图片上传API返回结果

type ImageUploader

type ImageUploader services.Service

ImageUploader 图片上传API

部分微信支付业务指定商户需要使用图片上传 API来上报图片信息,从而获得必传参数的值:图片MediaID 。 接口文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml

func (*ImageUploader) Upload

func (u *ImageUploader) Upload(
	ctx context.Context, fileReader io.Reader, filename, contentType string,
) (*ImageUploadResponse, *core.APIResult, error)

Upload 上传图片至微信支付

Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/SilenceNo1/wechatpay-go/core"
	"github.com/SilenceNo1/wechatpay-go/core/consts"
	"github.com/SilenceNo1/wechatpay-go/core/option"
	"github.com/SilenceNo1/wechatpay-go/services/fileuploader"
	"github.com/SilenceNo1/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Print("load merchant private key error")
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
	}

	file, err := os.Open("picture.jpg")
	if err != nil {
		return
	}
	defer file.Close()

	svc := fileuploader.ImageUploader{Client: client}
	resp, result, err := svc.Upload(ctx, file, "picture.jpg", consts.ImageJPG)

	if err != nil {
		// 处理错误
		log.Printf("call ImageUploader err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d media_id=%s", result.Response.StatusCode, *resp.MediaId)
	}
}
Output:

type MarketingImageUploadResponse

type MarketingImageUploadResponse struct {
	MediaUrl *string `json:"media_url"` // revive:disable-line:var-naming
}

MarketingImageUploadResponse 图片上传API(营销专用)返回结果

type MarketingImageUploader

type MarketingImageUploader services.Service

MarketingImageUploader 图片上传API(营销专用)

通过本接口上传图片后可获得图片url地址。图片url可在微信支付营销相关的API使用, 包括商家券、代金券、支付有礼等。 接口文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_0_1.shtml

func (*MarketingImageUploader) Upload

func (u *MarketingImageUploader) Upload(
	ctx context.Context, fileReader io.Reader, filename string, contentType string,
) (*MarketingImageUploadResponse, *core.APIResult, error)

Upload 上传图片至微信支付营销系统

Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/SilenceNo1/wechatpay-go/core"
	"github.com/SilenceNo1/wechatpay-go/core/consts"
	"github.com/SilenceNo1/wechatpay-go/core/option"
	"github.com/SilenceNo1/wechatpay-go/services/fileuploader"
	"github.com/SilenceNo1/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Print("load merchant private key error")
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
	}

	file, err := os.Open("picture.jpg")
	if err != nil {
		return
	}
	defer file.Close()

	svc := fileuploader.MarketingImageUploader{Client: client}
	resp, result, err := svc.Upload(ctx, file, "picture.jpg", consts.ImageJPG)

	if err != nil {
		// 处理错误
		log.Printf("call MarketingImageUploader err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d media_url=%s", result.Response.StatusCode, *resp.MediaUrl)
	}
}
Output:

type MchBizUploadResponse

type MchBizUploadResponse struct {
	MediaId *string `json:"media_id"` // revive:disable-line:var-naming
}

MchBizUploadResponse 商户上传反馈图片API返回结果

type MchBizUploader

type MchBizUploader services.Service

MchBizUploader 商户上传反馈图片API

将媒体图片进行二进制转换,得到的媒体图片二进制内容,在请求body中上传此二进制内容。 媒体图片只支持jpg、png、bmp格式,文件大小不能超过2M。 接口文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_10.shtml

func (*MchBizUploader) Upload

func (u *MchBizUploader) Upload(
	ctx context.Context, fileReader io.Reader, filename, contentType string,
) (*MchBizUploadResponse, *core.APIResult, error)

Upload 上传反馈图片至微信支付

Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/SilenceNo1/wechatpay-go/core"
	"github.com/SilenceNo1/wechatpay-go/core/consts"
	"github.com/SilenceNo1/wechatpay-go/core/option"
	"github.com/SilenceNo1/wechatpay-go/services/fileuploader"
	"github.com/SilenceNo1/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Print("load merchant private key error")
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
	}

	file, err := os.Open("picture.jpg")
	if err != nil {
		return
	}
	defer file.Close()

	svc := fileuploader.MchBizUploader{Client: client}
	resp, result, err := svc.Upload(ctx, file, "picture.jpg", consts.ImageJPG)

	if err != nil {
		// 处理错误
		log.Printf("call MchBizUploader err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d media_id=%s", result.Response.StatusCode, *resp.MediaId)
	}
}
Output:

type VideoUploadResponse

type VideoUploadResponse struct {
	MediaId *string `json:"media_id"` // revive:disable-line:var-naming
}

VideoUploadResponse 视频上传API返回结果

type VideoUploader

type VideoUploader services.Service

VideoUploader 视频上传API

部分微信支付业务指定商户需要使用视频上传 API来上报视频信息,从而获得必传参数的值:视频MediaID 。 接口文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml

func (*VideoUploader) Upload

func (u *VideoUploader) Upload(
	ctx context.Context, fileReader io.Reader, filename, contentType string,
) (*VideoUploadResponse, *core.APIResult, error)

Upload 上传视频至微信支付

Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/SilenceNo1/wechatpay-go/core"
	"github.com/SilenceNo1/wechatpay-go/core/consts"
	"github.com/SilenceNo1/wechatpay-go/core/option"
	"github.com/SilenceNo1/wechatpay-go/services/fileuploader"
	"github.com/SilenceNo1/wechatpay-go/utils"
)

func main() {
	var (
		mchID                      string = "190000****"                               // 商户号
		mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
		mchAPIv3Key                string = "2ab9****************************"         // 商户APIv3密钥
	)

	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Print("load merchant private key error")
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("new wechat pay client err:%s", err)
	}

	file, err := os.Open("video.mp4")
	if err != nil {
		return
	}
	defer file.Close()

	svc := fileuploader.VideoUploader{Client: client}
	resp, result, err := svc.Upload(ctx, file, "video.mp4", consts.VideoMP4)

	if err != nil {
		// 处理错误
		log.Printf("call VideoUploader err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d media_id=%s", result.Response.StatusCode, *resp.MediaId)
	}
}
Output:

Jump to

Keyboard shortcuts

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