1. 程式人生 > 其它 >支付寶支付-整合服務端和客戶端

支付寶支付-整合服務端和客戶端

技術標籤:雜類

服務端

在pom檔案中新增.

        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.10.204.ALL</version>
        </dependency>

講一下

sdkExecute和pageExecute的區別.

sdkExecute是作為app的sdk拼接引數來使用.這個後面降到.

pageExecute是作為form表單放入html中.直接轉就行

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
//例項化具體API對應的request類,類名稱和介面名稱對應,當前呼叫介面名稱:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已經封裝掉了公共引數,這裡只需要傳入業務引數。以下方法為sdk的model入參方式(model和biz_content同時存在的情況下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("商品");
        model.setSubject("商品名稱");
        // 唯一訂單號,保證唯一就行
        model.setOutTradeNo(System.currentTimeMillis() + "");
        model.setTimeoutExpress("30m");
        // 價格
        model.setTotalAmount("0.0.1");
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("喚醒地址");
        try {
            //這裡和普通的介面呼叫不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "";

客戶端操作

   final String orderInfo = "將服務端返回的資料放到這裡";
        final Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(PayDemoActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        // 必須非同步呼叫
        Thread payThread = new Thread(payRunnable);
        payThread.start();