支付|支付宝小程序(支付)

1.首先要注册企业账号,并创建小程序,在支付宝开发工具中与项目做关联
2.配置环境https://docs.alipay.com/mini/introduce/pay根据步骤做小程序开发准备并签约小程序支付。
支付|支付宝小程序(支付)
文章图片

下面配置是小程序的信息,可根据https://blog.csdn.net/wwt1996/article/details/86065209对密钥进行配置,AES可以不设置
支付|支付宝小程序(支付)
文章图片

3.根据小程序授权的授权码获取buyer_id即返回的userId
支付|支付宝小程序(支付)
文章图片

package cn.icr.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.request.AlipayTradeCreateRequest; import com.alipay.api.response.AlipaySystemOauthTokenResponse; import com.alipay.api.response.AlipayTradeCreateResponse; import cn.icr.util.AlipayConfigApp; import cn.icr.util.GenerateOrderNoUtil; @RestController @RequestMapping("/order") public class PayController2 { privateGenerateOrderNoUtilgu=newGenerateOrderNoUtil(); @RequestMapping("/getuserid") publicStringreturncode(@RequestParam(value="https://www.it610.com/article/token")String token){ //向支付宝发送请求 //获得初始化的AlipayClient //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigApp.gatewayUrl, AlipayConfigApp.app_id, AlipayConfigApp.merchant_private_key, "json", AlipayConfigApp.charset, AlipayConfigApp.alipay_public_key, AlipayConfigApp.sign_type); AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); request.setCode(token); //这个就是第一步获取的auth_code request.setGrantType("authorization_code"); //这个固定值,参考https://docs.open.alipay.com/api_9/alipay.system.oauth.token try { AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request); System.out.println(oauthTokenResponse.getBody()); System.out.println(oauthTokenResponse.getUserId()); return oauthTokenResponse.getUserId(); } catch (AlipayApiException e) { //处理异常 e.printStackTrace(); } return ""; } @RequestMapping("/pay") publicStringtopay(@RequestParam(value="https://www.it610.com/article/pid")String pid){ Stringout_trade_no=gu.gen("R",530L); System.out.println("买家账号"+pid); //实例化客户端 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigApp.gatewayUrl, AlipayConfigApp.app_id, AlipayConfigApp.merchant_private_key, "json", AlipayConfigApp.charset, AlipayConfigApp.alipay_public_key, AlipayConfigApp.sign_type); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.create. AlipayTradeCreateRequest request = new AlipayTradeCreateRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数。 request.setBizContent("{" + "\"out_trade_no\":\""+out_trade_no+"\"," + "\"total_amount\":0.01," + "\"subject\":\"买东西\"," + "\"buyer_id\":\""+pid+"\"" + "}"); try { //使用的是execute AlipayTradeCreateResponse response = alipayClient.execute(request); String trade_no = response.getTradeNo(); //获取返回的tradeNO。 System.out.println("生成的单号"+trade_no); return trade_no; } catch (AlipayApiException e) { e.printStackTrace(); } return ""; } }

4.开始前端处理唤起支付
【支付|支付宝小程序(支付)】支付|支付宝小程序(支付)
文章图片

//小程序中传入唤醒支付 my.tradePay({ tradeNO: '传入返回的trade_no', success: function(res) { my.alert(res.resultCode); }, fail: function(res) { my.alert(res.resultCode); }, });

    推荐阅读