iOS- 微信支付 (服務器調起支付 )以及回調不成功的緣由 不看後悔

寫的不錯,給留個言哈...前端

一. 支付準備工做c++

1. 微信相關準備工做sql

(1) 向微信官方開通支付功能. 這個不是前端的工做.服務器

(2) 導入官方下載的微信支付SDK包. 我用的是微信開放平臺下載的SDK 1.6.2微信

(3) 導入必要的庫文件網絡

     SystemConfiguration.framework,微信開發

     libz.dylib,app

     libsqlite3.0.dylib,ide

     libc++.dylib,post

     CoreTelephoy.framework (坑一: 這個庫是必要的,可是微信官方文檔中沒有說到要導入)

(4) 該項目中的Bundle Identifier 應該填向微信註冊的Bundle Identifier

(5) 註冊微信 (回調的時候用到,告訴微信,從微信返回到哪一個APP)

     Target --> info --> URL Types --> +按鈕 --> 填寫identifier 和 URL Schemes. 前一個是標識符,通常填@"weixin".後一個是註冊的微信appId. 好比"wx19a984b788a8a0b1".(註釋: 假的appid)

(6) 添加微信白名單

     info.plist --> 右擊 --> open as  --> source Code --> 添加白名單

     我是在<key>CFBundleVersion</key>這一行上面添加的. 注意保持正確的鍵值對.別插錯了.

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
    </array>

(7) 若是支付成功,回調方法不執行,或者回調不成功.請再次確認(4)(5)(6),是否填寫正確. 

(8) 運行一下,不報錯.報錯,再次確認(1)--(7)步驟.

二.代碼相關

1. 在AppDelegate.h中

(1) 導入微信類 #import @"WXApi.h".

(2) 遵照微信代理方法 <WXApiDelegate>

2. 在APPDelegate.m中

(1) 註冊微信

#pragma mark 註冊微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    //註冊 微信
    /**
     參數1 : 微信Appid
     參數2 : 對項目的描述信息(用項目名稱)
     */
    [WXApi registerApp:@"微信Appid" withDescription:@"雲宴"];
    
    return YES;
}

 (2) 跳轉方法,並設置代理

#pragma mark 跳轉處理
//被廢棄的方法. 可是在低版本中會用到.建議寫上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WXApi handleOpenURL:url delegate:self];
}
//被廢棄的方法. 可是在低版本中會用到.建議寫上

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}

//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [WXApi handleOpenURL:url delegate:self];
}

(3) 微信回調方法 (注意: 不要寫成Req方法了)

#pragma mark 微信回調方法

- (void)onResp:(BaseResp *)resp
{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);

    NSString * errStr       = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);

    
    NSString * strTitle;
    //判斷是微信消息的回調 --> 是支付回調回來的仍是消息回調回來的.
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"發送媒體消息的結果"];
    }

    NSString * wxPayResult;
    //判斷是不是微信支付回調 (注意是PayResp 而不是PayReq)

    if ([resp isKindOfClass:[PayResp class]])
    {
        //支付返回的結果, 實際支付結果須要去微信服務器端查詢
        strTitle = [NSString stringWithFormat:@"支付結果"];
        switch (resp.errCode)
        {
            case WXSuccess:
            {
                strMsg = @"支付結果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
case WXErrCodeUserCancel: { strMsg = @"用戶取消了支付"; NSLog(@"用戶取消支付: %d",resp.errCode); wxPayResult = @"cancel"; break; } default: { strMsg = [NSString stringWithFormat:@"支付失敗! code: %d errorStr: %@",resp.errCode,resp.errStr]; NSLog(@":支付失敗: code: %d str: %@",resp.errCode,resp.errStr); wxPayResult = @"faile"; break; } } //發出通知 從微信回調回來以後,發一個通知,讓請求支付的頁面接收消息,而且展現出來,或者進行一些自定義的展現或者跳轉 NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult]; [[NSNotificationCenter defaultCenter] postNotification:notification]; } }

 3. 在ViewController.h (進行支付請求的類)

 暫時沒有任何操做

4. 在ViewController.m中(進行支付的請求的類)

(1) 導入微信庫 #import @"WXApi.h"

(2) 監聽APPDelegate.m中發送的通知

#pragma mark 監聽通知
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //檢測是否裝了微信軟件
    if ([WXApi isWXAppInstalled])
    {
        
        //監聽通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }
}

 

#pragma mark - 事件
- (void)getOrderPayResult:(NSNotification *)notification
{
    NSLog(@"userInfo: %@",notification.userInfo);
    
    if ([notification.object isEqualToString:@"success"])
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    else
    {
        [self alert:@"提示" msg:@"支付失敗"];
    }
}

//客戶端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alter show];
}

(3) 移除通知

#pragma mark 移除通知
- (void)dealloc 
{
//移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; }

(4) 調起微信去支付

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self pay_button];
}

