您的位置:首页 > 移动开发 > IOS开发

iOS之支付宝集成(二)

2016-06-03 22:50 405 查看
参考:http://www.jianshu.com/p/f81578954974
http://www.jianshu.com/p/fe56e122663e/
===================

#import "ViewController.h"

#import <AlipaySDK/AlipaySDK.h>

#import "Order.h"

#import "DataSigner.h"

#import "DataVerifier.h"

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

     [superviewDidLoad];

    //1.申请账户ID商户ID生成加密的私钥

   
//2.创建订单信息 (用订单模型 阿里提供不要修改)

   
//3.生成订单字符串 (调用 description方法)把订单的所有属性联合起来生成字符串

    //4.加密订单信息

    //5.开始支付

    

    

    

   //1.生成订单字符串

    Order *order = [[Orderalloc]init];

    order.partner =
@"";

    order.seller =
@"";

    order.tradeNO =@"201608341056";//订单ID(由商家?自?行制定)唯一

    order.productName =
@"娃娃";//商品标题

    order.productDescription =@"简介";//商品描述

    //order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格保留2位小数 
1.8

    

    order.notifyURL =@"http://www.xxx.com";//回调URL应用间跳转

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

    order.paymentType =
@"1";

    order.inputCharset =
@"utf-8";

    order.itBPay =
@"30m"; //订单的有效果

    

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

    NSString *appScheme =
@"alisdkdemo";

    

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

    NSString *orderSpec = [order
description];

    NSLog(@"orderSpec = %@",orderSpec);

    //2.生成加密签名信息

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

    id<DataSigner> signer =CreateRSADataSigner(@"11111");//通过私钥生成签名对象

    NSString *signedString = [signer
signString:orderSpec];//生成签名信息

    //3.利用订单信息、签名信息、签名类型生成一个订单字符串
   //将签名成功字符串格式化为订单字符串,请严格按照该格式

    NSString *orderString =
nil;

    if (signedString !=
nil) {

        //orderString 
参数 传递给支付宝

        orderString = [NSStringstringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",

                       orderSpec, signedString, @"RSA"];

        //4.打开客户端,进行支付(商品名称,商品价格,商户信息)

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

            //【callback处理支付结果】

            NSLog(@"reslut = %@",resultDic);

#warning 支付事件完成

           //在这里验签

        }];

    

    }

}

==============
签名校验

@implementation AppDelegate

/**

 *  当从其他应用跳转到当前应用时,就会调用这个方法

 */

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

{

//

支付结果url,传入后由SDK解析,统一在上面的pay方法的callback中回调

    AlixPayResult * result =
nil;

    if (url != nil && [[url host]compare:@"safepay"] ==0)
{

        NSString * query = [[url query]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

         result = [[AlixPayResult
alloc] initWithString:query];//解析

    }

    

    if (result.statusCode ==9000) {

        /*

         *用支付宝的公钥验证签名严格验证请使用result.resultString与result.signString验签

         */

        id<DataVerifier> verifier =CreateRSADataVerifier(AlipayPubKey);//支付宝的公钥

        //验证签名----用支付宝的公钥验证支付宝用私钥传过来的信息

        if ([verifier
verifyString:result.resultString
withSign:result.signString]) {

            //验证签名成功,交易结果无篡改

            //交易成功

            

        } else { //失败

            

        }

    } else {

        // 失败

        

    }

    return 
YES;

}

================

/在程序被关了之后返回了支付结果会打开当前应用

//当应用被别人用协议头的方式打开时候就会调用

- (BOOL)application:(UIApplication *)application

            openURL:(NSURL *)url

  sourceApplication:(NSString *)sourceApplication

         annotation:(id)annotation {

    

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

       
//跳转支付宝钱包进行支付,处理支付结果

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

            NSLog(@"result = %@",resultDic);

            //判断字典的编码  9000表示支付成功!!!

        //在这里面验签

        }];

    }

    return
YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: