因为项目需求,支付方式添加了微信支付。之前做过银联的支付,所以,感觉不会太难。
使用的SDK为1.7.是13年12月份的,还是手动管理内存。
实际支付的代码不难,就是几个配置,因为项目之前做过微信分享,所以配置这步就省略了。
具体支付代码:
<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;">//调起微信支付 PayReq* req = [[[PayReq alloc] init]autorelease]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openID</span> = APPI_ID<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.partnerId</span> = PARTNER_ID<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.prepayId</span> = prePayid<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nonceStr</span> = nonce_str<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.timeStamp</span> = now<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.package</span> = package<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> req<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sign</span> = sign<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> [WXApi safeSendReq:req]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</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></ul>
不得不吐槽,微信支付的SDK,这里连个代理都没有,所以,你想获取支付后的结果的返回信息,需要去AppDelegate里,添加如下代码,添加微信的代理:
<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 openURL:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *)url sourceApplication:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sourceApplication annotation:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)annotation { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [WXApi handleOpenURL:url delegate:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</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></ul>
然后,再实现这个方法:
<code class="hljs erlang 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-pp" style="box-sizing: border-box;">-<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span> onResp:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">BaseResp</span>*)</span>resp</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>
通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在AppDelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:
<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;">void</span>) onResp:(BaseResp*)resp { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([resp isKindOfClass:[PayResp class]]) { PayResp *response = (PayResp *)resp; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// NSString *strTitle = [NSString stringWithFormat:@"支付结果"];</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];</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;">// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// message:strMsg</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// delegate:self</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// cancelButtonTitle:@"OK"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// otherButtonTitles:nil, nil];</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// [alert show];</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span> (response<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.errCode</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> WXSuccess: { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span> *notification = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span> notificationWithName:ORDER_PAY_NOTIFICATION object:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"success"</span>]; [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> defaultCenter] postNotification:notification]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">default</span>: { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span> *notification = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span> notificationWithName:ORDER_PAY_NOTIFICATION object:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"fail"</span>]; [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> defaultCenter] postNotification:notification]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</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></ul>
然后,在支付那个VC,添加通知
<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 - tabBar隐藏</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewWillAppear:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)animated { [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewWillAppear:animated]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>([WXApi isWXAppInstalled]) <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;">NSNotificationCenter</span> defaultCenter] addObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//监听一个通知</span> } } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark - tabbar还原</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewWillDisappear:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)animated { [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewWillDisappear:animated]; [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> defaultCenter]removeObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>];<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//移除通知</span> }</code>