您的当前位置:首页正文

秋秋的支付宝

来源:花图问答

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

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

创建应用.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 调用
先写一个方法,然后在点击方法内调用传入价格商品名即可,这个时候跟后台之前商品数据进行对接即可。

//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme = @"lalalla";//这个就自己写上你自己的了

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

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(aliprivateKey);//支付宝私钥 
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>