我们已经准备好了,你呢?

2024我们与您携手共赢,助您领跑移动端创新增长!

  1.你应该知道的前提知识1.1沙箱环境

  沙箱环境 (Beta) 是支付宝开放平台为您提供的与线上环境完全隔离的联调测试环境,在沙箱环境中完成的调用不会对线上数据造成任何影响,尤其适合涉及资金链路的能力的调试。

  1.2 配置沙箱应用环境

  开放平台-沙箱环境 (alipay.com)

  使用支付宝,扫描后,进行简单用户注册

  使用开发者账号登录 开放平台控制台 > 开发服务,(用支付宝扫码,授权登录,然后根据页面输入注册信息)

  输入注册后,即可点击 研发服务 即可进入 沙箱环境。

  1.3 三个重要的信息

  商户UID,公钥 ,私钥,程序中需要使用:

  点击3 可以看公私匙

  ![](支付宝支付的开发步骤.assets/image-20211203204239614.png

  2.创建maven web项目

  以商品购买为例演示,请忽略页面样式效果

  3.额外添加依赖包

  com.alipay.sdkalipay-sdk-java4.9.28.ALL4.封装配置和调用支付工具类4.1 AlipayConfig

  配置类,用来封装支付宝的三个重要信息: 商户UID,公钥 ,私钥

  package com.bobo.utils;public class AlipayConfig {public static String app_id = "20210118*****";//uidpublic static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCckRs3v/nnhbjyg3obpNuf+r9a7VvmVpj0pc/z4WiF001xSxSqA2WLL5SF2xvfsVqc0LifTu0yQwFGtsgOrEN62LNmObcNr1gJPN6DVWrL8+BritRc0/VukaHx6YbXM90NvUv6Or/rOVoH6hXHICaBOC1dxHo8yXXk4w58EtBSAuTMOTaO1zHNnb3gSO0VtjF0MIMpWG1cBUVKFWihD6+BVen3CVrtIVc9yv4PtLCNviOMiVX3a7bU3jwYrhov2RZmPHdaNNMFICioGgdOUtufMxrstk6UdtjUV07EKqRcpilJIm2r4ZPAgiYVAB69WyZp9NpZRPwWO+dMlZnYlXSzAgMBAAECggEAPXJxxTKSKPyBdqSFMv5DuAjYOx529/K6uyGseOHZfNROS42VAZitrK+y0coJqYRYtLzyCIwYaaaEhLVN1Pxky3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O/JmAtsfgCgNYFgGeUOHOUheCW2uEgmp2xlPEHPxv4AuzePcaENqY+g==";public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvvuUyc6SO4CwpeHx1nHyKrYw5alwoBVIjb6hV15N2ntOCradUk7Tp69nm08yLE9W6nIv/H8CrKVMv/FX7zJOugT4EGQbqs3rADpQLu2of8YqCz4aXISktKO//g5SeMH4ejWUk/P3TWNplJrbqkAuzRrCxRHWj8AAeFiBNxlUeJFdGMWnXIrr3doBVhAoD+SjPnchOFFxAjZ6MR/75FZKMjU/M8pmG5fcD8dS/9OtSbVDa8NFn19SVeyzwpOHuA4eOdCc3cacOYGutke5dR0CRJxPMQUTeeQebkfgKnA3EjCjGovSryu7gKIiQCbBHvc61s9K1wYWh97odtzngfHlBwIDAQAB";public static String notify_url = "";//public static String return_url = "";//支付完回调地址,用来查询订单支付状态public static String sign_type = "RSA2";public static String charset = "utf-8";public static String gatewayUrl = "";}4.2 AlibabapayGateWayUtils

  支付和查询支付状态类

  package com.bobo.utils;import com.alibaba.fastjson.JSON;import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.request.AlipayTradePagePayRequest;import com.alipay.api.request.AlipayTradeQueryRequest;import com.alipay.api.response.AlipayTradeQueryResponse;import java.util.HashMap;import java.util.Map;public class AlibabapayGateWayUtils {/***调用支付接口* @param orderID 订单号* @param total_amount 支付金额* @param subject 商品名称* @param body 商品描述* @return*/public static String createOrder(String orderID,String total_amount,String subject,String body) throws AlipayApiException {Map map = new HashMap<>();map.put("out_trade_no",orderID);map.put("trade_no",orderID);map.put("total_amount",total_amount);map.put("subject",subject);// 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。String timeout_express = "1c";map.put("timeout_express",timeout_express);map.put("product_code","FAST_INSTANT_TRADE_PAY");AlipayClient alipayClient = getAlipayClient();//设置请求参数AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();alipayRequest.setReturnUrl(AlipayConfig.return_url);alipayRequest.setNotifyUrl(AlipayConfig.notify_url);alipayRequest.setBizContent(JSON.toJSONString(map));String result = alipayClient.pageExecute(alipayRequest).getBody();return result;}private static AlipayClient getAlipayClient() {return new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);}/*** 支付后,用来查询订单状态信息* @param map* @return* @throws AlipayApiException*/public AlipayTradeQueryResponse queryOrder(Map map) throws AlipayApiException {AlipayClient alipayClient = getAlipayClient();//构造API请求AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();request.setBizContent(JSON.toJSONString(map));//订单允许的最晚付款时间//发送请求AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);System.out.println("请求支付宝查询接口返回的结果为:"+response.getBody());return response;}}5.项目中使用工具类

  去支付

  @ResponseBody@RequestMapping(value = "goPay", produces = "text/html; charset=UTF-8")public String goPay(Orders orders) throws AlipayApiException {String s = AlibabapayGateWayUtils.createOrder(orders.getId(), orders.getTotalMoney()+"", "测试数据", "描述");return s;}6.效果6.1 商品列表

  6.2 购买

  6.3 跳转到订单页面

支付宝app支付开发(一步步带你搞定支付宝支付的开发案例)

  6.4 点击支付宝支付6.4.1跳转到支付页面

  如果使用点下一步,则进入下图

  6.4.2 支付页面

  输入密码:111111

  6.4.3 交易成功

  6.4.4 自动跳转到 回调页面

  即AilipayConfig的配置的回调地址

  6.4.5 订单信息

我们凭借多年的APP小程序开发经验,坚持以“个性定制 源码交付 独立部署 满意为止”为宗旨,累计为5000多家客户提供品质服务,得到了客户的一致好评。即刻开启你的小程序/APP,帮您轻松抢占千亿流量红利,助您轻松获客!
立即咨询: 13716188458 / 18588225959,助您抢占市场先机。项目经理在线

我们已经准备好了,你呢?

2024我们与您携手共赢,助您领跑移动端创新增长

售前咨询
咨询电话

13716188458

18588225959

在线留言
扫码加微信
微信
在线留言
* 请输入姓名
* 请输入有效联系方式
请输入您的需求:
* 请输入需求
提交成功