首先需要导入商米支付paymentservice.jar包:下面介绍几个商米提供的工具类:
1.AppType
public class AppType {
private static HashMap<String, String> typeMap;
static {
/**
* 应用类型
* 00:银行卡应用
* 01:聚合扫码支付应用
* 02:银行卡+银联扫码支付应用
* 03-50由协议保留, 51往后为自定义扩展预留
*/
typeMap = new HashMap<>();
typeMap.put("00", "银行卡应用");
typeMap.put("01", "聚合扫码支付应用");
typeMap.put("02", "银行卡+银联扫码支付应用");
typeMap.put("51", "人脸");
}
public static boolean checkType(String type) {
return typeMap.containsKey(type);
}
}
2.Config
/**
* L3自定义配置类
*
* @author:sunhengzhi
* @date:2018-12-06
*/
public class Config {
/**
* 交易过程中是否显示UI界面(不包括结果页)
*/
private Boolean processDisplay;
/**
* 是否展示交易结果页
*/
private Boolean resultDisplay;
/**
* 是否打印小票
*/
private Boolean printTicket;
/**
* 指定签购单上的订单号类型
*/
private String printIdType;
/**
* 备注
*/
private String remarks;
public Boolean getProcessDisplay() {
return processDisplay;
}
public void setProcessDisplay(Boolean processDisplay) {
this.processDisplay = processDisplay;
}
public Boolean getResultDisplay() {
return resultDisplay;
}
public void setResultDisplay(Boolean resultDisplay) {
this.resultDisplay = resultDisplay;
}
public Boolean getPrintTicket() {
return printTicket;
}
public void setPrintTicket(Boolean printTicket) {
this.printTicket = printTicket;
}
public String getPrintIdType() {
return printIdType;
}
public void setPrintIdType(String printIdType) {
this.printIdType = printIdType;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
@Override
public String toString() {
return "Config{" +
"processDisplay=" + processDisplay +
", resultDisplay=" + resultDisplay +
", printTicket=" + printTicket +
", printIdType='" + printIdType + '\'' +
", remarks='" + remarks + '\'' +
'}';
}
}
3.InstallApkUtils
public class InstallApkUtils {
public static String smilePkgName = "com.alipay.zoloz.smile";
public static String SunmiPayPkgName = "com.sunmi.payment";// 商米收银服务
public static boolean checkApkExist(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
/**
* get App versionCode * @param context * @return
*/
public static int getVersionCode(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
int versionCode = 0;
try {
packageInfo = packageManager.getPackageInfo(packageName, 0);
versionCode = packageInfo.versionCode;
Log.e("@@@","versionCode=="+versionCode+" ");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
4.PaymentRequest
public class PaymentRequest {
/**
* 应用类型
* 00:银行卡应用
* 01:聚合扫码支付应用
* 02:银行卡+银联扫码支付应用
* 03-50由协议保留, 51往后为自定义扩展预留
*/
public String appType;
/**
* 交易类型
* 00-消费
* 01-撤消
* 02-退货
* 03-预授权
* 04-预授权撤销
* 05-预授权完成
* 06-预授权完成撤销
* 07-结算
* 08-签到
* A1-打印
* A2-本地交易记录查询
*/
public String transType;
/**
* 应用包名
*/
public String appId;
/**
* 交易金额
* 单位为分,1元表示为100L
*/
public long amount;
/**
* 支付码
*/
public String payCode;
/**
* MIS订单号
* 任意字符串,用于标识当笔交易的流水号,交易处理结果中会带回。
*/
public String misId;
/**
* 商户订单号
*/
public String orderId;
public String businessId;
/**
* 原MIS订单号
*/
public String oriMisId;
/**
* 原商户订单号
*/
public String oriOrderId;
/**
* 原交易日期
* 格式“MMdd”, 部分业务退货使用
*/
public String oriTransDate;
/**
* 原系统参考号
* 部分业务退货使用
*/
public String oriReferenceNum;
/**
* 原凭证号
* 撤销、打印使用
*/
public String oriVoucherNum;
/**
* 原授权码
* 预授权撤销使用
*/
public String oriAuthNum;
/**
* 是否打印小票
* 为“1”时打印;为“0”时不打印。默认打印
*/
public String printTicket;
public Config config;
public boolean checkBaseArgument() {
if (TextUtils.isEmpty(appId))
return false;
switch (TransType.getTransType(transType)) {
case CONSUME:
if (!AppType.checkType(appType)) {
return false;
}
if (amount <= 0)
return false;
return true;
case RETURN_MONEY:
if (TextUtils.isEmpty(oriMisId) && TextUtils.isEmpty(oriOrderId))
return false;
if (amount <= 0)
return false;
return true;
case QUERY:
if (TextUtils.isEmpty(orderId) && TextUtils.isEmpty(misId)) {
return false;
}
return true;
}
return false;
}
}
5.PaymentResponse
public class PaymentResponse {
/**
* 协议版本号
*/
public String version;
/**
* 交易结果返回码
* 00:成功
* 非00:失败, 见返回码定义
*/
public String resultCode;
/**
* 错误描述
*/
public String resultMsg;
/**
* 应用类型
*/
public String appType;
/**
* 交易类型
*/
public String transType;
/**
* MIS订单号
* 任意字符串,用于标识当笔交易的流水号,交易处理结果中会带回。
*/
public String misId;
/**
* 商户订单号
*/
public String orderId;
/**
* 交易金额
* 单位为分,1元表示为100L
*/
public long amount;
/**
* 已退款金额
* 单位为分,1元表示为100L
*/
public long refunded;
/**
* 实付金额
* 单位为分,1元表示为100L
*/
public long amount1;
/**
* 优惠金额
* 单位为分,1元表示为100L
*/
public long amount2;
/**
* 商家优惠金额
* 单位为分,1元表示为100L
*/
public long amount3;
/**
* 交易日期
* 格式“MMdd”
*/
public String transDate;
/**
* 交易时间
* 格式“HHmmss”
*/
public String transTime;
/**
* 凭证号
*/
public String voucherNum;
/**
* 批次号
*/
public String batchNum;
/**
* 参考号
*/
public String referenceNum;
/**
* 授权号
*/
public String authNum;
/**
* 卡号
*/
public String cardNum;
/**
* 发卡行
*/
public String issuer;
/**
* 收单行
*/
public String acquirer;
/**
* 操作员号
*/
public String operatorId;
/**
* 卡类型
*/
public String cardType;
/**
* 账户类型
*/
public String accountType;
/**
* 机型
*/
public String model;
/**
* 终端号
*/
public String terminalId;
/**
* 商户号
*/
public String merchantId;
/**
* 第三方平台订单号(微信、支付宝的订单号)
**/
public String platformId;
/**
* 原MIS订单号
*/
public String oriMisId;
/**
* 原商户订单号
*/
public String oriOrderId;
public String platform;
public String payCode;
}
6.TransType
public enum TransType {
//对外
CONSUME("00"),//-消费
PRE_AUTH("03"),//-预授权
PRE_AUTH_COMPLETE("05"),//-预授权完成
RETURN_MONEY("09"),//-退款
PRINT("A1"),//-打印
QUERY("A2"),//-本地交易记录查询
//内部使用
REVOKE("01"),//-撤消
RETURN_GOODS("02"),//-退货
PRE_AUTH_REVOKE("04"),//-预授权撤销
PRE_AUTH_COMPLETE_REVOKE("06"),//-预授权完成撤销
SETTLEMENT("07"),//-结算(换班)
SIGN_IN("08");//-签到
TransType(String code) {
this.code = code;
}
private String code;
public String Code() {
return code;
}
public static TransType getTransType(String code) {
if (TextUtils.isEmpty(code)) {
throw new IllegalArgumentException();
}
switch (code) {
case "00":
return CONSUME;
case "03":
return PRE_AUTH;
case "05":
return PRE_AUTH_COMPLETE;
case "09":
return RETURN_MONEY;
case "A1":
return PRINT;
case "A2":
return QUERY;
}
throw new IllegalArgumentException();
}
}
上面这些类基本大家一看就能看明白,接下来在提供一个调用类,商米支付支持刷脸,扫码,刷卡等方式的支付。
SummiPayUtil
public class SummiPayUtil {
private static final String TAG = "PayMentPayPresenter";
Context context;
/**
* 初始化刷脸服务
*
* @param context getApplicationContext()
*/
public SummiPayUtil(Context context) {
this.context = context;
}
public boolean startFaceService(long orderId, String phoneNumber, String uuid) {
startFaceService(orderId, phoneNumber, 1, uuid);
return true;
}
public boolean isHaveSunmiPay() {
return InstallApkUtils.checkApkExist(context, InstallApkUtils.SunmiPayPkgName);
}
/**
* 开始刷脸
*
* @return
*/
public boolean startFaceService(long orderId, String phoneNumber, long money, String uuid) {
if (!isHaveSunmiPay()) {
MyToast.showLong("未安装商米收银");
return false;
}
if (TextUtils.isEmpty(phoneNumber)) {
phoneNumber = "";
}
execute("51", orderId, phoneNumber, money, uuid);
return true;
}
public boolean startPayCode(long orderId, String payCode, long money, String uuid) {
execute("01", orderId, payCode, money, uuid);
return true;
}
private void execute(String payMode, long orderId, String phoneNumber, long money, String uuid) {
Logger.e("liluo", "调用支付"+money);
Intent intent = new Intent();
intent.setAction("sunmi.payment.action.entry");
intent.setPackage("com.sunmi.payment");
PaymentRequest request = new PaymentRequest();
request.appType = payMode;
request.appId = context.getPackageName();
request.transType = TransType.CONSUME.Code();
request.amount = money;/*money*///商米支付最低单位是分
request.orderId = uuid;
request.businessId = String.valueOf(orderId);
request.printTicket = "0";
request.payCode = phoneNumber;
Config config = new Config();
config.setResultDisplay(false);//默认开启收银台结果页,关闭之后将不会显示sunmi收银台的结果页
config.setProcessDisplay(true);//默认开启收银台进度页,关闭后将先不会跳转至sunmi收银台,直接进行交易流程
request.config = config;
String jsonString = jsonString(request);
PaymentService.getInstance().callPayment(jsonString);
}
public String jsonString(PaymentRequest request) {
String string =new Gson().toJson(request);
return string;
}
/**
* 商米退款
* @param payMode
* @param orderId
* @param
* @param money
* @param uuid
*/
public void returnExecute(String payMode, long orderId, long money, String uuid,String oriOrderId) {
Logger.e("liluo", "调用商米退款"+money);
Intent intent = new Intent();
intent.setAction("sunmi.payment.action.entry");
intent.setPackage("com.sunmi.payment");
PaymentRequest request = new PaymentRequest();
request.appType = payMode;
request.appId = context.getPackageName();
request.transType = TransType.RETURN_MONEY.Code();
request.amount = money;/*money*///商米支付最低单位是分
request.orderId = uuid;
request.businessId = String.valueOf(orderId);
request.printTicket = "0";
request.oriOrderId = oriOrderId;
Config config = new Config();
config.setResultDisplay(false);//默认开启收银台结果页,关闭之后将不会显示sunmi收银台的结果页
config.setProcessDisplay(false);//默认开启收银台进度页,关闭后将先不会跳转至sunmi收银台,直接进行交易流程
request.config = config;
String jsonString = jsonString(request);
PaymentService.getInstance().callPayment(jsonString);
}
/**
* 支付查询
* @param
* @param orderId
*/
public void selectExecute(String orderId) {
Logger.e("liluo","查询订单号:"+orderId);
Logger.e("liluo", "调用商米查询");
Intent intent = new Intent();
intent.setAction("sunmi.payment.action.entry");
intent.setPackage("com.sunmi.payment");
PaymentRequest request = new PaymentRequest();
request.appId = context.getPackageName();
request.transType = TransType.QUERY.Code();
request.orderId = orderId;
request.printTicket = "0";
Config config = new Config();
config.setResultDisplay(false);//默认开启收银台结果页,关闭之后将不会显示sunmi收银台的结果页
config.setProcessDisplay(false);//默认开启收银台进度页,关闭后将先不会跳转至sunmi收银台,直接进行交易流程
request.config = config;
String jsonString = jsonString(request);
PaymentService.getInstance().callPayment(jsonString);
}
}
调用就非常简单了,就不举例子了,有问题欢迎私信。