摘要:分享牛系列、分享牛转载。第三方支付,java第三方支付。android第三方支付。
原文地址:http://blog.csdn.net/zwl5670/article/details/51219438
1、获取支付宝必须的参数
申请支付宝商户帐号,并且创建应用通过审核。获取商户PID。使用openssl生成支付宝公钥、私钥和pkcs8格式私钥(安卓必须是pkcs8格式的私钥,openssl工具在支付宝官方的SDK里面都有,也有使用介绍)。
/** 商户PID*/
public static final String PARTNER = "";
/** 商户收款账号*/
public static final String SELLER = "";
/** 商户私钥,pkcs8格式*/
public static final String RSA_PRIVATE = "";
/** 支付宝公钥*/
public static final String RSA_PUBLIC = "";
2、导入jar包
将alipaySdk-20151215.jar包放入应用工程的libs目录下。
3、修改AndroidManifest.xml文件
在AndroidManifest.xml文件中添加如下代码:
<activity android:name="com.alipay.sdk.app.H5PayActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:exported="false" android:screenOrientation="behind" android:launchMode="singleTop" android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
* 注意:添加启动模式 Android:launchMode=”singleTop”。
添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4、创建支付宝支付类
在支付宝的官方demo中找到Base64.Java,PayResult.java和SignUtils.java文件放在自己的项目中。
创建支付类:
为了保证后台可以正确的接收数据可以设置异步接收支付结果,商户提供一个http协议的接口,包含在参数里传递给快捷支付,即notify_url。支付宝服务器在支付完成后,会以POST方式调用notify_url,以xml数据格式传输支付结果。
//服务器异步通知页面路径 Url.PAYORDER 订单支付-支付宝支付完成异步回调接口
orderInfo += “¬ify_url=” + “\”” + Url.PAYORDER + “\”“;
public class AlipayPay {
private static final int SDK_PAY_FLAG = 1;
private static final int SDK_CHECK_FLAG = 2;
private Context context;
/** 在后台获取的商户订单号 */
private String tradeNo;
public AlipayPay(Context context, String tradeNo) {
this.context = context;
this.tradeNo = tradeNo;
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SDK_PAY_FLAG: {
PayResult payResult = new PayResult((String) msg.obj);
String resultInfo = payResult.getResult();
String resultStatus = payResult.getResultStatus();
String memo = payResult.getMemo();
LogUtils.e("resultStatus==" + resultStatus + "==memo==" + memo);
if (TextUtils.equals(resultStatus, "9000")) {
Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
} else {
if (TextUtils.equals(resultStatus, "8000")) {
Toast.makeText(context, "支付结果确认中", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT)
.show();
}
}
break;
}
case SDK_CHECK_FLAG: {
Toast.makeText(context, "检查结果为:" + msg.obj, Toast.LENGTH_SHORT)
.show();
break;
}
default:
break;
}
};
};
/** * call alipay sdk pay. 调用SDK支付 subject 商品名称 body 商品详情 price 该笔订单的资金总额 */
public void pay(String subject, String body, String price) {
if (TextUtils.isEmpty(PayConfig.PARTNER)
|| TextUtils.isEmpty(PayConfig.RSA_PRIVATE)
|| TextUtils.isEmpty(PayConfig.SELLER)) {
new AlertDialog.Builder(context)
.setTitle("警告")
.setMessage("需要配置PARTNER | RSA_PRIVATE| SELLER")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
}
}).show();
return;
}
String orderInfo = getOrderInfo(subject, body, price);
String sign = sign(orderInfo);
try {
sign = URLEncoder.encode(sign, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
final String payInfo = orderInfo + "&sign=\"" + sign + "\"&"
+ getSignType();
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask((Activity) context);
String result = alipay.pay(payInfo);
Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/** * check whether the device has authentication alipay account. * 查询终端设备是否存在支付宝认证账户 */
public void check(View v) {
Runnable checkRunnable = new Runnable() {
@Override
public void run() {
PayTask payTask = new PayTask((Activity) context);
boolean isExist = payTask.checkAccountIfExist();
Message msg = new Message();
msg.what = SDK_CHECK_FLAG;
msg.obj = isExist;
mHandler.sendMessage(msg);
}
};
Thread checkThread = new Thread(checkRunnable);
checkThread.start();
}
/** * get the sdk version. 获取SDK版本号 */
public void getSDKVersion() {
PayTask payTask = new PayTask((Activity) context);
String version = payTask.getVersion();
Toast.makeText(context, version, Toast.LENGTH_SHORT).show();
}
/** * create the order info. 创建订单信息 */
public String getOrderInfo(String subject, String body, String price) {
String orderInfo = "partner=" + "\"" + PayConfig.PARTNER + "\"";
orderInfo += "&seller_id=" + "\"" + PayConfig.SELLER + "\"";
orderInfo += "&out_trade_no=" + "\"" + tradeNo + "\"";
orderInfo += "&subject=" + "\"" + subject + "\"";
orderInfo += "&body=" + "\"" + body + "\"";
orderInfo += "&total_fee=" + "\"" + price + "\"";
orderInfo += "¬ify_url=" + "\"" + Url.PAYORDER + "\"";
orderInfo += "&service=\"mobile.securitypay.pay\"";
orderInfo += "&payment_type=\"1\"";
orderInfo += "&_input_charset=\"utf-8\"";
orderInfo += "&it_b_pay=\"30m\"";
orderInfo += "&return_url=\"m.alipay.com\"";
return orderInfo;
}
/** * get the out_trade_no for an order. 生成商户订单号,该值在商户端应保持唯一(可自定义格式规范) * */
public String getOutTradeNo() {
SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",
Locale.getDefault());
Date date = new Date();
String key = format.format(date);
Random r = new Random();
key = key + r.nextInt();
key = key.substring(0, 15);
return key;
}
/** * sign the order info. 对订单信息进行签名 * * @param content * 待签名订单信息 */
public String sign(String content) {
return SignUtils.sign(content, PayConfig.RSA_PRIVATE);
}
/** * get the sign type we use. 获取签名方式 * */
public String getSignType() {
return "sign_type=\"RSA\"";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
5、调起支付
AlipayPay alipayPay=new AlipayPay(context,moneySn);
alipayPay.pay("钱包充值", "支付宝充值","100");
具体问题还是需要查看支付宝的官方文档,如果文档查询不到建议找支付宝客服解决问题。