Google pay 国际版开发
一、官网地址(科学上网)
官方对接文档 https://developers.google.com/pay/api/android/overview
Stripe对接Google Pay https://stripe.com/docs/google-pay
Stripe验证支付 https://stripe.com/docs/payments/accept-a-payment?integration=elements
Stripe管理后台 https://dashboard.stripe.com/dashboard
二、接入流程

三、主要流程
1、服务器请求orderId
2、调用Google Pay
3、通过Stripe完成支付
4、服务器完成验证
四、主要代码实现(1、4主要是服务器流程,以下主要是2、3流程)
项目配置
build.gradle 添加
implementation 'com.google.android.gms:play-services-wallet:18.1.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.stripe:stripe-android:16.0.1'
AndroidManifest.xml 的 application添加
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
调用Google Pay
@RequiresApi(api = Build.VERSION_CODES.N)
private void payWithGoogle( double price) {
if (paymentsClient == null) {
paymentsClient = createPaymentsClient(mActivity);
}
// 主要是Google Pay一些参数设置
Optional<JSONObject> paymentDataRequestJson = createPaymentDataRequest(price);
if (!paymentDataRequestJson.isPresent()) {
return;
}
PaymentDataRequest request =
PaymentDataRequest.fromJson(paymentDataRequestJson.get().toString());
if (request != null) {
if (mActivity!=null){
mActivity.showCircle2Loading();
}
// 调起 Google Pay
AutoResolveHelper.resolveTask(
paymentsClient.loadPaymentData(request),
mActivity,
LOAD_PAYMENT_DATA_REQUEST_CODE
);
}
}
Google Pay 基本设置
@RequiresApi(api = Build.VERSION_CODES.N)
private Optional<JSONObject> createPaymentDataRequest(double priceCents) {
try {
JSONObject paymentDataRequest = getBaseRequest();
// 指定是否支持 Google Pay API 所支持的一种或多种付款方式。
paymentDataRequest.put("allowedPaymentMethods", new JSONArray().put(getCardPaymentMethod()));
// 有关根据用户是否同意交易来为交易授权的详细信息。包含总价和价格状态
paymentDataRequest.put("transactionInfo", getTransactionInfo(priceCents));
//商家信息
paymentDataRequest.put("merchantInfo", getMerchantInfo());
paymentDataRequest.put("shippingAddressRequired", false);
paymentDataRequest.put("emailRequired", false);
return Optional.of(paymentDataRequest);
} catch (JSONException e) {
return Optional.empty();
}
}
private JSONObject getCardPaymentMethod() throws JSONException {
JSONObject cardPaymentMethod = getBaseCardPaymentMethod();
// 设置stripe为付款方式
JSONObject tokenizationSpec = new GooglePayConfig(LIVE_API).getTokenizationSpecification();
cardPaymentMethod.put("tokenizationSpecification", tokenizationSpec);
return cardPaymentMethod;
}
/**
* 金钱信息
*/
private JSONObject getTransactionInfo(double price) throws JSONException {
JSONObject transactionInfo = new JSONObject();
transactionInfo.put("totalPrice", price+"");
transactionInfo.put("totalPriceStatus", "FINAL");
transactionInfo.put("countryCode", COUNTRY_CODE);
transactionInfo.put("currencyCode", CURRENCY_CODE);
transactionInfo.put("checkoutOption", "COMPLETE_IMMEDIATE_PURCHASE");
return transactionInfo;
}
/**
* 商家信息
* merchantId 商家Id
*/
private JSONObject getMerchantInfo() throws JSONException {
return new JSONObject().put("merchantName", "Guruji").put("merchantId", "填写商家ID");
}
private JSONObject getBaseRequest() throws JSONException {
return new JSONObject()
.put("apiVersion", 2)
.put("apiVersionMinor", 0);
}
Google Pay返回数据
{
"apiVersionMinor": 0,
"apiVersion": 2,
"paymentMethodData": {
"description": "中国招商银行 (CMB) •••• 5019",
"tokenizationData": {
"type": "PAYMENT_GATEWAY",
"token": "{\n \"id\": \"tok_1HcQIMBcf7rsT369XhdHf1aI\",\n \"object\": \"token\",\n \"card\": {\n \"id\": \"card_1HcQIMBcf7rsT369lDDy6PIM\",\n \"object\": \"card\",\n \"address_city\": null,\n \"address_country\": null,\n \"address_line1\": null,\n \"address_line1_chec

本文介绍如何集成 Google Pay 国际版进行支付,包括接入流程、主要代码实现及配置步骤。文中提供了详细的示例代码,展示了如何通过 Stripe 完成支付验证。
最低0.47元/天 解锁文章
428

被折叠的 条评论
为什么被折叠?



