实现方法

实现流程

  1. 用户在微信内页面中发起微信支付
  2. 页面程序调用 OTT Pay API 进行支付,OTT Pay 服务器返回 支付跳转链接
  3. 页面程序使用跳转到 支付跳转链接 会调起微信钱包进行支付
  4. 用户支付后,不管成功或失败,都会跳转到请求参数中提供的 returnURL,跳转返回的 URL 上会有 paymentId=xxxx(xxxx 即 OTT Pay 返回的 paymentId)

代码示例

取得支付参数:

Map<String, Object> data = new HashMap<>();
data.put("amount", "100");
data.put("callbackURL", "https://your.callback.url");
data.put("returnURL", "https://your.return.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.eyJzdWIiOiJBQjAwMDAwMDA0OTUiLCJhdWRpZW5jZSI6bnVsbCwicm9sZSI6bnVsbCwiY3JlYXRlZCI6MTY3ODQ3MTEwMjU5NiwidXNlcnR5cGUiOiJDVVNUT01FUiIsImV4cCI6MTY3ODQ3MjAwMiwidXNlcmlkIjo5NX0.UjhYQc5UCMiKiSiuClEMn1ugPcUYQWvD81qmN2awToWsJOInq-S6LFtaNaoZosRMm__UUT2UMjO2CCfTHrThJg";
Request request = new Request.Builder()
    .url("https://ecom-api.ottpay.com/api/v1/pay/weixin/public-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);
    }
    
    // 响应结果
    System.out.println(response.body().string());
} catch (IOException e) {
    throw new RuntimeException(e);
}

返回结果中的 payInfo 就是支付跳转链接,前端页面跳转到此链接进行支付。支付结束