一.支付宝支付
1. 集成说明
1.1 作为当下最热门的支付宝和微信支付,相信有很多的app都很乐意的将支付宝集成在app中,毕竟只要你开发的app有一点涉及到买卖的,都需要集成支付宝或者微信支付.那么接下来我将为大家一一列举举出我在集成支付宝和微信中遇到的难点和其中出现的一些坑.
2. 集成前的准备(里面的步骤在集成文档中都有介绍)
2.1 注册支付宝帐号(账户最好采用公司邮箱注册)——附上网址
<code class="hljs perl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">https:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//www</span>.baidu.com/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">s</span>?wd=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E6</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%94</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BB</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%98</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%9D</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BC</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%80</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%8F</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%91</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E8</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%80</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%85</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B9</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B3</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%8F</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B0</span>&rsv_spt=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&rsv_iqid=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xfe0a29290002df6b</span>&issp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&f=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>&rsv_bp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>&rsv_idx=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>&ie=utf-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>&tn=monline_3_dg&rsv_enter=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&rsv_sug3=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>&rsv_sug1=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>&rsv_sug7=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>&rsv_sug2=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>&prefixsug=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E6</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%94</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BB</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%98</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%9D</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E9</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%9B</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%86</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E6</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%88</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%90</span>&rsp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>&inputT=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9885</span>&rsv_sug4=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10467</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2.2 设置(申请)支付密码
2.3 设置密码提示问题
2.4 绑定手机
2.5 创建应用(目的 : 获取APPID帐号)
2.6 然后就等待支付宝那边审核通过了
3. 开始集成
3.1 打开集成文档
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-label" style="box-sizing: border-box;">https:</span>//doc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.open</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.alipay</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>/</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
3.2 按步骤执行
3.2.1 点击文档中的 支付能力—->App支付—->SDK下载(https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1)—->SDK&DEMO(将demo和sdk下载下来)
3.2.2 运行支付宝支付Demo,了解支付宝整个支付的流程(如果里面出现错误,直接百度或者查看文档,里面有详细的解释,我这里就不再一一说明了)
4. 系统交互流程(红色部分是重点部分)
4.1 流程 : 商户客户端发送请求(获取签名后的订单信息)—->商户服务端(返回签名后的订单信息sign)—->调用支付宝支付接口,发送支付请求—->返回支付结果
5. 订单 : 由服务器生成
6. 注意 : 只需得到服务器返回的sign
7. 导入相关文件(此处导入文件最好看官方文档,比较正式)
8. 导入相关库
8.1 TARGETS—->General—->Linked Frameworks and Libraries—->点击”+”导入相关的库(此处导入的文件当已开发文档中规定的为主)
8.2 填写支付宝URL Schemes : TARGETS—-> info—-> URL Types(5)—-> 点击”+”对支付宝的添加(此处当已开发文档中规定的为主)—-> 结果图如下
9. 说明
9.1 支付宝的集成本来是和Demo上代码书写的顺序一样,但是由于我们后台是第一次做支付,所以最终无法返回一个正确的sign,这样导致我们集成的时候中间有一个步骤是多余的.
10. 集成
10.1 发送请求给后台,请求需要的字串(由于我们做的app设计到需要传入10个参数给后台,才能返回结果)—-> 下面贴上请求代码
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> activityId = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.ActivityId</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *stringActivityId = [@(activityId) stringValue]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//活动ID</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userId = [AppConfig shareInstance]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.UserID</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//用户ID</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> setObject:stringActivityId forKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityId"</span>]; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> setObject:userId forKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userId"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *bodyStr = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.title</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *subjectStr = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.title</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span> total_amount = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.totalFee</span>; NSDateFormatter* formatter = [NSDateFormatter new]; [formatter setDateFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"yyyy-MM-dd HH:mm:ss"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *lastDic = @{ @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"body"</span>:bodyStr, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"subject"</span>:subjectStr, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"total_amount"</span>:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%.2ld"</span>,(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span>)total_amount], @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"spbillCreateip"</span>:[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> deviceIPAdress] }; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> *bigDic = [lastDic mutableCopy]; [bigDic addEntriesFromDictionary:dic];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
10.2 发送请求(里面包含了调用支付宝的接口)
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[[[SpeedService alloc]init]FetchUserPayProduct:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> paramDic:bigDic completionBlock:^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> response, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSError</span> *error) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"++++%@"</span>,response); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//服务器返回的订单信息</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *samllDic = [response objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"result"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//服务器返回的签名</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *sign = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sign"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//签名Encode编码</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *locationSignEncode = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> encodeValue:sign]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建订单模型对象并且赋值</span> Order *order = [Order new]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.biz_content</span> = [BizContent new]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.app_id</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"app_id"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.biz_content</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"biz_content"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.charset</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"charset"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.method</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"method"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.notify_url</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"notify_url"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sign_type</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sign_type"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.timestamp</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"timestamp"</span>]; order<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.version</span> = [samllDic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"version"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//订单信息Encode编码</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *messageEncode = [order orderInfoEncoded:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最终订单信息字符串Encode编码</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *orderOneString = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@&sign=%@"</span>, messageEncode, locationSignEncode]; [[AlipaySDK defaultService] payOrder:orderOneString fromScheme:Scheme callback:^(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *resultDic) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"服务器返回的结果:%@"</span>,resultDic); }]; }];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li></ul>
10.3 签名Encode编码
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*)encodeValue:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*)value { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* encodedValue = value; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (value<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { encodedValue = (__bridge_transfer <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)value, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>, (__bridge CFStringRef)@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"!*'();:@&=+$,/?%#[]"</span>, kCFStringEncodingUTF8 ); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> encodedValue; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
10.4 注意
1. 订单信息Encode编码就不需要自己手动编码了,直接调用支付宝提供的接口,接口里面已经对排序,编码进行了设置,直接调用就行
2. 里面的集成步骤和Demo中的有所不同,你们也可以根据自己的需求,酌情的增加或者减少.
11. 支付宝如果按照上面的步骤集成,应该是没问题的,文章结尾后会贴上我集成的时候遇见的bug和坑,敬请期待(支付宝集成完毕)
二 . 微信支付
1. 集成说明 : 相对于支付宝集成,微信集成要简单的多了
2. 集成前的准备(完成一下需要的帐号信息)
2.1 微信帐号
2.2 微信帐号密码
2.3 微信支付商户号
2.4 商户平台登录帐号
2.5 商户平台登录密码
2.6 应用APPID
3. 注意 : 以上步骤在微信支付平台上都有说明,我这里就不一一介绍了
4. 微信开放平台(附上网址:只要注册过了,就可以直接登录查看相关信息)—>选择
[微信支付]APP支付开发者文档
<code class="hljs perl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">https:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//www</span>.baidu.com/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">s</span>?wd=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%A1</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E6</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%94</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BB</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%98</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B9</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B3</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%8F</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%B0</span>&rsv_spt=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&rsv_iqid=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x970a0db100003d17</span>&issp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&f=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>&rsv_bp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>&rsv_idx=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>&ie=utf-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>&tn=monline_3_dg&rsv_enter=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>&rsv_sug3=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>&rsv_sug1=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>&rsv_sug7=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>&rsv_sug2=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>&prefixsug=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E5</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AE</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%A1</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E6</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%94</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%AF</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%E4</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%BB</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%98</span>&rsp=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>&inputT=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7099</span>&rsv_sug4=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7846</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
5. 下载微信支付sdk
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-label" style="box-sizing: border-box;">https:</span>//pay<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.weixin</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.qq</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>/wiki/doc/api/app/app<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.php</span>?chapter=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>_1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
6. 运行Demo(如果出现错误,直接照着官方文档修改或者百度就行—>通常下载微信支付sdk运行是不存在问题的)
7. 交互时序图(红色框是重点部分)
8. 总结 : 我们只需要给后台请求,返回需要的7个参数就可以了
9. 导入相关文件(注意 : 以微信支付开发文档为准)
10. 添加 URL Types(5)微信—-> 如果在集成微信支付之前使用了微信分享,那该步骤可以省略
TARGETS—->info—->URL Types(5)—->点击”+”添加微信URL Schemes(如下图)
11. 向微信注册AppID(代码如下)—-> 第一个空 : AppID(最好使用宏) 第二个空 : 可以随便填写
11.1 注意 : 如果在集成微信支付之前集成了微信分享,那么该句代码必须写在分享注册的代码之后
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)application:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIApplication</span> *)application didFinishLaunchingWithOptions:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *)launchOptions { [WXApi registerApp:WXapp_id withDescription:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"测试demo"</span>]; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
12. 集成代码一(给服务器发送请求—->得到需要的7个参数openID,partnerId,prepayId,nonceStr,timeStamp,package,sign)
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark - 微信支付</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setUpWxPay { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> activityId = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.ActivityId</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *stringActivityId = [@(activityId) stringValue]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userId = [AppConfig shareInstance]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.UserID</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userTel = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mobile"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (userTel<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> || userTel<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>) { [SVProgressHUD showErrorWithStatus:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入正确的联系电话"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userName = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (userName<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { [SVProgressHUD showErrorWithStatus:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入姓名"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userIdentity = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"identity"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> identity = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> isCorrect:userIdentity]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *userEmail = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.paramDic</span> objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"email"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (userEmail<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { [SVProgressHUD showErrorWithStatus:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入邮箱号"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span> totalFee = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.totalFee</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *body = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.title</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *subject = _activitiy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.title</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *spbillCreateip = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> deviceIPAdress]; SpeedService *speedService = [[SpeedService alloc] init]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{ @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"body"</span>:body, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"subject"</span>:subject, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userId"</span>:userId, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"activityId"</span>:stringActivityId, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:userName, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"email"</span>:userEmail, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mobile"</span>:userTel, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"identity"</span>:userIdentity, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"total_amount"</span>:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d"</span>,totalFee], @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"spbillCreateip"</span>:spbillCreateip }; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"字典是 : %@"</span>,dict); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (identity == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//判断是否安装了微信</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([WXApi isWXAppInstalled]){ [speedService FetchUserWxPayProduct:dict completionBlock:^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> response, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSError</span> *error) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!error) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"返回的结果是 : %@"</span>,response); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * str = [response objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"code"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([str isEqualToString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"0000"</span>]) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dic = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionaryWithDictionary:[response objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"result"</span>]]; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> configRequest:dic stringStr:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; } }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"网络超时,返回的错误信息是 : %@"</span>,error); } }]; }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ [SVProgressHUD showErrorWithStatus:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请安装微信"</span>]; } }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ [SVProgressHUD showErrorWithStatus:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"请输入正确的身份证号码"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li></ul>
13. 调用微信支付代码,即可完成微信支付
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark - 调用微信支付的接口</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)configRequest:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *)dic stringStr:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)stringStr{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//需要创建这个支付对象</span> PayReq *req = [[PayReq alloc] init]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由用户微信号和AppID组成的唯一标识,用于校验微信用户</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.openID</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"appid"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 商家id,在注册的时候给的</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.partnerId</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"partnerid"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.prepayId</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"prepayid"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 随机编码,为了防止重复的,在后台生成</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.nonceStr</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"noncestr"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 这个是时间戳,也是在后台生成的,为了验证支付的</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableString</span> *stamp = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"timestamp"</span>]; req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.timeStamp</span> = stamp<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.intValue</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 根据财付通文档填写的数据和签名</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这个比较特殊,是固定的,只能是即req.package = Sign=WXPay</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.package</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"package"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 这个签名也是后台做的</span> req<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sign</span> = [dic objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sign"</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//发送请求到微信,等待微信返回onResp</span> [WXApi sendReq:req]; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul>
14. 特别说明 : 由于调用微信支付的7个参数都是服务器返回来的,不需要客户端做任何事情.当然有些参数客户端也是可以自己生成的,这就留给你们自己去处理了.
三. 总结
1. 集成支付宝出现的错误 :
1.1 能拿到服务器那边的sign,但是在调用支付宝接口的时候,代码运行并不会执行支付宝接口的block块,并且打印信息直接是提示今天已经注册报道了.
—-> 1.1.1 解决办法:将info–URL Types(5)–URL Schemes中配置的长度该为高于6个字串,很有可能是因为重名的问题.(直接重命名就行)
1.2 支付宝注册时间问题 : 在8月9号之前注册的用户,使用支付宝2.0的版本会出现问题,但是可以使用支付宝1.0版本的(重申:如果解签再重新签约的话,就可以使用支付宝2.0,并且兼容支付宝1.0的sdk)
—-> 1.2.1 解决办法 : 解签,再重新签约大约花1个工作日的时间
1.3 集成支付宝的时候,给自己的服务器发送请求得到sign.当打印请求拼接的参数的时候,经过仔细对比,发现其中会出现key和value值顺序颠倒问题
—-> 1.3.1 解决办法:将原来导入的sdk删除,重新倒入一次sdk就可以解决问题(很有可能是sdk造成的问题,我重新导入就解决了这问题)
1.4 签名(sign)出现的问题: 运用服务器返回的sign拼接成字符串,真机上出现系统繁忙,请重试.
—-> 1.4.1 解决方案 : 经过和支付宝提供的demo对比,支付宝的demo是完全可以实现支付功能,但是字符串加上后台返回的sign就出现这样的错误提示,说明签名有问题,这时只能叫后台返回正确的签名.
1.5 顺序问题 :如果服务器返回的sign没问题,那么最终就是的发送给支付宝参数拼接顺序问题了,要严格按照服务器返回的字串顺序拼接,然后再请求支付宝接口.(最后的出的结论是,不需要程序员手动按照顺序拼接,直接调用支付宝提供的方法,就可以完成拼接).
2. 微信支付出现的错误 :
1.1 集成完微信支付之后,真机运行,微信支付的时候只出现一个白的确定按钮,点击的时候跳回原来的app,并且返回-2