码迷,mamicode.com
首页 > 其他好文 > 详细

支付宝支付流程

时间:2016-06-23 15:55:38      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 

.code
{
    background: #fbedbb;
    border: #c0c0c0 1px solid;
    padding: 5px;
    margin: 0 40px 0 20px;
    font-family: Verdana,Helvetica, "微软雅黑" , Arial, "宋体" , sans-serif;
}

 

 

1.拼单

AlixPayOrder *order = [[AlixPayOrder alloc]init];

        order.partner = [params objectAtIndex:1];

        order.seller = [params objectAtIndex:2];

        order.tradeNO = resultDict[@"ordersn"];

        order.productName = [params objectAtIndex:6];

        order.productDescription = [params objectAtIndex:8];

        order.amount = [NSString stringWithFormat:@"%.2f", [(NSString *)((NSDictionary *)resultDict[@"paychannel"])[@"amount"]floatValue] / 100];

        order.notifyURL = [(NSString *)[params objectAtIndex:3] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        order.paymentType = [params objectAtIndex:7];

        order.itBPay = [NSString stringWithFormat:@"%@m", [params objectAtIndex:9]];

        NSString *orderInfo = [order description];

        id<DataSigner> signer;

        signer = CreateRSADataSigner([params objectAtIndex:0]);

        NSString *signedStr = [signer signString:orderInfo];//gw 订单信息用私钥签名

        //        NSNewcoLog(@"%@", signedStr);

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

                                 orderInfo, signedStr, [params objectAtIndex:5]];

        NSString *appScheme = @"Lottery-Trade-3-3";

        ApplicationDelegate.key = [(NSArray *)((NSDictionary *)resultDict[@"paychannel"])[@"payparams"]objectAtIndex:4];//签约帐户后获取到的支付宝公钥

2.调用支付宝反馈

 

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

 

            NSLog(@"支付宝返回:%@",resultDic);

 

            if (resultDic) {

 

                //结果处理

 

                NsZhifubaoModel *result = [NsZhifubaoModel zhifuWithDictionary:resultDic];

 

                if ([result.resultStatus integerValue] == 9000) {

 

                    if ([result.success isEqualToString:@"true"]) {

 

                        NsAccountRecordController *accountCtrl = [[NsAccountRecordController alloc]initWithTitle:@"账户明细"  type:AccountRecord_Normal];

 

                        accountCtrl.isWarn = result.out_trade_no;

 

                        UINavigationController *nav = [ApplicationDelegate navigationController];

 

                        [nav pushViewController:accountCtrl animated:YES];

 

                        [accountCtrl release];

 

                    }

 

                    /*

 

                    //签名

 

                    NSString* key = [(NSArray *)((NSDictionary *)resultDict[@"paychannel"])[@"payparams"]objectAtIndex:4];//签约帐户后获取到的支付宝公钥

 

                    id<DataVerifier> verifier;

 

                    verifier = CreateRSADataVerifier(key);

 

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

 

                    {

 

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

 

                        NsAccountRecordController *accountCtrl = [[NsAccountRecordController alloc]initWithTitle:@"账户明细"  type:AccountRecord_Normal];

 

                        accountCtrl.isWarn = result.out_trade_no;

 

                        UINavigationController *nav = [ApplicationDelegate navigationController];

 

                        [nav pushViewController:accountCtrl animated:YES];

 

                        [accountCtrl release];

 

                    }

 

                    */

 

                }

 

                else {

 

                    NSLog(@"交易失败");

 

                }

 

            } else {

 

                NSLog(@"交易失败");

 

            }

 

        }];

 

 3.调用支付宝钱包

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

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

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

        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

            NSLog(@"支付宝返回:%@",resultDic);

            if (resultDic) {

                //结果处理

                NsZhifubaoModel *result = [NsZhifubaoModel zhifuWithDictionary:resultDic];

                if ([result.resultStatus integerValue] == 9000) {

                    if ([result.success isEqualToString:@"true"]) {

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

                        id<DataVerifier> verifier;

                        verifier = CreateRSADataVerifier(key);

                        // 验证的步骤:首先把订单信息和签名值分别提取出来

                        // 订单信息就是sign_type的连字符&之前的所有字符串result.resultString

                        // 签名值是sign后面双引号内的内容,注意签名的结尾也是=  result.sign

                        if ([verifier verifyString:result.resultString withSign:result.sign]) {//gw 在处理返回的支付结果时,需要使用公钥验证返回结果是否被篡改了

                            NsAccountRecordController *accountCtrl = [[NsAccountRecordController alloc]initWithTitle:@"账户明细"  type:AccountRecord_Normal];

                            accountCtrl.isWarn = result.out_trade_no;

                            UINavigationController *nav = [ApplicationDelegate navigationController];

                            [nav pushViewController:accountCtrl animated:YES];

                            [accountCtrl release];

                        }

                    }

            }

                else {

                    NSLog(@"交易失败");

                }

            } else {

                NSLog(@"交易失败");

            }

 

        }];

    }

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

        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {

            NSLog(@"支付宝返回:%@",resultDic);

        }];

    }

    return YES;

}

 附加:支付宝反回字符串的解析(将字符串分割成子串,并且以字典的形式存储)

//

//  NsZhifubaoModel.h

