Implementation
Implementation Procedure
- The user initiates WeChat payment in the WeChat mini app
- The WeChat mini app calls the OTT Pay API for payment, and the OTT Pay server returns Payment Parameters
- The WeChat mini app uses the returned payment parameter to call the WeChat wallet for payment
Code Example
Get Payment Parameters:
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);
}
The WeChat mini app page calls the built-in object of the WeChat page to make payment according to the payment parameters:
wx.requestPayment({
appId: appId,
timeStamp: timeStamp,
nonceStr: nonceStr,
package: packageStr,
signType: signType,
paySign: paySign,
success: function (res) {
// payment successful
// ...
},
fail: function (err) {
// payment failed
// ...
},
complete: function (res) {
// payment completed
// ...
}
});