今天博主有一個微信支付的需求,遇到了一些困難點,在此和大家分享,希望能夠共同進步.
1.申請AppID,導入依賴庫,配置URL Scheme這里就不深究了,各位看官請自行百度.
2.在AppDelegate中導入頭文件,遵守WXApiDelegate,然后在launch里注冊AppID.
3.在支付界面導入頭文件,創建支付對象,調起微信
//調起微信支付,傳入參數
PayReq* req = [[[PayReq alloc] init]autorelease];
req.openID = APPI_ID;
req.partnerId = PARTNER_ID;
req.PRepayId = prePayid;
req.nonceStr = nonce_str;
req.timeStamp = now;
req.package = package;
req.sign = sign;
[WXApi safeSendReq:req];
4.微信支付的回調需要添加在AppDelegate中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//返回應用后的回調,通過handleOpenURL進入微信支付的回調onResp:(BaseResp*)resp
return [WXApi handleOpenURL:url delegate:self];
}
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp *response = (PayResp *)resp;
NSString *strTitle = [NSString stringWithFormat:@"支付結果"];
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitlemessage:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
switch (response.errCode) {
case WXSuccess: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
但是將微信支付相關的代碼全部寫到AppDelegate中又會顯得代碼冗余,這時我們需要創建通知,在成功回調后發出通知,在支付界面監聽回調,完成支付.
5.在監聽回調中,上傳我們從微信支付中獲得的參數,完成支付.
http://www.jianshu.com/p/97d38b00e53d?plg_nld=1&plg_auth=1&plg_nld=1&plg_dev=1&plg_uin=1&plg_usr=1&plg_vkey=1&plg_nld=1&plg_uin=1&plg_nld=1&plg_auth=1&plg_usr=1&plg_vkey=1&plg_dev=1
http://www.tuicool.com/articles/32UFBzQ?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1
http://www.jianshu.com/p/6e75a450dcc4?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1
新聞熱點
疑難解答