支付宝支付的对接(支付宝小程序)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_36834189/article/details/83504644

最近一段时间接触了小程序以及其他一些相关的支付宝支付的对接,看了很长时间的开发文档。

对此也有自己的一些看法,在此记录。

具体的一些链接:

小程序支付接入:https://docs.alipay.com/mini/introduce/pay

接口相关参数文档:https://docs.open.alipay.com/api_1/alipay.trade.create/

生成RSA密钥工具:https://docs.open.alipay.com/291/105971/

样式:

  

一些前置工作在此不多说了。(审核申请等)

支付流程:

 

首先下载工具获得私钥和公钥。

拷贝商户应用公钥。

登录支付宝,右上角的账号管理中 设置应用公钥(需要绑定手机号以及一系列审核)

然后在代码中使用的全是支付宝公钥,不是应用公钥

导入pom依赖:

  <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>3.3.87.ALL</version>
  </dependency>

贴一段主要代码:

/**
 * @Author xty
 * @Description
 * @Date 18:03 2018/10/26
 */
@Service("payService")
@Transactional(rollbackFor = Exception.class, value = "transactionManager")
public class PayServiceImpl implements PayService {

    /**
     * 商户appid
     */
    public static String APPID = "";
    /**
     * 请求网关地址
     */
    public static String URL = "https://openapi.alipay.com/gateway.do";
    /**
     * 私钥
     */
     public static String RSA_PRIVATE_KEY = "";
    /**
     * 支付宝公钥
     */
    public static String ALIPAY_PUBLIC_KEY ="";
    /**
     * RSA2
     */
    public static String SIGNTYPE = "RSA2";
    /**
     * 返回格式
     */
    public static String FORMAT = "json";
    /**
     * 编码
     */
    public static String CHARSET = "UTF-8";


    @Override
    public AlipayTradeCreateResponse pay(AlipayRequest req) {
        AlipayClient alipayClient = new
                DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET,
                ALIPAY_PUBLIC_KEY, SIGNTYPE);

        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest ();

        request.setBizContent("{"+
                "\"out_trade_no\""+":"+"\""+IdUtil.nextId()+"\""+","+
                "\"total_amount\""+":"+"\""+req.getTotalAmount()+"\""+","+
                "\"subject\""+":"+"\""+req.getSubject()+"\""+","+
                "\"buyer_id\""+":"+"\""+req.getBuyerId()+"\""+
                "}"
        );
        AlipayTradeCreateResponse response = null;
        try {
            response = alipayClient.execute(request);

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }
}

上面具体参数根据自己公司的来填写。

setBizContent中的业务参数可以看文档根据自己需要来传。

返回参数:

之后前端拿到参数进行接口调用。

my.tradePay({
		tradeNO: '2017111521001104105336677922',  
		success: function(res) {
			my.alert(res.resultCode);
		},
		fail: function(res) {
          my.alert(res.resultCode);
       },
	});

 

关于buyer_id获取(userid)

相关授权:

前端获取auth_code,传给后端后返回userid和token,后端进行相关一系列业务处理。(token存入redis中)


/**
 * @Author xty
 * @Description
 * @Date 18:03 2018/10/26
 */
@Service("payService")
@Transactional(rollbackFor = Exception.class, value = "transactionManager")
public class PayServiceImpl implements PayService {

    @Autowired
    private ICache iCache;

    @Autowired
    private UcUserRelationService ucUserRelationService;

    @Override
    public AlipaySystemOauthTokenResponse oauth(AliOauthTokenRequest request) throws AlipayApiException {

        AlipayClient alipayClient = new
                DefaultAlipayClient(AlipayConstant.URL, AlipayConstant.APPID, AlipayConstant.RSA_PRIVATE_KEY, AlipayConstant.FORMAT, AlipayConstant.CHARSET,
                AlipayConstant.ALIPAY_PUBLIC_KEY, AlipayConstant.SIGNTYPE);

        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        alipaySystemOauthTokenRequest.setGrantType(request.getGrantType());
        alipaySystemOauthTokenRequest.setCode(request.getCode());
        alipaySystemOauthTokenRequest.setRefreshToken(request.getRefreshToken());

        AlipaySystemOauthTokenResponse response = alipayClient.execute(alipaySystemOauthTokenRequest);
        if (response.isSuccess()) {
            String userId = response.getUserId();
            String accessToken = response.getAccessToken();
            //1.存入关系表
            UcUserRelation ucUserRelation = new UcUserRelation();
            ucUserRelation.setId(IdUtil.nextId());
            ucUserRelation.setUserId(request.getLoginUserId());
            ucUserRelation.setObjType(UcUserRelationObjType.ALIPAY.value);
            ucUserRelation.setObjId(Long.parseLong(userId));
            ucUserRelation.setObjNo(request.getCode());
            ucUserRelation.setCreateBy(Long.parseLong("0"));
            ucUserRelation.setGmtCreate(new Date());
            ucUserRelation.setModifyBy(Long.parseLong("0"));
            ucUserRelation.setGmtModify(new Date());
            ucUserRelationService.insertSelective(ucUserRelation);
            //2.token存入redis
            iCache.setString(userId,accessToken,30,TimeUnit.MINUTES);
            return response;
        } else {
            return response;
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页