美文网首页
秋秋的支付宝

秋秋的支付宝

作者: 淡若烟 | 来源:发表于2016-07-30 10:49 被阅读63次

    不能对不起我的几个粉丝~,我决定更新一篇文章啦~

    点击支付宝 进入下载demo及创建应用的的平台啦~当然还有其他完全写好了的,这就给你们点我--demo

    创建应用,挨个填写信息即可,这个让老板自己创去吧,创好了找他要账号

    创建应用.png

    当然,这些东西看起来太复杂了,我得给你一个实际的项目,跟 后台做过交互的,成功调起了,不然那些参数传过来传过去的,看着太过繁琐,还不如实际一些,程序猿,不就喜欢最直接的么,哪来那些弯弯扭扭~~

    这几个东西,你懂的,先放进去,那个Alipay文件夹,demo里面都有,先下载了,拖进去

    必要的库.png

    在appledegate 导入#import "AlipaySDK/AlipaySDK.h"
    并导入下面的代码
    <pre><code>

    -(BOOL)application:(UIApplication *)app openURL:(NSURL )url options:(NSDictionary)options<code>
    {
    if([url.host isEqualToString:@"safepay"])
    {
    //跳转支付宝钱包进行支付,处理支付结果
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
    }];
    }
    return YES;
    }

    </code></pre>

    现在就去viewcontroller 调用
    先写一个方法,然后在点击方法内调用传入价格商品名即可,这个时候跟后台之前商品数据进行对接即可。

    <pre><code>
    //支付宝付款
    -(void)userpayWithMoney:(NSString *)money Productname:(NSString *)productname
    {
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * resultStr = [user objectForKey:@"ordernum"];
    Order *order = [[Order alloc] init];
    order.partner = alipartner;//支付宝PID例@"2066511942711331"这里进行了宏定义
    order.seller = aliseller;//支付宝商家账户这里进行了宏定义就是收钱那个账号
    order.tradeNO = resultStr; //订单ID也就是订单号,可以自己随机生成传给后台,也可以后台生成传给展示数据,我这里是自己生成的,下面可以给你生成的代码(由商家自行制定)
    //必须要商品标题
    order.productName = productname; //商品标题
    NSString * pricestr = [money stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"¥"]];
    float p = [pricestr floatValue];
    order.amount = [NSString stringWithFormat:@"%.2f",p]; //商品价格
    order.notifyURL = payURL; //回调URL这里进行了宏定义,这个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
    NSString *appScheme = @"lalalla";//这个就自己写上你自己的了
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(aliprivateKey);//支付宝私钥 怎么获取,这里有详解[私钥获取方法](http://blog.it985.com/12276.html)
    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);
            dispatch_async(dispatch_get_main_queue(),^{
                int success = [[resultDic objectForKey:@"resultStatus"] intValue] ;
                if (success==9000)
                {
                    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付成功" preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction * act = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action)
                    {
                        [self.navigationController pushViewController:[[OrderController alloc]init] animated:YES];
                    }];
                    [alertView addAction:act];
                    
                    [self presentViewController:alertView animated:YES completion:nil];
                }
                else if (success==6001)
                {
                    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付取消" preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction * act = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                    [alertView addAction:act];
                    
                    [self presentViewController:alertView animated:YES completion:nil];
                }
                else
                {
                    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"支付失败" preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction * act = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                    [alertView addAction:act];
                    
                    [self presentViewController:alertView animated:YES completion:nil];
                }
            });
            
        }];
    }
    

    }

    </code></pre>

    相关文章

      网友评论

          本文标题:秋秋的支付宝

          本文链接:https://www.haomeiwen.com/subject/pextsttx.html