#pragma mark - 實現方法

#pragma mark 建立支付按鈕
- (void)pay_button
{
    self.payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.payButton.frame = CGRectMake(10, 100, 300, 40);
    self.payButton.backgroundColor = [UIColor orangeColor];
    [self.payButton setTitle:@"去支付" forState:UIControlStateNormal];
    [self.payButton addTarget:self action:@selector(payButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.payButton];
}

#pragma mark - 點擊事件
- (void)payButtonClicked
{
    [self sendNetWorking_WXPay];
}

- (void)sendNetWorking_WXPay
{
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",YYIP,@"wx/pay"];
    
    NSDictionary * parameter = @{
                                 @"pay_type"       : @"1",
                                 @"total_price"    : @"10",
                                 @"appointment_id" : @"208"
                                 };
    
    [self sendNetWorkingWith:urlStr andParameter:parameter];
}

#pragma mark 網絡請求 -->> post
- (void)sendNetWorkingWith:(NSString *)url andParameter:(NSDictionary *)parameter
{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        //網絡請求回來的8個參數.詳見微信開發平臺
        
        NSDictionary * result = responseObject[@"result"];
        
        NSString * appid         = result[@"appid"];
        NSString * noncestr      = result[@"noncestr"];
        NSString * package1      = result[@"package1"];
        NSString * partnerid     = result[@"partnerid"];
        NSString * paySign       = result[@"paySign"];
        NSString * prepayid      = result[@"prepayid"];
        NSString * timestamp     = result[@"timestamp"];
        
        
//        NSString * err_code      = result[@"err_code"];
//        NSString * timeStamp     = result[@"timeStamp"];
//        NSString * tradeid       = result[@"tradeid"];

        
    [self WXPayRequest:appid nonceStr:noncestr package:package1 partnerId:partnerid prepayId:prepayid timeStamp:timestamp sign:paySign];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 調起微信支付
- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{
    //調起微信支付
    PayReq* wxreq             = [[PayReq alloc] init];
    
    wxreq.openID              = appId; // 微信的appid
    wxreq.partnerId           = partnerId;
    wxreq.prepayId            = prepayId;
    wxreq.nonceStr            = nonceStr;
    wxreq.timeStamp           = [timeStamp intValue];
    wxreq.package             = package;
    wxreq.sign                = sign;
    
    [WXApi sendReq:wxreq];
}

三.上代碼

1. AppDelegate.h

#import <UIKit/UIKit.h>

#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

2. AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


#pragma mark 註冊微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    //註冊 微信
    /**
     參數1 : 微信Appid
     參數2 : 對項目的描述信息(用項目名稱)
     */
    [WXApi registerApp:@"wx09a984b788a8a0b0" withDescription:@"雲宴"];
    
    return YES;
}

#pragma mark 跳轉處理
//被廢棄的方法. 可是在低版本中會用到.建議寫上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WXApi handleOpenURL:url delegate:self];
}
//被廢棄的方法. 可是在低版本中會用到.建議寫上

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}

//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [WXApi handleOpenURL:url delegate:self];
}

