首先先让大家看看开发出来的卡是什么样子的如下:
开发出来是这个效果,其中包括步骤如下:
快速接入
第一步:创建小程序
要在您的小程序内使用 商户会员卡功能,您需要首先完成 开发者入驻 并 创建小程序 。
第二步:添加功能
在小程序详情页的功能列表中添加“商户会员卡”和“支付宝卡包”两个功能包,如下图所示:
配置参数 | 获取方式/示例值 |
---|---|
URL(支付宝网关(固定)) | https://openapi.alipay.com/gateway.do |
APPID(APPID 即创建应用后生成) | 获取见(https://opendocs.alipay.com/open/200/105310#s5) |
APP_PRIVATE_KEY(开发者私钥,由开发者自己生成) | 获取见(https://opendocs.alipay.com/open/291/105971) |
FORMAT(参数返回格式,只支持 JSON) | JSON(固定) |
CHARSET(编码集,支持 GBK/UTF-8) | 开发者根据实际工程编码配置 |
ALIPAY_PUBLIC_KEY(支付宝公钥,由支付宝生成) | 获取详见(https://opendocs.alipay.com/open/291/105971) |
SIGN_TYPE(商户生成签名字符串所使用的签名算法类型,目前支持 RSA2 和 RSA,推荐使用 RSA2) | RSA2 |
第一步:创建卡模板
第二步:配置开卡表单(看个人业务了可有可无表单项)
第三步:获取投放链接
第四步:小程序内唤起会员开卡授权页面
第五步:获取用户填写的表单信息
第六步:商户开卡给用户
下面就是java写的接口说明了
/save 存支付宝信息接口
/getcode 根据 code获取AccessToken 和UserId(获取支付宝 唯一id)接口
/shenfenrz 拉取支付宝身份验证接口
/model 创建卡模板接口
/chuangjian 配置开卡表单信息接口
/fafang 投放链接接口
/userInfo 获取表单信息接口
/opencard 支付宝开卡接口
/imgurllog 获取卡包log图片接口
/imgurlbj 获取卡包背景图片接口
/delrz 删除支付宝信息接口
/delcard 会员卡销卡接口
其中/shenfenrz是拉取人脸识别认证的接口具体功能效果如下图:
上代码:
1.1 pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用外部的,不要打进去 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.0.ALL</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2 application.yml
server:
port: 8888
spring:
application:
name: cloud-provider-service
datasource:
# type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
# driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包
# url: jdbc:mysql://localhost:3306/db2020326?useUnicode=true&characterEncoding-utr-8&useSSL=false
# username: root
# password: root
url: jdbc:sqlserver://localhost;DatabaseName=dzrclkcs
username: sa
password: Dingzhou!#%
# url: jdbc:sqlserver://192.168.1.65;DatabaseName=ccflow-dzrclk
# username: sa
# password: admin123W
#使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entities #所有Entity别名类所在包
# configuration: map-underscore-to-camel-case=true
1.3 主启动
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@MapperScan("com.example.demo.dao")
@ImportResource(locations = {
"classpath:mapper/PaymentMapper.xml"})
public class DemoApplication extends SpringBootServletInitializer {
// 重写 configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.err.println("111111111");
}
}
1.4 业务类
package com.example.demo;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.example.demo.entities.NDShenPi;
import com.example.demo.entities.userinfo;
import com.example.demo.service.sfrz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
/**
* @author : 宋银义
* @date : 2020-05-28 09:48
**/
@Controller
public class shenfenrz {
@Autowired
private sfrz sfrz;
/**
* 个人回调地址
*/
public static final String REDIRECT_URI = "http://www.baidu.com";
/**
* 支付宝网关(固定)
*/
public static final String URL = "https://openapi.alipay.com/gateway.do";
/**
* APPID 即创建应用后生成
*/
public static final String APP_ID = "202100116xxx";
/**
* 开发者私钥,由开发者自己生成
*/
public static final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDWm+wVrCh70oqmRpUtA/RAn7JhbJJaJ9yznVFqMdHk7jhIIP5J2pxQbIDAb/5iA8TuyURp6yu0uq3B1+fuf9PL/A7p8b8V4lyrMMD+5KrXZtZO7TfOLQwm1D+BxkMaY6LhFXs0LMf6jEndz+IvZZY0mi4RmkplkEWBOy+UoueEcF/LFkNXAr3VQy6DztnN1rlTuic0Bbb+uRMJSdvn9/H1OmbTC5mqYFOM5uB2jZYR49lhyLew3o02SLbvtZ9ML5xlg9lm8RSQkLC/2LO9fk9QO+P9HB8POoDwmFH0Rwccuz+nwVC6nEQgouKlGrFb3s/yRlsUFBctSmncsIpLgKYZAgMBAAECggEBANZNHyPJJSf4ZzGA/AfWkfpSPgthIofalF3bs2fq1eDYLOWB5MuU/FCm+K6xZwEzKF0WKDXoe11eqJIP6m9TwAhK7A2atKjCwKzF44LkjuDHtStdYEOG4kKH4d7SP0HDyusey0efjBZaLozLwN55f9OJPoWfX7kppu5UTsALKEsrIeVozTIFKG6gN2BgmH54T5nBE8x0HYHq5qymyeHnCiIPaqaoTxHzQK/n4hjS3zbbU214WbbyVxOyphTTn9RbHlLUq0V0opMne2ycn1iEKMPY4MyePZWV/y1syqP7WZY0MyNpicN2LeXOMEkZpFGGgxGL7RmCdHO/iOhnp31tIjECgYEA/8asi72F6C1kGVVChl19RoEV1KPv5YQQ9m6V6roXhPUhhxNLu781UCvB/14NGeZfNKcIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
/**
* 参数返回格式,只支持json
*/
public static final String FORMAT = "json";
/**
* 编码集,支持GBK/UTF-8
*/
public static final String CHARSET = "UTF-8";
/**
* 支付宝公钥,由支付宝生成
*/
public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq9xTpJDfyWSwel7o6KXQ92o2Y5qSCMHoC8FFn5vFibWD/s5TcGSVzyMgiBrqqCPyY5Qxxxxxxxxxxxxxxxx";
/**
* 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
*/
public static final String SIGN_TYPE = "RSA2";
/**
* 接口权限值,目前只支持auth_user和auth_base两个值-->>
* auth_base:以auth_base为scope发起的网页授权,是用来获取进入页面的用户的userId的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(通常是业务页面)。
* auth_user:以auth_user为scope发起的网页授权,是用来获取用户的基本信息的(比如头像、昵称等)。但这种授权需要用户手动同意,用户同意后,就可在授权后获取到该用户的基本信息。若想获取用户信息,scope的值中需要有该值存在,如scope=auth_user,auth_base。
*/
public static final String SCOPE = "auth_base";
@GetMapping("/delcard")
@ResponseBody
public HashMap<String, String> selectcard(String appid) throws AlipayApiException {
HashMap<String, String> map = new HashMap<>();
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayMarketingCardDeleteRequest request = new AlipayMarketingCardDeleteRequest();
UUID uuid = UUID.randomUUID();
String uuid1=String.valueOf(uuid);
userinfo userinfo=sfrz.selectopcardid(appid);
String opencardid = "";
if (userinfo.getOpencardid()!=null){
opencardid = userinfo.getOpencardid();
}else {
opencardid = "";
}
long time = new Date().getTime();
request.setBizContent("{" +
"\"out_serial_no\":\""+uuid1+"\"," +
"\"target_card_no\":\""+opencardid+ "\"," +
"\"target_card_no_type\":\"BIZ_CARD\"," +
"\"reason_code\":\"USER_UNBUND\"," +
"\"ext_info\":\"{\\\"new_card_no\\\":\\\""+time+"\\\",\\\" donee_user_id\\\":\\\""+appid+"\\\"}\"" +
" }");
AlipayMarketingCardDeleteResponse response = alipayClient.execute(request);
String AlipayResultS = response.getBody();
JSONObject AlipayResult = JSONObject.parseObject(AlipayResultS);
if (AlipayResult != null) {
JSONObject AlipayJson = AlipayResult.getJSONObject("alipay_marketing_card_delete_response");
try {
String msg = AlipayJson.getString("msg");
String biz_serial_no = AlipayJson.getString("biz_serial_no");
String code = AlipayJson.getString("code");
map.put("msg",msg);
map.put("biz_serial_no",biz_serial_no);
map.put("code",code);
} catch (Exception e) {
e.getMessage();
}
}
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return map;
}
@GetMapping("/delrz")
@ResponseBody
public HashMap<String, String> delappid(String sfzh){
HashMap<String, String> map = new HashMap<>();
int i = sfrz.delappid(sfzh);
if (i>0){
map.put(