iOS 支付宝支付的使用

有篇文章写了如果集成支付宝的SDK,以及在集成SDK中我们遇到的错误和解决方法,这次就总结下支付宝如何使用;
在支付宝开放平台中,我们可以下载相应的SDK,并且里面也有demo,可以参照里面的方法来实现我们的需求,,我们这里接入了支付宝的支付和微信支付,随后会在写关于微信支付的文章;

在这里就做一下简单的使用:
在.m文件中创建一个button,点击button触发支付宝的支付;

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =CGRectMake(100, 100, 100, 50);
    [btn setTitle:@"点击支付" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor magentaColor];
    [btn addTarget:self action:@selector(doClickAlipay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

//该方法为支付宝中demol的一个方法(这里你们可以从后台请求获取到订单号,是自行制定的)
#pragma mark   ==============产生随机订单号==============
- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

//最新的支付宝SDK,似乎不用判断用户是否安装有支付宝。本人测试过,如果没有安装会直接跳转网页支付,如果安装了则会直接调转到支付宝客户端进行支付。(如果害怕有隐患的话,就自己做下处理,判断是否安装了支付宝)
//点击button,进行支付
- (void)doClickAlipay{

   //这三个值是商户在支付宝平台注册申请签约之后获得到的值,直接填入就行。
    NSString *partner = @"";
    NSString *seller = @"";
    NSString *privateKey = @"";

    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }

    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
    order.productName = @"陌野网络科技"; //商品标题
    order.productDescription = @"充值"; //商品描述
    order.amount = @"0.01"; //商品价格,在这里为了测试所有价格充值是0.01,这是最小的单位,如果写0.001在请求支付的时候会有错误提示
    order.notifyURL =  @"http://www.baidu.com"; //回调URL,可以自己随便写个测试下,看下结果是什么

   //这些都不用改
    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。就是在支付完成后回到我们的原有app中,如果不设置,用的是demol中的则支付后会跳转到demol中
    NSString *appScheme = @"ZhiPayDemol22";

这里写图片描述

 //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);

    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循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) {
            NSLog(@"reslut = %@",resultDic);
            //返回结果需要通过resultStatus以及result字段的值来综合判断并确定支付结果。在resultStatus=9000,并且success=“true”以及sign=“xxx”校验通过的情况下,证明支付成功。其它情况归为失败。较低安全级别的场合,也可以只通过检查resultStatus以及success=“true”来判定支付结果。以下为订单支付成功的完成信息示例:
            ResultStatus={9000};memo={};result={partner="2088101568358171"&seller_id="xxx@alipay.com"&out_trade_no="0819145412-6177"&subject="测试"&body="测试测试"&total_fee="0.01"&notify_url="http://notify.msp.hk/notify.htm"&service="mobile.securitypay.pay"&payment_type="1"&_input_charset="utf-8"&it_b_pay="30m"&success="true"&sign_type="RSA"&sign="hkFZr+zE9499nuqDNLZEF7W75RFFPsly876QuRSeN8WMaUgcdR00IKy5ZyBJ4eldhoJ/2zghqrD4E2G2mNjs3aE+HCLiBXrPDNdLKCZgSOIqmv46TfPTEqopYfhs+o5fZzXxt34fwdrzN4mX6S13cr3UwmEV4L3Ffir/02RBVtU="}
        }];

    }

}

在AppDelegate的.m文件中引入头文件:

#import <AliPaySDK/AlipaySDK.h>

//支付宝支付结果的回调,在这里你可以根据支付成功的结果进行相关的操作,比如要跳转到指定页面,在这里就可以写个通知进行跳转;

备注:重要更新,如果你发现你的回调方法没有走,可能是以下原因。在IOS9.0以后以下两个方法被废弃了,所以如果你是Xcode7.2的话,可能会出现不能进入微信的onResp回调方法,原因是下边两个方法没有被调用,所以这里更新一下,改用另外一个方法(并不建议删除这两个方法,新方法是9.0以后的方法,可能系统低版本的用户不支持。我在这里使用的是Xcode 7.0)

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

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

//改用方法为- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options;
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

            NSLog(@" == %@",url.host);
         //在这里根据url判断是支付宝的支付还是其他的支付结果
        if ([url.host isEqualToString:@"safepay"]) {

//跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            if ([resultDic[@"ResultStatus"] isEqualToString:@"9000"]) {
                //注册通知,支付宝支付成功后,跳转到订单成功页面,在PayOrderVC里面接收通知
                NSNotification * alipayNotification = [NSNotification notificationWithName:@"AlipayNotification" object:nil];
                [[NSNotificationCenter defaultCenter] postNotification:alipayNotification];
            }
        }];
        }
    return YES;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值