Example #1
0
func Pay(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
	var parmas pingpp.ChargeParams
	buf := new(bytes.Buffer)
	buf.ReadFrom(r.Body)
	json.Unmarshal(buf.Bytes(), &params)

	extra := make(map[string]interface{})
	extra["success_url"] = "http://127.0.0.1:7063/paySuccess.html"
	// extra["cancel_url"] = "http://127.0.0.1:7063/payCancel.html"

	params.Currency = "cny"
	params.Client_ip = r.RemoteAddr
	params.extra = extra

	ch, err := charge.New(params)

	if err != nil {
		errs, _ := json.Marshal(err)
		fmt.Fprint(w, string(errs))
	} else {
		chs, _ := json.Marshal(ch)
		fmt.Fprintln(w, string(chs))
	}
}
Example #2
0
func ExampleCharge_new() {
	metadata := make(map[string]interface{})
	metadata["color"] = "red"
	//extra 参数根据渠道不同有区别,下面注释的是一部分的示例
	extra := make(map[string]interface{})
	// //upacp_wap
	// extra["result_url"] = "http://www.yourdomain.com"

	//bfb_wap
	// extra["result_url"] = "http://www.yourdomain.com"
	// extra["bfb_login"] = false

	// //yeepay_wap
	// extra["product_category"] = "1"
	// extra["identity_id"] = "sadfadsjkfhasuidfhbjdasf"
	// extra["identity_type"] = 1
	// extra["terminal_type"] = 1
	// extra["terminal_id"] = "1sdf"
	// extra["user_ua"] = "1qwec"
	// extra["result_url"] = "http://www.yourdomain.com"

	// //alipay_wap
	// extra["cancel_url"] = "http://www.yourdomain.com"
	// extra["success_url"] = "http://www.yourdomain.com"

	// //wx_pub
	// extra["open_id"] = "sdafdgagfd"

	//jdpay_wap
	// extra["success_url"] = "http://www.yourdomain.com"
	// extra["fail_url"] = "http://www.yourdomain.com"
	// extra["token"] = "dsafadsfasdfadsjuyhfnhujkijunhaf"

	// //wx_pub_qr
	// extra["product_id"] = "23sf"

	//这里是随便设置的随机数作为订单号,仅作示例,该方法可能产生相同订单号,商户请自行生成,不要纠结该方法。
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	orderno := r.Intn(999999999999999)

	params := &pingpp.ChargeParams{
		Order_no:  strconv.Itoa(orderno),
		App:       pingpp.App{Id: "app_1Gqj58ynP0mHeX1q"},
		Amount:    1000,
		Channel:   "alipay",
		Currency:  "cny",
		Client_ip: "127.0.0.1",
		Subject:   "Your Subject",
		Body:      "Your Body",
		Extra:     extra,
		Metadata:  metadata,
	}
	//返回的第一个参数是 charge 对象,你需要将其转换成 json 给客户端,或者客户端接收后转换。
	ch, err := charge.New(params)
	if err != nil {
		errs, _ := json.Marshal(err)
		fmt.Println(string(errs))
		log.Fatal(err)
		return
	}
	fmt.Println(ch)

}
Example #3
0
func pay(w http.ResponseWriter, r *http.Request) {
	if strings.ToUpper(r.Method) == "GET" {
		http.NotFound(w, r)
	} else if strings.ToUpper(r.Method) == "POST" {
		var chargeParams pingpp.ChargeParams
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Content-Type", "application/json")
		defer r.Body.Close()
		buf := new(bytes.Buffer)
		buf.ReadFrom(r.Body)

		json.Unmarshal(buf.Bytes(), &chargeParams)
		r := rand.New(rand.NewSource(time.Now().UnixNano()))
		orderno := r.Intn(999999999999999)
		extra := make(map[string]interface{})
		switch strings.ToLower(chargeParams.Channel) {
		case "upacp_wap":
			extra["result_url"] = "http://www.yourdomain.com/result"
		case "alipay_wap":
			extra["cancel_url"] = "http://www.yourdomain.com/cancel"
			extra["success_url"] = "http://www.yourdomain.com/success"
		case "bfb_wap":
			extra["result_url"] = "http://www.yourdomain.com/result"
			extra["bfb_login"] = false
		case "yeepay_wap":
			extra["product_category"] = "1"
			extra["identity_id"] = "your_identity_id"
			extra["identity_type"] = 1
			extra["terminal_type"] = 1
			extra["terminal_id"] = "1sdf"
			extra["user_ua"] = "1qwec"
			extra["result_url"] = "http://www.yourdomain.com/result"
		case "wx_pub":
			extra["open_id"] = "your_openid"
		case "jdpay_wap":
			extra["success_url"] = "http://www.yourdomain.com/success"
			extra["fail_url"] = "http://www.yourdomain.com/fail"
			extra["token"] = "your_token_from_jd"
		case "wx_pub_qr":
			extra["product_id"] = "your_productid"

		}

		pingpp.Key = "sk_test_ibbTe5jLGCi5rzfH4OqPW9KC"

		params := &pingpp.ChargeParams{
			Order_no:  strconv.Itoa(orderno),
			App:       pingpp.App{Id: "app_1Gqj58ynP0mHeX1q"},
			Amount:    chargeParams.Amount,
			Channel:   strings.ToLower(chargeParams.Channel),
			Currency:  "cny",
			Client_ip: "127.0.0.1",
			Subject:   "Your Subject",
			Body:      "Your Body",
			Extra:     extra,
		}

		//返回的第一个参数是 charge 对象,你需要将其转换成 json 给客户端,或者客户端接收后转换。
		ch, err := charge.New(params)

		if err != nil {
			errs, _ := json.Marshal(err)
			fmt.Fprint(w, string(errs))
		} else {
			chs, _ := json.Marshal(ch)
			fmt.Fprintln(w, string(chs))
		}

	}
}