实现方法

实现流程

  1. 用户在微信小程序中发起微信支付
  2. 微信小程序调用 OTT Pay API 进行支付,OTT Pay 服务器返回 支付参数
  3. 微信小程序使用返回的 支付参数 调起微信钱包进行支付

代码示例

获得支付参数:

Map<String, Object> data = new HashMap<>();
data.put("appId", "wx7f194c1f7aad1643");
data.put("openId", "owv3d1fnMq0IpB6vWL7pK3TmJhBw");
data.put("amount", "100");
data.put("callbackURL", "https://your.callback.url");
data.put("remark", "this is remark");

Gson gson = new Gson();
RequestBody body = RequestBody.create(gson.toJson(data), MediaType.parse("application/json; charset=utf-8"));
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJPTjAwMDAwMTY2MTM5IiwiYXVkaWVuY2UiOm51bGwsInJvbGUiOm51bGwsImNyZWF0ZWQiOjE2Nzg0NzY3NDg5NTIsInVzZXJ0eXBlIjoiQ1VTVE9NRVIiLCJleHAiOjE2Nzg0Nzc2NDgsInVzZXJpZCI6MTM5fQ.CRO7ilMO9-1Rpr72lhr6wTmi9fMgGC-foAMLOR8sZD8f1fjOhcRHe625ubVZgGrNlxxD2nLUDQ-1cU8rzwDKhg";
Request request = new Request.Builder()
    .url("https://ecom-api.ottpay.com/api/v1/pay/weixin/mapp-pay")
    .addHeader("Authorization", "Bearer " + token)
    .post(body)
    .build();

final OkHttpClient httpClient = new OkHttpClient();
try (Response response = httpClient.newCall(request).execute()) {
    if (!response.isSuccessful()) {
        System.out.println(response.body().string());
        throw new IOException("Unexpected code " + response);
    }
    
    // Get response body
    System.out.println(response.body().string());
} catch (IOException e) {
    throw new RuntimeException(e);
}

小程序页面根据支付参数调用微信页面内置对象进行支付:

wx.requestPayment({
    appId: appId,
    timeStamp: timeStamp,
    nonceStr: nonceStr,
    package: packageStr,
    signType: signType,
    paySign: paySign,
    success: function (res) {
        // 支付成功
        // ...
    },
    fail: function (err) {
        // 支付失败
        // ...
    },
    complete: function (res) {
        // 支付完成
        // ...
    }
});