下載下傳 SDK:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
申請資格:
http://kf.qq.com/faq/120911VrYVrA150906F3qqY3.html
開發文檔:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=3_1
導入類庫

将下載下傳好的 微信支付 SDK 拉到工程裡面
裡面的 read_me.txt 裡面有對應的重要說明
在 info.plist 裡面添加
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
然後檢視 info.plist
如果檔案顯示如下則成功添加:
接着到 URL Types 添加你的微信開發者帳号ID
接下來開始寫代碼了
AppDelegate.h
#import "AppDelegate.h"
#import "WXApi.h"
//必須遵守協定
@interface AppDelegate ()<WXApiDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
微信測試APPID wxb4ba3c02aa476ea1
1.導入微信支付 SDK,注冊微信支付
2.設定微信的 APPID 為 URL Schemes
3.發起支付,調起微信支付
4.處理支付結果
*/
[WXApi registerApp:@"你的 ID" withDescription:@"wxdemo"];
return YES;
}
-(void)onResp:(BaseResp *)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
//傳回支付結果,實際支付結果需要取微信服務端查詢
NSString *strMsg = @"支付結果";
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付成功";
NSLog(@"支付成功-PaySuccess,resp.errCode = %d",resp.errCode);
break;
default:
strMsg = @"支付失敗";
NSLog(@"支付失敗-PaySuccess,resp.errCode = %d,resp.errStr = %@",resp. errCode,resp.errStr);
break;
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%d",resp.errCode ]message:resp.errStr preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:sure];
[self.window.rootViewController presentViewController:alertController animated:YES completion:^{
}];
}
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
ViewController.m
#import "ViewController.h"
#import "WXApi.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)payBtnAction:(id)sender {
[ViewController jumpToBizPay];
}
+ (NSString *)jumpToBizPay {
if (![WXApi isWXAppInstalled]) {
NSLog(@"該裝置沒有安裝微信");
return @"該裝置沒有安裝微信";
}
if (![WXApi isWXAppSupportApi]) {
NSLog(@"該設定不支援微信支付");
return @"該設定不支援微信支付";
}
//============================================================
// V3&V4支付流程實作
// 注意:參數配置請檢視伺服器端Demo
// 更新時間:2015年11月20日
//============================================================
NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
//解析服務端傳回json資料
NSError *error;
//加載一個NSURL對象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//将請求的url資料放到NSData對象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ( response != nil) {
NSMutableDictionary *dict = NULL;
//IOS5自帶解析類NSJSONSerialization從response中解析出資料放到字典中
dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"url:%@",urlString);
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == ){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//調起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
//日志輸出
NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
return @"";
}else{
return [dict objectForKey:@"retmsg"];
}
}else{
return @"伺服器傳回錯誤,未擷取到json對象";
}
}else{
return @"伺服器傳回錯誤";
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end