-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from ArtisanCloud/develop
Develop
- Loading branch information
Showing
12 changed files
with
273 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package virtualPayment | ||
|
||
import ( | ||
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel" | ||
) | ||
|
||
type Client struct { | ||
App *kernel.ApplicationInterface | ||
*kernel.BaseClient | ||
|
||
appKey string | ||
offerId string | ||
} | ||
|
||
func NewClient(app *kernel.ApplicationInterface) (*Client, error) { | ||
config := (*app).GetConfig() | ||
|
||
baseClient, err := kernel.NewBaseClient(app, nil) | ||
|
||
if err != nil { | ||
|
||
return nil, err | ||
} | ||
|
||
client := &Client{ | ||
BaseClient: baseClient, | ||
App: app, | ||
appKey: config.GetString("app_key", ""), | ||
offerId: config.GetString("offer_id", ""), | ||
} | ||
|
||
client.OverrideGetMiddlewares() | ||
client.RegisterHttpMiddlewares() | ||
|
||
return client, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package virtualPayment | ||
|
||
import ( | ||
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel" | ||
) | ||
|
||
func RegisterProvider(app kernel.ApplicationInterface) (*Client, error) { | ||
client, err := NewClient(&app) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package request | ||
|
||
type VirtualPaymentOrderRequest struct { | ||
SessionKey string `json:"session_key"` // 文档已更新,现在允许存session key 过期时间官方写的是三天,配合前端checkSessionKey组件经测试 官方有bug | ||
ProductId int64 `json:"product_id"` // 商品id | ||
Price int64 `json:"price"` // 金额 单位分 | ||
OutTradeNo string `json:"out_trade_no"` // 订单号 | ||
Attach string `json:"attach"` // 附加信息 | ||
} | ||
|
||
type UploadProductsRequest struct { | ||
Env int8 `json:"env"` // 0正式 1测试 | ||
UploadItem []*GoodItem `json:"upload_item"` | ||
} | ||
|
||
type GoodItem struct { | ||
Id string `json:"id"` // 商品id 同product id 长度(0,64],字符只允许使用字母、数字、'_'、'-' | ||
Name string `json:"name"` // 商品名称 长度(0,1024] 不能有特殊字符 | ||
Price int64 `json:"price"` // 道具单价,单位分,需要大于0 | ||
Remake string `json:"remake"` // 道具备注,长度(0,1024] | ||
ItemUrl string `json:"item_url"` // 道具图片的url地址,当前仅支持jpg,png等格式 使用查询上传结果接口会发现,经常会报错上传图片失败 需要多传几次 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package response | ||
|
||
type UploadItem struct { | ||
Id string `json:"id"` | ||
ItemUrl string `json:"item_url"` | ||
Name string `json:"name"` | ||
Price int `json:"price"` | ||
UploadStatus int `json:"upload_status"` | ||
ErrMsg string `json:"errmsg"` // 这个upload status与官方文档返回不一致 可以直接判断errmsg是否为空 代表是否有错误 | ||
} | ||
|
||
type UploadProductSearchResponse struct { | ||
Cost int `json:"cost"` | ||
End int `json:"end"` | ||
Errcode int `json:"errcode"` | ||
Errmsg string `json:"errmsg"` | ||
Progress int `json:"progress"` | ||
Status int `json:"status"` | ||
Total int `json:"total"` | ||
UploadItem []*UploadItem `json:"upload_item"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package virtualPayment | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/ArtisanCloud/PowerLibs/v3/object" | ||
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel" | ||
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/virtualPayment/request" | ||
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/device/response" | ||
) | ||
|
||
// 小程序虚拟支付 | ||
// uri固定写死为 requestVirtualPayment | ||
// 官方示例是写死为这个postbody 没有任何相关文档表示这个postbody是怎么来的 以及各个参数的意义,只能直译 | ||
// https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/virtual-payment.html#_2-4-%E7%AD%BE%E5%90%8D%E8%AF%A6%E8%A7%A3 | ||
|
||
func (client *Client) TransactionVirtual(ctx context.Context, in *request.VirtualPaymentOrderRequest) (result *object.StringMap, err error) { | ||
|
||
if client.offerId == "" { | ||
|
||
return nil, errors.New("offerId is empty") | ||
} | ||
|
||
if client.appKey == "" { | ||
|
||
return nil, errors.New("appKey is empty") | ||
} | ||
|
||
if in.SessionKey == "" { | ||
|
||
return nil, errors.New("sessionKey is empty") | ||
} | ||
|
||
uri := "requestVirtualPayment" | ||
|
||
postBody := fmt.Sprintf(`{"offerId":"%s","buyQuantity":1,"env":0,"currencyType":"CNY","platform":"android","productId":"%d","goodsPrice":%d,"outTradeNo":"%s","attach":"%s"}`, client.offerId, in.ProductId, in.Price, in.OutTradeNo, in.Attach) | ||
|
||
paySign := kernel.CalcPaySig(uri, postBody, client.appKey) | ||
signature := kernel.CalcSignature(postBody, in.SessionKey) | ||
|
||
// todo 是否要变为强类型 | ||
return &object.StringMap{ | ||
"post_body": postBody, | ||
"pay_sign": paySign, | ||
"signature": signature, | ||
}, nil | ||
|
||
} | ||
|
||
// 商品上传 | ||
// https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/virtual-payment.html#_2-3-%E6%9C%8D%E5%8A%A1%E5%99%A8API | ||
|
||
func (client *Client) StartUploadGoods(ctx context.Context, params *request.UploadProductsRequest) (result *response.BaseResp, err error) { | ||
|
||
fmt.Printf("appid: %s, app_key: %s, offer_id: %s \n", (*client.App).GetConfig().GetString("app_id", ""), client.appKey, client.offerId) | ||
|
||
if err != nil { | ||
|
||
return nil, err | ||
} | ||
|
||
postBody, err := json.Marshal(params) | ||
|
||
if err != nil { | ||
|
||
return nil, err | ||
} | ||
|
||
signPost := string(postBody) | ||
|
||
// paySign | ||
endpoint := "/xpay/start_upload_goods" | ||
paySign := kernel.CalcPaySig(endpoint, signPost, client.appKey) | ||
|
||
uri := fmt.Sprintf("%s?pay_sig=%s", endpoint, paySign) | ||
|
||
_, err = client.HttpPostJson(ctx, uri, params, nil, nil, &result) | ||
|
||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.