//  Lottery-Trade-3.3

//

//  Created by 袁冬冬 on 16/5/17.

//

//

 

#import <Foundation/Foundation.h>

 

@interface NsZhifubaoModel : NSObject

 

@property (nonatomic, copy) NSString *memo;

 

@property (nonatomic, copy) NSString *resultStatus; //状态码

 

@property (nonatomic, copy) NSString *resultString; //订单信息

 

@property (nonatomic, copy) NSString *partner; //

 

@property (nonatomic, copy) NSString *seller_id;

 

@property (nonatomic, copy) NSString *out_trade_no; //支付宝订单号

 

@property (nonatomic, copy) NSString *subject;

 

@property (nonatomic, copy) NSString *body;

 

@property (nonatomic, copy) NSString *total_fee;

 

@property (nonatomic, copy) NSString *notify_url;

 

@property (nonatomic, copy) NSString *show_url;

 

@property (nonatomic, copy) NSString *service;

 

@property (nonatomic, copy) NSString *_input_charset;

 

@property (nonatomic, copy) NSString *payment_type;

 

@property (nonatomic, copy) NSString *return_url;

 

@property (nonatomic, copy) NSString *it_b_pay;

 

@property (nonatomic, copy) NSString *success;

 

@property (nonatomic, copy) NSString *sign_type;

 

@property (nonatomic, copy) NSString *sign;

 

- (instancetype)initWithDictionary:(NSDictionary *)dict;

 

+ (instancetype)zhifuWithDictionary:(NSDictionary *)dict;

 

@end

 

//

//  NsZhifubaoModel.m

//  Lottery-Trade-3.3

//

//  Created by 袁冬冬 on 16/5/17.

//

//

 

#import "NsZhifubaoModel.h"

 

@implementation NsZhifubaoModel

 

- (instancetype)initWithDictionary:(NSDictionary *)dict {

    self = [super init];

    if (self) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

 

+ (instancetype)zhifuWithDictionary:(NSDictionary *)dict {

    return [[NsZhifubaoModel alloc] initWithDictionary:dict];

}

 

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

    if ([key isEqualToString:@"result"]) {

        if ([value rangeOfString:@"&sign_type="].location != NSNotFound) {

            NSArray *tempArray = [value componentsSeparatedByString:@"&sign_type="];

            self.resultString = tempArray[0];

        }

        [self parseStringToDic:value];

    }

}

 

//将支付宝返回的result字符串转换成字典

- (NSDictionary *)parseStringToDic:(NSString *)str {

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    //将字符串分割成数组,数组中存储键值对

    NSArray *key_values = [str componentsSeparatedByString:@"&"];

    for (int i = 0; i < key_values.count; i++) {

        NSString *tempStr = key_values[i];

        if ([tempStr rangeOfString:@"partner="].location != NSNotFound) {

            self.partner = [[tempStr substringFromIndex:[@"partner" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"seller_id="].location != NSNotFound) {

            self.seller_id = [[tempStr substringFromIndex:[@"seller_id" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"out_trade_no="].location != NSNotFound) {

            self.out_trade_no = [[tempStr substringFromIndex:[@"out_trade_no" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"subject="].location != NSNotFound) {

            self.subject = [[tempStr substringFromIndex:[@"subject" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }if ([tempStr rangeOfString:@"body="].location != NSNotFound) {

            self.body = [[tempStr substringFromIndex:[@"body" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }if ([tempStr rangeOfString:@"total_fee="].location != NSNotFound) {

            self.total_fee = [[tempStr substringFromIndex:[@"total_fee" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"notify_url="].location != NSNotFound) {

            self.notify_url = [[tempStr substringFromIndex:[@"notify_url" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"show_url="].location != NSNotFound) {

            self.show_url = [[tempStr substringFromIndex:[@"show_url" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"service="].location != NSNotFound) {

            self.service = [[tempStr substringFromIndex:[@"service" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"_input_charset="].location != NSNotFound) {

            self._input_charset = [[tempStr substringFromIndex:[@"_input_charset" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"payment_type="].location != NSNotFound) {

            self.payment_type = [[tempStr substringFromIndex:[@"payment_type" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"return_url="].location != NSNotFound) {

            self.return_url = [[tempStr substringFromIndex:[@"return_url" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"it_b_pay="].location != NSNotFound) {

            self.it_b_pay = [[tempStr substringFromIndex:[@"it_b_pay" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"success="].location != NSNotFound) {

            self.success = [[tempStr substringFromIndex:[@"success" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"sign_type="].location != NSNotFound) {

            self.sign_type = [[tempStr substringFromIndex:[@"sign_type" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

        if ([tempStr rangeOfString:@"sign="].location != NSNotFound) {

            self.sign = [[tempStr substringFromIndex:[@"sign" length]+1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        }

//        //将键值对字符串分割

//        NSArray *temArray = [tempStr componentsSeparatedByString:@"="];

//        //将键值对存到字典里

//        NSString *secondStr = [temArray[1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

//        NSLog(@"这里:%@",secondStr);

//        [dict setObject:secondStr forKey:temArray[0]];

    }

    return dict;

}

@end

 

 

 

 

支付宝支付流程

标签:

原文地址:http://www.cnblogs.com/dongdongyuan/p/5611062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!