第三方支付

一.微信支付流程
1.导入SDK


第三方支付_第1张图片
44.png

2.导入相关的系统库及文件。不导入会报错。
SystemConfiguration.framework
libz.tbd
libsqlite3.0.tbd
CoreTelephony.framework
libc++.tbd
3.配置info.plist
a.schemes ,注意,item0 这里要修改成商户自己的APPID


45.png

b.白名单
46.png

4.修改bitcode
第三方支付_第2张图片
47.jpeg

5.在AppDelegate 注册微信
导入#import "WXApiManager.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //向微信注册wxd930ea5d5a258f4f
    [WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"demo 2.0"];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

6.接下来,就是发起请求支付了,实现上核心代码只有几行

                 //调起微信支付
                    PayReq* req             = [[[PayReq alloc] init]autorelease];
                    req.partnerId           = [dict objectForKey:@"partnerid"];
                    req.prepayId            = [dict objectForKey:@"prepayid"];
                    req.nonceStr            = [dict objectForKey:@"noncestr"];
                    req.timeStamp           = stamp.intValue;
                    req.package             = [dict objectForKey:@"package"];
                    req.sign                = [dict objectForKey:@"sign"];
                    [WXApi sendReq:req];

7.不管支付成功还是失败,我们直接处理回调结果即可

 switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付结果:成功!";
                NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                break;
                
            default:
                strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }

客户端进行签名

1.导入文件。


第三方支付_第3张图片
49.png

![Uploading 48_198784.png . . .]
2.里面有两个文件是非arc的,我们需要设置一下 -fno-objc-arc


第三方支付_第4张图片
48.png

3.导入系统库及info.list配置,请参数上面服务端签名。

4.AppDelegate配置
1)导入头文件

#import "WXApi.h"
#import "payRequsestHandler.h"

2)实现微信代理

@interface AppDelegate ()
@end

3)注册微信,及微信支付回调
复制代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //向微信注册
    [WXApi registerApp:APP_ID withDescription:@"demo 2.0"];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return  [WXApi handleOpenURL:url delegate:self];
}

// 微信支付成功或者失败回调
-(void) onResp:(BaseResp*)resp
{
    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    NSString *strTitle;
    
    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
    }
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果,实际支付结果需要去微信服务器端查询
        strTitle = [NSString stringWithFormat:@"支付结果"];
        
        switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付结果:成功!";
                NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                break;
                
            default:
                strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

5.写方法直接在支付时调用
复制代码

- (void)payForWechat
{
    //创建支付签名对象
    payRequsestHandler *req = [[payRequsestHandler alloc] init];
    //初始化支付签名对象
    [req init:APP_ID mch_id:MCH_ID];
    //设置密钥
    [req setKey:PARTNER_ID];
    
    NSMutableDictionary *dict = [req sendPay_demo];
    
    if(dict != nil){
        NSMutableString *retcode = [dict objectForKey:@"retcode"];
        if (retcode.intValue == 0){
            NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
            
            //调起微信支付
            PayReq* req             = [[PayReq alloc] init];
            req.openID              = [dict objectForKey:@"appid"];
            req.partnerId           = [dict objectForKey:@"partnerid"];
            req.prepayId            = [dict objectForKey:@"prepayid"];
            req.nonceStr            = [dict objectForKey:@"noncestr"];
            req.timeStamp           = stamp.intValue;
            req.package             = [dict objectForKey:@"package"];
            req.sign                = [dict objectForKey:@"sign"];
            [WXApi sendReq:req];
            //日志输出
            NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
        }else{
            [self alert:@"提示信息" msg:[dict objectForKey:@"retmsg"]];
        }
    }else{
        [self alert:@"提示信息" msg:@"服务器返回错误,未获取到json对象"];
    }
}

//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
    [alter show];
}

二.支付宝支付流程
步骤1: 启动IDE(如Xcode),把iOS包中的压缩文件中以下文件拷贝到项目文件夹下,并导入到项目工程中。

AlipaySDK.bundle                    
AlipaySDK.framework

步骤2: 在需要调用AlipaySDK的文件中,增加头文件引用。

#import 

步骤3: 配置请求信息。

  Order *order = [[Order alloc] init];
   order.partner = partner;//合作身份者ID,以 2088 开头由 16 位纯数字组成的字符串。请参考“7.1 如何获得PID与 密钥”。
           order.seller = seller;//支付宝收款账号,手机号码或邮箱格式。
private_key//商户方的私钥,pkcs8 格式
      order.tradeNO = [self generateTradeNO]; //订单ID(由商家□自□行制定)
order.productName = product.subject; //商品标题           order.productDescription = product.body; //商品描述

order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商 品价格

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);

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

步骤4: 配置支付宝客户端返回url处理方法.
如示例 AliSDKDemo\APAppDelegate.m 文件中,@implementation AppDelegate 中增加如下代码:

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

//如果极简 SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给 SDK
 if ([url.host isEqualToString:@"safepay"]) {

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

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

if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode

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

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

return YES; }

三.md5
1、 导入头文件:

#import  

2、加密的方法:

- (NSString *) md5:(NSString *) input {  
    const charchar *cStr = [input UTF8String];  
    unsigned char digest[CC_MD5_DIGEST_LENGTH];  
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call  
      
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];  
      
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)  
    [output appendFormat:@"%02x", digest[i]];  
      
    return  output;  
}  

3、调用加密方法:

NSString *str = @"123456";  
  
NSString *result = [self md5:str];  
  
NSLog(@"%@",result);   

四.sha1

//sha1加密方式
- (NSString *)getSha1String:(NSString *)srcString{
constchar *cstr = [srcString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSDatadataWithBytes:cstr length:srcString.length];

uint8_t digest[CC_SHA1_DIGEST_LENGTH];

CC_SHA1(data.bytes, data.length, digest);

NSMutableString* result = [NSMutableStringstringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
       [result appendFormat:@"%02x", digest[i]];
   }

return result;
}
[resultLabel1setText:[selfgetSha1String:srcStringTextField.text]];

你可能感兴趣的:(第三方支付)