您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。