天天看點

iOS內建微信支付功能

第一步:內建微信的SDK

https://pay.weixin.qq.com/wiki/doc/api/index.html     

點選進入

iOS內建微信支付功能
iOS內建微信支付功能

下載下傳對應SDK或示例,最後可以看看示例程式

第二步:在Xcode中填寫微信開放平台申請的Appid

iOS內建微信支付功能

Xcode>info>URL Types  中建立加入Appid

第三步:在Appdelegate.m 中注冊微信支付 和回調

#import "WXApi.h"

添加 代理

WXApiDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    //self.window.backgroundColor = [UIColor clearColor];

    // 微信支付注冊

    [WXApiregisterApp:PAY_WEIXIN_ID];

    returnYES;

}

// ios 9.0以上系統版本回調

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options

{

    // 微信

    if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) {

        [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self];

    }

    // 支付寶

    if ([url.schemeisEqualToString:@"SearchPigeonWorld"]) {

        //跳轉支付寶錢包進行支付,處理支付結果

        [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {

            if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {

                [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO];

            }

        }];

    }

    returnYES;

}

//支付成功時調用,回到第三方應用中 ios 9.0以下系統版本回調

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    // 微信

    if ([url.schemeisEqualToString:PAY_WEIXIN_ID])

    {

        [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self];

    }

    // 支付寶

    if ([url.hostisEqualToString:PAY_ALIPAY_appID]) {

        //跳轉支付寶錢包進行支付,處理支付結果

        [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {

            if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {

                [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO];

            }

        }];

    }

    returnYES;

}

- (void)onResp:(BaseResp*)resp

{

    if([respisKindOfClass:[PayRespclass]]){

        BOOL isPaySuccess =NO;

        switch (resp.errCode) {

            caseWXSuccess:

                isPaySuccess = YES;

                break;

            caseWXErrCodeUserCancel:

                isPaySuccess = NO;

                break;

            caseWXErrCodeSentFail:

                isPaySuccess = NO;

                break;

            caseWXErrCodeAuthDeny:

                isPaySuccess = NO;

                break;

            default:

                isPaySuccess = NO;

                break;

        }

        if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {

            [self.appMyDelegatepayCenterWeixinOnResultWith:isPaySuccess];

        }

    }

}

第四步:在使用微信的地方調用支付方法

#pragma mark 2.2.14(10)使用微信進行付款,擷取微信加密資訊

- (void)getWebResponsePayWeixinInfo {

    NSDictionary *parameters =@{@"key" :appDelegate.userKeyString,

                                 @"foundRecordId" : [self.payInfoDictobjectForKey:@"foundRecordId"]};

    [MBProgressHUDshowMessage:@""];

    [WebDataResponseInterfaceSessionManagerPostWebDataWithApi:WEBInterFace_Good_CreateWeiXinPayOrderandParameters:parameters andSuccess:^(id successObject) {

        MYLOG(@"%@", successObject);

        [MBProgressHUDhideHUD];

        if ([successObject[@"status"]isEqualToString:@"success"]) {

            successObject = [successObject objectForKey:@"value"];

            // 微信支付

            //需要建立這個支付對象

            PayReq *req   = [[PayReqalloc] init];

            //由使用者微信号和AppID組成的唯一辨別,用于校驗微信使用者

            req.openID = successObject[@"appid"];

            // 商家id,在注冊的時候給的

            req.partnerId = [successObjectobjectForKey:@"partnerid"];

            // 預支付訂單這個是背景跟微信伺服器互動後,微信伺服器傳給你們伺服器的,你們伺服器再傳給你

            req.prepayId  = [successObjectobjectForKey:@"prepayid"];

            // 根據财付通文檔填寫的資料和簽名

            //這個比較特殊,是固定的,隻能是即req.package = Sign=WXPay

            req.package   = [successObjectobjectForKey:@"package"];

            // 随機編碼,為了防止重複的,在背景生成

            req.nonceStr  = [successObjectobjectForKey:@"noncestr"];

            // 這個是時間戳,也是在背景生成的,為了驗證支付的

            req.timeStamp = [[successObjectobjectForKey:@"timestamp"]doubleValue];

            // 這個簽名也是背景做的

            req.sign = [successObjectobjectForKey:@"sign"];

            //發送請求到微信,等待微信傳回onResp

            [WXApisendReq:req];

        } else {

            [MBProgressHUDshow:[successObject objectForKey:@"value"]icon:nilview:self.view];

        }

    } andFailure:^(NSError *error) {

        [MBProgressHUDhideHUD];

        MYLOG(@"error: %@", error);

    }];

}