PingPlusPlus (一): 簡單付款

感謝2015年4月20的 Ping++技術支持 耐心指導~


流程:

  1. 客戶端 對 服務端發出支付請求
  2. 服務端 根據請求,設置相應的extra.                     //這個extra不是額外的呀,是必須的!
  3. 客戶端 接收 服務端返回的Charge對象


我把ping++的處理 放在service層中了~~

	// 測試ping++交易
	// 返回Charge對象
	public Charge transaction() {
		Charge charge = null;
		Pingpp.apiKey = "sk_test_q540q9GanD0O4eLWDSqDa5SC";

		Map<String, Object> chargeMap = new HashMap<String, Object>();
		// 某些渠道需要添加extra參數,具體參數詳見接口文檔
		chargeMap.put("amount", 100);
		chargeMap.put("currency", "cny");
		chargeMap.put("subject", "Your Subject");
		chargeMap.put("body", "Your Body");
		chargeMap.put("order_no", "1111");
		chargeMap.put("channel", "alipay_wap");
		chargeMap.put("client_ip", "127.0.0.1");
		Map<String, String> app = new HashMap<String, String>();		
		app.put("id", "app_GeTunDi9WzzTyfDG");
		Map<String, String> extramap = new HashMap<String, String>();
                //extra的參數根據文檔: https://pingxx.com/document/api#api-c-new
                extramap.put("success_url", "http://127.0.0.1:8080/PartTimeJob/pinus_webview.html");
		chargeMap.put("extra", extramap);
		chargeMap.put("app", app);
		
		try {
			// 發起交易請求
			charge = Charge.create(chargeMap);
			System.out.println(charge);
		} catch (PingppException e) {
			e.printStackTrace();
		}
		return charge;
	}


HTML5客戶端,用的是ping++HTML5 SDK 的example.

    function wap_pay(channel) {
        var amount = document.getElementById('amount').value * 100;

        var pay_url = "deposit-transaction";  // 改成自己的url,就行了~其他的不用改.

        var xhr = new XMLHttpRequest();
        xhr.open("POST", pay_url, true);
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.send(JSON.stringify({
            channel: channel,
            amount: amount
        }));

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText);
                pingpp.createPayment(xhr.responseText, function(result, err) {
                    console.log(result);
                    console.log(err);
                });
            }
        }
    }


歡迎提建議~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章