KWMPayUtil.m 5.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
//
//  KWMPayUtil.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/6/15.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMPayUtil.h"
#import "KWMStringUtil.h"

@implementation KWMPayUtil

+(NSString *)payTypeString:(KWMPayType)payType{
    NSString *payTypeString = @"";
    switch (payType) {
        case TypeNone:
            payTypeString = @"请选择一种付款方式";
            break;
        case TypeCredit:
            payTypeString = @"信用卡支付";
            break;
        case TypeWechatPay:
            payTypeString = @"微信支付";
            break;
        case TypeAlipay:
            payTypeString = @"支付宝支付";
            break;
houweibin committed
29 30
        case TypeWebpay:
            payTypeString = @"网页支付";
31 32 33 34 35 36
    }
    return payTypeString;
}

+(NSDictionary *)wechatPayParameters:(BUYCheckout *)checkout{
    //这是测试商店的单,先用来测试
u  
lee committed
37
#if DEBUG
houweibin committed
38 39
    NSString *checkout_token = checkout.token;
    NSString *total_price = [NSString stringWithFormat:@"%.2f",checkout.totalPrice.floatValue];
40 41
    //先写test,后面改成正式的订单描述
    NSString *subject = @"test";
u  
lee committed
42 43
#else
    NSString *checkout_token = checkout.token;
houweibin committed
44
    //需要保留小数点后两位(包括小数点后的.00),以保持与后台一致,从而验证通过
houweibin committed
45
    NSString *total_price = [NSString stringWithFormat:@"%.2f",checkout.totalPrice.floatValue];
houweibin committed
46
    NSString *subject = [NSString stringWithFormat:@"Cemarose订单-%@",checkout.token];
u  
lee committed
47 48
#endif
    NSString *ip_address = @"127.0.0.1";
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    NSString *before_sign = [NSString stringWithFormat:@"checkout_token=%@&ip_address=%@&subject=%@&total_price=%@&key=%@",
                             checkout_token,ip_address,subject,total_price,User_Pay_KEY];
    NSString *base64String = [KWMStringUtil BASE64:before_sign];
    NSString *sign = [KWMStringUtil MD5:base64String];
    
    NSDictionary *parameters = @{
                                 @"ip_address":ip_address,
                                 @"checkout_token":checkout_token,
                                 @"subject":subject,
                                 @"total_price":total_price,
                                 @"sign":sign
                                 };
    return parameters;
}

+(NSDictionary *)aliPayParameters:(BUYCheckout *)checkout{
    //这是测试商店的单,先用来测试
u  
lee committed
66
#if DEBUG
houweibin committed
67 68
    NSString *checkout_token = checkout.token;
    NSString *total_price = [NSString stringWithFormat:@"%.2f",checkout.totalPrice.floatValue];
69
    NSString *subject = @"test";
u  
lee committed
70 71
#else
    NSString *checkout_token = checkout.token;
houweibin committed
72
    //需要保留小数点后两位(包括小数点后的.00),以保持与后台一致,从而验证通过
houweibin committed
73
    NSString *total_price = [NSString stringWithFormat:@"%.2f",checkout.totalPrice.floatValue];
houweibin committed
74
    NSString *subject = [NSString stringWithFormat:@"Cemarose订单-%@",checkout.token];
u  
lee committed
75
#endif
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    NSString *before_sign = [NSString stringWithFormat:@"checkout_token=%@&subject=%@&total_price=%@&key=%@",
                             checkout_token,subject,total_price,User_Pay_KEY];
    NSString *base64String = [KWMStringUtil BASE64:before_sign];
    NSString *sign = [KWMStringUtil MD5:base64String];
    
    NSDictionary *parameters = @{
                                 @"checkout_token":checkout_token,
                                 @"subject":subject,
                                 @"total_price":total_price,
                                 @"sign":sign
                                 };
    return parameters;
}

houweibin committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
//现在在两个地方会用到这条api
//1,支付后切换回到APP
//2,beforepay创建/复用旧checkout时,判断该checkout是否被支付了,但是之前没清空。
+(NSDictionary *)payResultParameters:(BUYCheckout *)checkout{
#if DEBUG
    NSString *checkout_token = checkout.token;
#else
    NSString *checkout_token = checkout.token;
#endif
    NSString *before_sign = [NSString stringWithFormat:@"checkout_token=%@&key=%@",
                             checkout_token,User_Pay_KEY];
    NSString *base64String = [KWMStringUtil BASE64:before_sign];
    NSString *sign = [KWMStringUtil MD5:base64String];
    
    NSDictionary *parameters = @{
                                 @"checkout_token":checkout_token,
                                 @"sign":sign
                                 };
    return parameters;
}

111 112 113 114 115 116 117 118
+(void)weChatPay:(KWMWechatPayData *)wechatData{
    if(wechatData){
        PayReq *request = [[PayReq alloc] init];
        request.openID = wechatData.appid;
        request.partnerId = wechatData.partnerid;
        request.prepayId= wechatData.prepayid;
        request.package = wechatData.package;
        request.nonceStr= wechatData.noncestr;
u  
lee committed
119
        request.timeStamp= (UInt32)wechatData.timestamp;
120 121 122 123 124 125 126 127 128 129 130 131 132
        request.sign= wechatData.sign;
        [WXApi sendReq:request];
    }
}

+(void)aliPay:(NSString *)alipayData callback:(CompletionBlock)callback{
    if(![KWMStringUtil isEmpty:alipayData]){
        NSString *appScheme = @"iCemarose";
        [[AlipaySDK defaultService] payOrder:alipayData fromScheme:appScheme callback:callback];
    }
}

@end