#warning 這一步中  不要錯誤的把req 當成了 resp
#pragma mark 微信回調方法
- (void)onResp:(BaseResp *)resp
{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);
    
    NSString * errStr       = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);
    
    
    NSString * strTitle;
    //判斷是微信消息的回調
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"發送媒體消息的結果"];
    }
    
    
    NSString * wxPayResult;
    //判斷是不是微信支付回調 (注意是PayResp 而不是PayReq)
    if ([resp isKindOfClass:[PayResp class]])
    {
        //支付返回的結果, 實際支付結果須要去微信服務器端查詢
        
        strTitle = [NSString stringWithFormat:@"支付結果"];
        
        switch (resp.errCode)
        {
            case WXSuccess:
            {
                strMsg = @"支付結果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
                case WXErrCodeUserCancel:
            {
                strMsg = @"用戶取消了支付";
                NSLog(@"用戶取消支付: %d",resp.errCode);
                wxPayResult = @"cancel";
                break;
            }
            default:
            {
                strMsg = [NSString stringWithFormat:@"支付失敗! code: %d  errorStr: %@",resp.errCode,resp.errStr];
                NSLog(@":支付失敗: code: %d str: %@",resp.errCode,resp.errStr);
                wxPayResult = @"faile";
                break;
            }
        }
        
        //發出通知
        NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

@end

3. ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

4. ViewController.m

#import "ViewController.h"

#import "AFHTTPSessionManager.h"
#import "WXApi.h"

#define YYIP       @"http:公司IP地址"


@interface ViewController ()

@property (nonatomic, strong) UIButton * payButton;


@end

@implementation ViewController

#pragma mark 監聽通知
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //檢測是否裝了微信軟件
    if ([WXApi isWXAppInstalled])
    {
        
        //監聽通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }
}

#pragma mark 移除通知
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self pay_button];

}

#pragma mark - 實現方法

#pragma mark 建立支付按鈕
- (void)pay_button
{
    self.payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.payButton.frame = CGRectMake(10, 100, 300, 40);
    self.payButton.backgroundColor = [UIColor orangeColor];
    [self.payButton setTitle:@"去支付" forState:UIControlStateNormal];
    [self.payButton addTarget:self action:@selector(payButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.payButton];
}

#pragma mark - 點擊事件
- (void)payButtonClicked
{

    if ([WXApi isWXAppInstalled])

    {

      [self sendNetWorking_WXPay];

    }

     else

    {

      [self alert:@"提示" msg:@"未安裝微信"];

    }

}

- (void)sendNetWorking_WXPay { NSString * urlStr = [NSString stringWithFormat:@"%@%@",YYIP,@"wx/pay"]; NSDictionary * parameter = @{ @"pay_type"       : @"1", @"total_price"    : @"10", @"appointment_id" : @"208" }; [self sendNetWorkingWith:urlStr andParameter:parameter]; } #pragma mark 網絡請求 -->> post
- (void)sendNetWorkingWith:(NSString *)url andParameter:(NSDictionary *)parameter { AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@",responseObject); //網絡請求回來的8個參數.詳見微信開發平臺
 NSDictionary * result = responseObject[@"result"]; NSString * appid         = result[@"appid"]; NSString * noncestr      = result[@"noncestr"]; NSString * package1      = result[@"package1"]; NSString * partnerid     = result[@"partnerid"]; NSString * paySign       = result[@"paySign"]; NSString * prepayid      = result[@"prepayid"]; NSString * timestamp     = result[@"timestamp"]; // NSString * err_code = result[@"err_code"]; // NSString * timeStamp = result[@"timeStamp"]; // NSString * tradeid = result[@"tradeid"];
 [self WXPayRequest:appid nonceStr:noncestr package:package1 partnerId:partnerid prepayId:prepayid timeStamp:timestamp sign:paySign]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); }]; } #pragma mark - 調起微信支付
- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{ //調起微信支付
    PayReq* wxreq             = [[PayReq alloc] init]; wxreq.openID = appId; // 微信的appid
    wxreq.partnerId           = partnerId; wxreq.prepayId = prepayId; wxreq.nonceStr = nonceStr; wxreq.timeStamp = [timeStamp intValue]; wxreq.package = package; wxreq.sign = sign; [WXApi sendReq:wxreq]; } #pragma mark - 事件
- (void)getOrderPayResult:(NSNotification *)notification { NSLog(@"userInfo: %@",notification.userInfo); if ([notification.object isEqualToString:@"success"]) { UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alertView show]; }
else if([notification.object isEqualToString:@"cancel"])
{
[self alert:@"提示" msg:@"用戶取消了支付"];
}
else { [self alert:@"提示" msg:@"支付失敗"]; } } //客戶端提示信息 - (void)alert:(NSString *)title msg:(NSString *)msg { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alter show]; } @end
相關文章
相關標籤/搜索