您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
0.介绍预览
针对需要在IOS手机上接入原生微信支付场景,调用微信进行支付。如图:
1.资料准备
1.1 账号注册
打开https://openhome.alipay.com,注册支付宝开放平台开发者账号
1.2 开发者认证
登录,进入开发者中心,进行开发者资质认证,并创建移动应用。
1.3 签约应用
创建应用后上传相关资料,上线应用并通过审核。审核通过后点击应用,进行签约,此步骤不能省略,否则或报ISV权限不足。
1.4 应用配置
打开应用信息,配置一下内容,接口签名可下载支付宝签名验签工具进行生成,生成完成后将公钥复制到平台上。
2.Java后台开发
添加依赖
<!-- 支付宝支付依赖 --> <dependency> <groupId>com.pentahohub.nexus</groupId> <artifactId>alipay-sdk-java</artifactId> <version>20170615110434</version> <scope>system</scope> <systemPath>${basedir}/libs/alipay-sdk-java20170615110434.jar</systemPath> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency>
生成统一订单
/** * account 总金额 * sid 用户id * desc 节点id * @param request * @return */ @RequestMapping(value="/pay/alipay/signprams",produces="application/json;charset=utf-8") @ResponseBody public String signprams(HttpServletRequest request){ String res = "{code:404}"; try{ String totalAmount = request.getParameter("account"); String sid = request.getParameter("sid"); String nid = request.getParameter("desc"); String subject = "电费充值"; String body = "贵大电费预付费充值"; String prefix = "ALE"+sid+"E"; String out_trade_no = WXRequestUtil.getTradeNo(prefix); //实例化客户端 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do ", AlipayUtil.APPID, AlipayUtil.PRIVATE_KEY, "json", AlipayUtil.CHARSET, AlipayUtil.PUBLIC_KEY, "RSA2"); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay AlipayTradeAppPayRequest alirequest = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.setPassbackParams(URLEncoder.encode(body.toString()));; //描述信息 添加附加数据 model.setSubject(subject); //商品标题 model.setOutTradeNo(out_trade_no); //商家订单编号 model.setTimeoutExpress("30m"); //超时关闭该订单时间 model.setTotalAmount(totalAmount); //订单总金额 model.setProductCode("QUICK_MSECURITY_PAY"); //销售产品码,商家和支付宝签约的产品码,为固定值QUICK_MSECURITY_PAY alirequest.setBizModel(model); alirequest.setNotifyUrl("http://pay.gzdxfszx.com/pay/alipay/notify.xs "); //回调地址 String orderStr = ""; try { //这里和普通的接口调用不同,使用的是sdkExecute AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alirequest); orderStr = response.getBody(); // System.out.println(orderStr);//就是orderString 可以直接给客户端请求,无需再做处理。 } catch (AlipayApiException e) { e.printStackTrace(); } String sign = MD5Encode.encode(out_trade_no); savePayLog(out_trade_no,totalAmount,sid,body, sign,nid,1); JSONObject resObj = new JSONObject(); resObj.put("code",ResponseCode.HTTP_SUCCESS); resObj.put("out_trade_no",out_trade_no); resObj.put("sign",sign); resObj.put("orderStr",orderStr); res = resObj.toJSONString(); }catch (Exception e){ e.printStackTrace(); } return res; }
异步通知处理
@RequestMapping(value="/pay/alipay/notify",produces="application/json;charset=utf-8") @ResponseBody public String notify(HttpServletRequest request){ Map requestParams = request.getParameterMap(); System.out.println("支付宝支付结果通知"+requestParams.toString()); //获取支付宝POST过来反馈信息 Map<String,String> params = new HashMap<String,String>(); for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } //乱码解决,这段代码在出现乱码时使用。 //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } //切记alipaypublickey是支付宝的公钥,请去open.alipay.com 对应应用下查看。 //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type) try { //验证签名 boolean flag = AlipaySignature.rsaCheckV1(params, AlipayUtil.PUBLIC_KEY, AlipayUtil.CHARSET, "RSA2"); if(flag){ if("TRADE_SUCCESS".equals(params.get("trade_status"))){ // //付款金额 // String amount = params.get("buyer_pay_amount"); // //支付宝交易号 // String trade_no = params.get("trade_no"); // //附加数据 // String passback_params = URLDecoder.decode(params.get("passback_params")); //商户订单号 String out_trade_no = params.get("out_trade_no"); updateOrderInfo(out_trade_no,MD5Encode.encode(out_trade_no)); } } } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }
3.IOS客户端开发
导入支付宝开发包
添加URL Types
在需要调用支付宝的地方实现方法
-(void)aliPay { [MBProgressHUD showMessage:@"跳转中,请稍候"]; NSString *appScheme = @"alisdkdemo"; NSString *userUrlStr = [NSString stringWithFormat:@"%@?sid=%@&account=%@&desc=%@", ALI_PREPAY_URL, self.student.sid,self.payJinE,self.student.nid]; NSURL *url = [NSURL URLWithString:userUrlStr]; // NSLog(@"userUrlStr = %@", userUrlStr); NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) { [MBProgressHUD hideHUD]; // NSLog(@"支付宝支付的response = %@", operation.responseString); NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *userDict = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil]; NSString *orderString = [userDict objectForKey:@"orderStr"]; self.sign = [userDict objectForKey:@"sign"]; self.ordnum = [userDict objectForKey:@"out_trade_no"]; [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { NSLog(@"reslut = %@",resultDic); [MBProgressHUD hideHUD]; NSString *resultStatus = [resultDic objectForKey:@"resultStatus"]; if([resultStatus isEqualToString:@"9000"]){ // 支付成功,更新用户信息 [self payDidFinish]; }else{ // 支付失败, [self setupAlertControllerWithTitle:@"支付宝支付结果" messge:@"本次支付未完成,您可以稍后重试!" confirm:@"好的"]; } }]; }failure:^(AFHTTPRequestOperation *operation, NSError *error) { [MBProgressHUD hideHUD]; NSLog(@"发生错误!%@",error); }]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:operation]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。