支付宝APP支付(IOS手机端+java后台)版
程序员文章站
2023-12-05 20:20:22
0.介绍预览
针对需要在ios手机上接入原生微信支付场景,调用微信进行支付。如图:
1.资料准备
1.1 账号注册
打开https://openhome.a...
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]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: this.$toast() 了解一下?