ios 支付宝

    集成支付宝和微信的时候  很多人都会遇到坑  我的建议是 show in finder  在工程内部建一个文件夹 然后把支付宝相关的文件导进去 add到工程中       后面的支付什么的  so简单

    特别说明 我用的版本是 2.0    最新好像是2.1吧  写支付宝sdk那群王八蛋 天天改参数名字  显示自己很牛逼      要调起支付宝手机端  需要配置白名单   

   

    直接代码了  参数 说明 

    NSString *partner = @"商户号";

    NSString *seller = @"收款账号";

    NSString *privateKey = @"私钥";//自己生成

      if ([partner length] == 0 || [seller length] == 0){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少partner或者seller。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alert show];

        return;

    }

    Order *order  = [[Order alloc] init];//订单类 去下载支付宝demo 有Order类 导进来就能用

    order.partner = partner;

    order.seller  = seller;

    order.tradeNO = @"订单号";//一般后台生成  如果要自己生成 支付

    order.productName= @"商品名字";

    order.productDescription = @"商品的描述";

    order.amount = @"价钱";

    order.notifyURL = @"后台回调地址";//

    //常量类型

    order.service = @"mobile.securitypay.pay";

    order.paymentType = @"1";

    order.inputCharset = @"utf-8";

    order.itBPay = @"30m";

    order.showUrl = @"m.alipay.com";

    

    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types

    NSString *appScheme = @"alisdkdemo";

    //将商品信息拼接成字符串

    NSString *orderSpec = [order description];

    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode

    id<DataSigner> signer = CreateRSADataSigner(privateKey);

    NSString *signedString = [signer signString:orderSpec];

    //将签名成功字符串格式化为订单字符串,请严格按照该格式

    NSString *orderString = nil;

    if (signedString != nil) {

        orderString = [NSString stringWithFormat:@"%@&sign="%@"&sign_type="%@"",

                       orderSpec, signedString, @"RSA"];

        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {

            NSString * resultStatus = [resultDic objectForKey:@"resultStatus"];

            if ([resultStatus intValue] == 9000) {//表示成功

                [KAlertView showType:KAlertTypeCheck text:@"打赏成功" for:2.0 animated:YES];

            }

            else if ([resultStatus intValue] == 4000) {//失败

                [KAlertView showType:KAlertTypeError text:@"打赏失败" for:2.0 animated:YES];

            }

            else if ([resultStatus intValue] == 6002) {//网络出错

                [KAlertView showType:KAlertTypeError text:@"网络连接出错" for:2.0 animated:YES];

                

            } else if ([resultStatus intValue] == 6001) {//取消

                [KAlertView showType:KAlertTypeNone text:@"打赏已取消!" for:1.0 animated:YES];

            }

        }];

    }

 这是发起的时候的返回状态   当我们进入支付宝 操作付款或者 点击取消付款 我们需要在本app里获取到这个状态   我们需要在appdelegate里面 调用方法    发送一个通知

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary *)options {

  // 微信支付处理

    if ([url.description rangeOfString:WEIXIN_APP_ID].length > 0) {

        return [WXApi handleOpenURL:url delegate:self];

    }

    //跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给SDK

    if ([url.host isEqualToString:@"safepay"]) {

        [[AlipaySDK defaultService]

         processOrderWithPaymentResult:url

         standbyCallback:^(NSDictionary *resultDic) {

             

             //发送消息通知

             [[NSNotificationCenter defaultCenter] postNotificationName:@"AlipayResultNotification" object:resultDic];

             

         }];

    }

     return YES;

}

 // 注册支付宝支付结果通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedAlipayNotify:) name:@"AlipayResultNotification" object:nil];

/ 支付宝通过客户端支付回调的通知

-(void)receivedAlipayNotify:(NSNotification*)sender {

    

    NSDictionary *resultDic = sender.object;

    NSString* resultStatus = resultDic[@"resultStatus"];

    if ([resultStatus intValue] == 9000) {

        [KAlertView showType:KAlertTypeCheck text:@"打赏成功" for:2.0 animated:YES];

    }

    else if ([resultStatus intValue] == 4000) {

        [KAlertView showType:KAlertTypeError text:@"打赏失败" for:2.0 animated:YES];

    }

    else if ([resultStatus intValue] == 6002) {

        [KAlertView showType:KAlertTypeCheck text:@"网络连接出错" for:2.0 animated:YES];

        

    } else if ([resultStatus intValue] == 6001) {

        [KAlertView showType:KAlertTypeNone text:@"打赏已取消!" for:1.0 animated:YES];

    }

}

这就是支付宝的完整回调过程   只要配好sdk  支付就完成百分之70  

原文地址:https://www.cnblogs.com/lxgblog/p/6208944.html