成功起调支付宝支付
下载支付sdk复制到libs文件夹下并add as library:https://docs.open.alipay.com/54/104509
1、首先申请支付宝 企业账户 链接:https://memberprod.alipay.com/account/reg/enterpriseIndex.htm
备注:企业账户是以邮箱申请
2、1登录支付宝企业账户,到产品中心创建 APP支付 产品
2 、2创建 APP支付 成功得到收款账户
3、打开手机支付宝APP扫描登录 蚂蚁金服|开发平台 链接https://auth.alipay.com/login/index.htm?goto=https%3A%2F%2Fdocs.open.alipay.com%2F204%2F105297%2F
登录后,点击 管理中心——>网页&移动应用——>支付接入——创建应用
创建成功,并签约 APP支付,显示已签约则成功
4、设置密钥,及APPID
鼠标放到【主账号】上,下拉,点击 密钥管理,,,,注意是mapi网关下的密钥
设置应用公钥:https://docs.open.alipay.com/291/105971
5、实现activity代码:
public class MainActivity extends AppCompatActivity {// 支付宝KEYpublic static final String PARTNER = "2088331094298926";//蚂蚁金服|开放平台 密钥APPIDpublic static final String SELLER = "200970769@qq.com";//支付宝企业账户public static final String RSA_PRIVATE ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCqBQZ8p0X+AB5so6Gfb3JWihBJHKwlzA8Gaj/pz4VwskubWqgDjMmGw9TWhSuQezbX8BVogdJz9vYkgb2WnTVoOd3" +"bD0JvemfW+CBZzmHpjCFEWuT0l8wcnoJuF8vlUztHoscqJbZNJO/MXe3SQq0/qXt/gV5yEzScwdlbfuifK8jAhGmyUxYzKDMsfpfhlYJlkSFxmegDmNb8d+xsQZdrmESYop9tf60fdFwikqy1U09hXdNeC5XdClgLy8ihge9sf8j5vLPKr" +"PhDlC/WoDYnuUNylHmRO78EscNRa89AMiwARsX3pI7Hp5jvHku7m6EFzAJIpRHgi1mT8HMbP+eLAgMBAAECggEAAQRWrZgyivplNDEmXlXEUVAyGL3bKb62uEsaZsOfALWQTjWUm7DT48J4G3BgelTCVzhQaHeeUqM/hahhAe2ezCjMcur8cr5A" +"sMB7MQ5S95qm+Emy5OOjjOhFz3LE/XI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Cv+qPNNC3Abm2qHMJF6FD2tmiOgS";//蚂蚁金服|开放平台 RSA(SHA256)密钥(推荐)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView textView = (TextView)findViewById(R.id.myte);textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {aliPay();}});}/*** sign the order info. 对订单信息进行签名** @param content 待签名订单信息*/public String sign(String content) {return SignUtils.sign(content, RSA_PRIVATE);}/*** get the sign type we use. 获取签名方式*/public String getSignType() {return "sign_type=\"RSA\"";
// return "sign_type=\"RSA2\"";}//支付宝private void aliPay() {// 调用接口传值String name = "天鑫网吧充值订单";String money = "0.01";String orderInfo = getOrderInfo(name, name, money);// 创建订单信息String sign = sign(orderInfo);// 对订单信息进行签名try {// 仅需对sign 做URL编码sign = URLEncoder.encode(sign, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}final String payInfo = orderInfo + "&sign=\"" + sign + "\"&" + getSignType();Runnable payRunnable = new Runnable() {@Overridepublic void run() {// 构造PayTask 对象
// PayTask alipay = new PayTask(UpgradePayActivity.this);
// // 调用支付接口
// String result = alipay.pay(payInfo, true);
// Message msg = new Message();
// msg.what = SDK_PAY_FLAG;
// msg.obj = result;
// handler.sendMessage(msg);DoPaymentTask paymentTask = new DoPaymentTask();paymentTask.execute(payInfo);}};Thread payThread = new Thread(payRunnable);payThread.start();}public String getTime(){long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳String str=String.valueOf(time);return str;}/*** create the order info. 创建订单信息*/public String getOrderInfo(String subject, String body, String price) {// 合作者身份IDString orderInfo = "partner=" + "\"" + PARTNER + "\"";// 卖家支付宝账号orderInfo += "&seller_id=" + "\"" + SELLER + "\"";// 商户网站唯一订单号orderInfo += "&out_trade_no=" + "\"" + getTime()+ "\"";// 商品名称orderInfo += "&subject=" + "\"" + subject + "\"";// 商品详情orderInfo += "&body=" + "\"" + body + "\"";// 商品金额orderInfo += "&total_fee=" + "\"" + price + "\"";// Constants.PAY_CHANGE_URL+"?code=alipay&user_id"// 服务器异步通知页面路径
// orderInfo += "¬ify_url=" + "\"" + "https://open.tencent.com/" + "\"";// 接口名称, 固定值orderInfo += "&service=\"mobile.securitypay.pay\"";// 支付类型, 固定值orderInfo += "&payment_type=\"1\"";// 参数编码, 固定值orderInfo += "&_input_charset=\"utf-8\"";// 设置未付款交易的超时时间// 默认30分钟,一旦超时,该笔交易就会自动被关闭。// 取值范围:1m~15d。// m-分钟,h-小时,d-天,1c-当天(无论交易何时创建,都在0点关闭)。// 该参数数值不接受小数点,如1.5h,可转换为90m。orderInfo += "&it_b_pay=\"30m\"";// 支付宝处理完请求后,当前页面跳转到商户指定页面的路径,可空orderInfo += "&return_url=\"m.alipay.com\"";// 调用银行卡支付,需配置此参数,参与签名, 固定值// orderInfo += "&paymethod=\"expressGateway\"";return orderInfo;}private class DoPaymentTask extends AsyncTask<String, Void, Map<String, String>> {@Overrideprotected Map<String, String> doInBackground(String... params) {// 构造PayTask 对象PayTask alipay = new PayTask(MainActivity.this);// 调用支付接口,获取支付结果Map<String, String> result = alipay.payV2(params[0], true);return result;}@Overrideprotected void onPostExecute(Map<String, String> result) {@SuppressWarnings("unchecked")PayResult payResult = new PayResult(result);/**对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。*/String resultInfo = payResult.getResult();// 同步返回需要验证的信息String resultStatus = payResult.getResultStatus();Log.i("lgq支付宝支付响应状态", resultStatus);if (resultStatus.equals("6001")){Toast.makeText(MainActivity.this,"支付取消",Toast.LENGTH_SHORT).show();}// 判断resultStatus 为9000则代表支付成功if (TextUtils.equals(resultStatus, "9000")) {Toast.makeText(MainActivity.this,"支付成功",Toast.LENGTH_LONG).show();// 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
// verifyPay();} else {// 该笔订单真实的支付结果,需要依赖服务端的异步通知。// 判断resultStatus 为非“9000”则代表可能支付失败// “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)if (TextUtils.equals(resultStatus, "8000")) {
// if (payResultListener != null) {
// payResultListener.onProcess();
// }} else {// 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
// if (payResultListener != null) {
// payResultListener.onFail();
// }}
// 9000 订单支付成功
// 8000 正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
// 4000 订单支付失败
// 5000 重复请求
// 6001 用户中途取消
// 6002 网络连接出错
// 6004 支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
// 其它 其它支付错误}}}
}
工具类
SignUtils
public class SignUtils {private static final String ALGORITHM = "RSA";private static final String SIGN_ALGORITHMS = "SHA1WithRSA";private static final String DEFAULT_CHARSET = "UTF-8";public static String sign(String content, String privateKey) {try {PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);PrivateKey priKey = keyf.generatePrivate(priPKCS8);java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);signature.initSign(priKey);signature.update(content.getBytes(DEFAULT_CHARSET));byte[] signed = signature.sign();return Base64.encode(signed);} catch (Exception e) {e.printStackTrace();}return null;}}
demo链接:https://download.csdn.net/download/meixi_android/10723852
在线交流bug:qq1085220040
2、支付宝支付2.0
账号申请与1.0一样,集成比1.0更高效简洁
权限:
<uses-permission android:name="android.permission.INTERNET"/>
1、引入支付宝SDK2.0
下载SDK链接:https://download.csdn.net/download/meixi_android/11362258
添加步骤:
(1)下载支付宝SDK2.0aar文件复制到libs文件夹下
(2)添加app的build.gradle下添加配置
// 支付宝 SDK AAR 包所需的配置
compile (name: 'alipaySdk-15.6.4-20190611174341', ext: 'aar')
(3)在项目的build.gradle文件夹下添加配置
allprojects {repositories {// 支付宝 SDK AAR 包所需的配置flatDir {dirs 'libs'}
2、实现代码:
(1)五个工具类都是固定值,直接cp可用
(2)activity代码实现调用支付宝支付
/*** 支付宝支付业务示例*/
public void payV2(View v) {if (TextUtils.isEmpty(APPID) || (TextUtils.isEmpty(RSA2_PRIVATE) && TextUtils.isEmpty(RSA_PRIVATE))) {showAlert(this, "支付参数缺失");return;}/** 这里只是为了方便直接向商户展示支付宝的整个支付流程;所以Demo中加签过程直接放在客户端完成;* 真实App里,privateKey等数据严禁放在客户端,加签过程务必要放在服务端完成;* 防止商户私密数据泄露,造成不必要的资金损失,及面临各种安全风险;** orderInfo 的获取必须来自服务端;*/boolean rsa2 = (RSA2_PRIVATE.length() > 0);Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);String orderParam = OrderInfoUtil2_0.buildOrderParam(params);String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);final String orderInfo = orderParam + "&" + sign;final Runnable payRunnable = new Runnable() {@Overridepublic void run() {PayTask alipay = new PayTask(MainActivity.this);Map<String, String> result = alipay.payV2(orderInfo, true);Log.i("msp", result.toString());Message msg = new Message();msg.what = SDK_PAY_FLAG;msg.obj = result;mHandler.sendMessage(msg);}};// 必须异步调用Thread payThread = new Thread(payRunnable);payThread.start();
}