Google Pay的主要支付流程如下:
手机端通过向服务端发起支付请求,生成预订单,并将生成的订单号返回给手机端。
2. 在手机端发起支付请求,将生成的订单号传递给Google。
3. Google服务器将向手机端返回支付结果。由于此处涉及消耗型产品,购买后必须通知Google Play我已经完成了本次交易。
在手机端向服务端发送校验请求后,一旦通过校验,即可进行订单处理。服务端会重试校验,并确保成功发货,以保证订单正常发货完成。
客户端支付成功后,将订单的token传递给服务端进行再次校验。服务端有两种方式来校验订单:
1.引入google apis 调取Google Api 进行验证
2.直接调用Google 提供的API接口去验证
下面将详细介绍两种验证方式所需的配置和相应参数。
验证方式 一 : 引入google apis 调取Google Api 进行验证
开发者文档地址可在以下链接找到:https://developers.google.com/identity/protocols/oauth2/service-account?hl=zh-cn#delegatingauthority
1.去Google Play Console 选择设置中的API 权限:https://play.google.com/console/u/0/developers/6365974680746954105/api-access
2. 如果您第一次进入时没有Google Play Console Developer,您需要前往Google Cloud Platform后台创建服务账号。首先,在Google Cloud后台(https://console.cloud.google.com/)搜索“Google Play Console Developer”,然后点击启用即可。接下来,您可以创建服务账号。
已成功创建Google Play Console Developer,并成功获取对应的json文件。
3. 在Google Play Console 后台,将刚刚创建的Google Play Console Developer服务账号添加到用户和权限列表中。
接下来,您可以选择将服务账号的权限直接授予管理员(admin permission),或者授予财务部门的权限。
点击API权限中的查看Play管理中心权限,可以查看应用权限和账号权限。务必确保为对应的Project授予相应的权限。
4. 现在我们已经成功配置好了Google Play Console Developer的环境,接下来就可以开始实现服务端订单的校验了。这一步需要使用Java服务端进行处理。
首先将google apis 接入到项目:
com.google.apis google-api-services-androidpublisher v3-rev20211125-1.32.1
校验代码:
改写后的文案如下: ```java @RestController public class GoogleController { @PostMapping(/) public ProductPurchase checkOrder(@RequestBody GooglePayDto googlePayDto, HttpServletRequest requestDto) throws IOException, GeneralSecurityException { // 使用服务帐户Json文件获取Google凭据 List scopes = new ArrayList(); scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER); ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource(classpath:static/刚下载的json文件,这里放到了static目录下); GoogleCredential credential = GoogleCredential.fromStream(resource.getInputStream()) .createScoped(scopes); // 使用谷歌凭据和收据从谷歌获取购买信息 HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JacksonFactory jsonFactory = new JacksonFactory(); AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential) .setApplicationName(应用程序名).build(); AndroidPublisher.Purchases purchases = publisher.purchases(); final AndroidPublisher.Purchases.Products.Get request = purchases.products().get(googlePayDto.getPackageName(), googlePayDto.getProductId(),googlePayDto.getPurchaseToken()); Sy... ```传入三个参数
包名:packageName
商品ID:产品编号
购买令牌:支付凭证
结果如下:
验证方式二:通过直接使用API接口进行验证。开发者文档地址:https://developers.google.com/android-publisher/authorization?hl=zh-cn
整体步骤:
这个API项目和登录项目是不同的。
激活Google Play Android Developer API
OAuth同意屏幕是指在开发者授权账号登录时,弹出的登录页面。
生成用于web应用的OAuth客户端ID
google play开发者后台,API权限菜单中关联刚刚创建的项目,一个google play账号只需要也只能关联一个api项目就行了,这个项目可以查询关联账号中的所有应用的订单
点击授权页面,使用Google开发者账号为项目进行授权,获取验证码。
使用code来获取refreshToken,这个token只会在第一次返回,并且需要永久保存(因为它非常重要)。如果不小心丢失了refreshToken,就需要重新创建一个OAuth客户端ID,并重复步骤6和7来获取新的refreshToken。
使用refreshToken进行刷新,获取新的accessToken后,即可通过该accessToken查询订单状态。需要注意的是,accessToken通常只在5分钟左右有效,在此之后需要再次使用refreshToken来获取新的accessToken。
详细步骤:第一步:
在Google API控制台上创建一个API项目
setp2:激活Google Play Android Developer API
请在Google上搜索“Google Play Android Developer API”,然后点击开启。
第三步:启用OAuth同意界面
第四步:创建OAuth客户端ID
创建页面和创建成功后的修改页面可以获取到clientId和clientSecret:
在这一步,API项目已经成功创建完成。
第五步:将API项目与Google Play后台关联:
步骤6:获取验证码
地址:https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={填写的重定向地址}&client_id={创建的clientId}
将上面的{XX}替换成创建api项目时填写的重定向地址,和clientId,然后将连接放到浏览器中打开,就会吊起授权界面,使用你的开发者账号授权登录
在浏览器中打开请求方式,进行授权后,URL将会重定向,并在此时获取到code。
在重定向地址上,我们可以观察到两个参数:code和scope。然而,我们只需要关注code这个参数。值得注意的是,这里的code是经过urlencode编码的,在使用时需要进行解码操作。
第七步:使用代码获取refreshToken。您可以通过Postman发起一个POST请求来完成此步骤。
链接:https://accounts.google.com/o/oauth2/token
请使用POST请求方式。
参数:grant_type=authorization_code
code=获取到的code(需要看看code中是否有%号,如果有需要urldecode)
在创建API项目时,需要指定client_id(即客户端ID)。
client_secret refers to the client secret (also known as the client key) generated during the creation of an API project.
重定向地址是在创建API项目时所设置的redirect_uri。
在这里获得了refreshToken,非常重要,请务必记住:将refreshToken保存下来。它只会在第一次请求中返回,并且后续相同的请求不会再返回refreshToken。如果不小心丢失了refreshToken,需要重新创建一个WebClientId,请务必注意。
如果再次调用此接口的结果是:
第八步:利用refreshToken获取accessToken
地址:https://accounts.google.com/o/oauth2/token
请使用POST请求方式。
参数:grant_type=refresh_token
refresh_token=新获得的refreshToken
在创建API项目时,需要指定client_id(即客户端ID)。
client_secret refers to the client secret (also known as the client key) generated during the creation of an API project.
第九步:最后一步,查询订单状态。
查询订单状态
请问你需要将这段话改写成什么样的形式呢?是要简化语言,还是需要重新组织结构?请提供更多具体信息,以便我能够更好地帮助你。
包名必须是在创建登录 API 项目时用于创建 Android 客户端 ID 的应用程序包名。
商品ID(productId)是指与购买商品相对应的唯一标识符。
购买成功后,您可以通过调用Purchase对象的getPurchaseToken()方法来获取令牌。
access_token:我们获得的accessToken
请求方式:GET
解释返回值:
{
purchaseTimeMillis: 16239806, // The number of milliseconds since the epoch (January 1, 1970) representing the time of product purchase.
purchaseState: 0, // The purchase status of the order. Possible values are: 0. Purchased 1. Cancelled 2. Pending
consumptionState: 0, //产品的消费状态。可能的取值为:0. 尚未消耗 1. 已消耗
developerPayload:
"orderId": "GPA.XXXXXXX8",//google订单号 "purchaseType": 0,
acknowledgementState: 0, can be rewritten as The acknowledgement state is set to 0.
kind: androidpublisher#productPurchase,
obfuscatedExternalAccountId: SDK2106180944530041, // This field is used to store user information during customer payment, as advised by Google. It should not be too long, otherwise the client may encounter payment issues.
obfuscatedExternalProfileId:
regionCode: HK
}
实际上,只需服务端判断consumptionState的值即可。如果其值为1,表示已经消费完成,接下来就可以执行后续逻辑,例如发放金币或开启VIP等操作。
~~to: Ly
还木有评论哦,快来抢沙发吧~