Giter VIP home page Giter VIP logo

trade's Introduction

trade

trade for alipay,wxpay with go

Alipay

sign

package main

import (
	"encoding/json"
	"fmt"

	"github.com/tiantour/tempo"
	"github.com/tiantour/trade/alipay"
)

func main() {
	sign, err := sign(alipay.Request{
		Body:        "测试商品详情",
		Subject:     "测试商品名称",
		OutTradeNo:  "1234567812345678",
		TotalAmount: 0.02,
		ProductCode: "QUICK_WAP_PAY",
	})
	fmt.Println(sign, err)
}

func sign(args alipay.Request) (string, error) {
	body, err := json.Marshal(args)
	if err != nil {
		return "", err
	}
	// 签名
	sign, err := alipay.NewTrade().Sign(alipay.Sign{
		AppID:      "your appid",
		Method:     "your method",
		ReturnURL:  "your return url",
		Charset:    "utf-8",
		SignType:   "RSA2",
		TimeStamp:  tempo.NewNow().String(),
		Version:    "1.0",
		NotifyURL:  "your notify url",
		BizContent: string(body),
	}, "your private key")
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%s?%s",
		"your gateway",
		sign,
	), nil
}

notify

package main

import (
	"fmt"

	"github.com/tiantour/trade/alipay"
)

func main() {
	args := alipay.Notice{}
	err := alipay.NewTrade().Verify(args, "your public key")
	fmt.Println(err)
}

Wxpay

sign

package main

import (
	"fmt"
	"strconv"

	"github.com/tiantour/imago"
	"github.com/tiantour/tempo"
	"github.com/tiantour/trade/wxpay"
)

func main() {
	args := wxpay.Request{
		Body:       "测试商品详情",
		Detail:     "商品名称",
		ProductID:  "商品编号",
		OutTradeNo: "1234567812345678",
		TotalFee:   1,
	}
	sign, err := sign(args)
	fmt.Println(sign, err)
}

func sign(args wxpay.Request) (wxpay.Defray, error) {
	sign := wxpay.Sign{
		AppID:          "your appid",
		MchID:          "your machid",
		NonceStr:       imago.NewRandom().Text(16),
		TradeType:      "JSAPI",
		SpbillCreateIP: "user ip address",
		NotifyURL:      "your notify url",
		OpenID:         "user openid",
		Request:        args,
	}
	str, err := wxpay.NewTrade().Sign(sign, "your api key")
	if err != nil {
		return wxpay.Defray{}, err
	}
	sign.Sign = str
	prepay, err := wxpay.NewTrade().Prepay(sign)
	if err != nil {
		return wxpay.Defray{}, err
	}
	defray := wxpay.Defray{
		AppID:     "your app id",
		Package:   "prepay_id=" + prepay,
		NonceStr:  imago.NewRandom().Text(16),
		TimeStamp: strconv.FormatInt(tempo.NewNow().Unix(), 10),
		SignType:  "MD5",
	}
	str, err = wxpay.NewTrade().Sign(defray, "your api key")
	if err != nil {
		return wxpay.Defray{}, err
	}
	defray.PaySign = str
	return defray, nil
}

notify

package main

import (
	"fmt"

	"github.com/tiantour/trade/wxpay"
)

func main() {
	args := wxpay.Notice{}
	err := wxpay.NewTrade().Verify(args, "your api key")
	fmt.Println(err)
}

trade's People

Contributors

jiangyoucai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.