-(NSString*)GetString:(NSDictionary*)anyobjiect_dict{
// 创建一个局部数组,防止OrderInfoAllKeysArray 为空
NSArray * AllKeys =@[@"busi_partner",@"dt_order",@"info_order",
@"money_order",@"name_goods",@"no_order",
@"notify_url",@"oid_partner",@"risk_item",@"sign_type",
@"valid_order"];
if (self.OrderInfoAllKeysArray !=nil) {
// 如果OrderInfoAllKeysArray 存在将 Allkeys 替代
AllKeys = self.OrderInfoAllKeysArray;
}
if (ErrorMessageDictInfo) {
[ErrorMessageDictInfosetObject:@"OrderInfoAllKeysArray没有传入"forKey:@"AllKeys"];
}
// 将一个不可变的数组,可变化。进行数组对象字母排序
NSMutableArray * LetterKeysArray = [NSMutableArrayarrayWithArray:AllKeys];
[LetterKeysArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
// 声明一个可变的字符串,将字典转化为字符串
NSMutableString * PiecedMutableString = [[NSMutableStringalloc]initWithCapacity:0];
for (NSString * tempstrin LetterKeysArray) {
if ([anyobjiect_dict[tempstr] length] != 0) {
[PiecedMutableString appendFormat:@"&%@=%@",tempstr,anyobjiect_dict[tempstr]];
}
}
// 进行可变的字符串进行处理
if (PiecedMutableString.length !=0) {
// 删除字符串的第一个字符:@“&”
[PiecedMutableString deleteCharactersInRange:NSMakeRange(0,1)];
}else{
if(ErrorMessageDictInfo){
[ErrorMessageDictInfosetObject:@"65行可变字符串不存在"forKey:@"PiecedMutableString"];
}
}
if ([anyobjiect_dict[@"sign_type"]isEqualToString:@"MD5"]) {
// 拼接上签名的Key-Value
[PiecedMutableString appendFormat:@"&key=%@",self.SingKey];
}
// 声明要输出的字符串对象
NSString * Return_SignString = nil;
if([anyobjiect_dict[@"sign_type"]isEqualToString:@"MD5"]){
Return_SignString = [ApplePayAlgorithm GetMd5String:PiecedMutableString];
}else{
// 哈希加密
Return_SignString = [ApplePayAlgorithm signHmacString:PiecedMutableString withKey:self.SingKey];
}
#ifdef kLLPayUtilNeedRSASign
id<LLPDataSigner> signer = LLPCreateRSADataSigner(self.signKey);
signString = [signer signString:paramString];
#endif
// 输出加密后的字符串
NSLog(@"签名字符串:%@",Return_SignString);
return Return_SignString;
}
+(NSString*)GetMd5String:(NSString*)anyobject_string{
// 进行编码转换成字符
const char *original_str = [anyobject_stringUTF8String];
unsigned char result[32];
CC_MD5(original_str, (CC_LONG)strlen(original_str), result);//调用md5
NSMutableString * MD5_String = [NSMutableStringstring];
for (int i =0; i < 16; i++){
[ MD5_String appendFormat:@"%02x", result[i]];
}
return MD5_String;
}
+ (NSString *)signHmacString:(NSString*)text withKey:(NSString*)key{
constchar *cKey = [keycStringUsingEncoding:NSUTF8StringEncoding];
constchar *cData = [textcStringUsingEncoding:NSUTF8StringEncoding];
uint8_t cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey,strlen(cKey), cData, strlen(cData), cHMAC);
NSString *hash= nil;
NSMutableString* output = [NSMutableStringstringWithCapacity:CC_SHA256_DIGEST_LENGTH *2];
for(int i =0; i < CC_SHA256_DIGEST_LENGTH; i++){
[output appendFormat:@"%02x", cHMAC[i]];
}
hash = output;
return hash;
}
3. 配置文件。其中商户秘钥是不正确的。// ApplePayCommont.h
// Apple_Pay
//
// Created by 周双建 on 16/2/25.
// Copyright © 2016年周双建. All rights reserved.
//
#ifndef ApplePayCommont_h
#define ApplePayCommont_h
商户号
static NSString *kLLOidPartner =@"201602190000022156";
// 商户秘钥
static NSString *kLLPartnerKey =@"201601250000011007";//这个不匹配,切记
NSString *kAPMerchantID =@"merchant.com.zsj.ApplePay";
#endif
4.订单的创建。#pragma mark 创建订单
-(void)CreateOrderInfo{
// 获取当前时间
NSDateFormatter * DateFormatter = [[NSDateFormatteralloc]init];
[DateFormatter setDateFormat:@"yyyyMMddHHmmss"];
// 将时间转化成字符串
NSString * PresentTimeString = [DateFormatterstringFromDate:[NSDatedate]];
// 订单前的标志
NSString * OrderPresentSign = @"LL";
// 加密的形式 MD5 RSA 哈希
NSString * SignType = @"MD5";
// 声明一个可变的参数字典
self.OrderInfoMutableDict = [NSMutableDictionarydictionaryWithCapacity:0];
NSMutableDictionary * PrameMutableDict = [NSMutableDictionarydictionary];
[PrameMutableDict setDictionary:@{
//签名方式String RSA 或者 MD5
@"sign_type":SignType,
//商户业务类型是String(6)虚拟商品销售:101001
@"busi_partner":@"101001",
//商户订单时间dt_order是String(14)格式:YYYYMMDDH24MISS 14位数字,精确到秒
@"dt_order":PresentTimeString,
//交易金额money_order 是Number(8,2) 该笔订单的资金总额,单位为RMB-元。大于0的数字,精确到小数点后两位。如:49.65
@"money_order":@"0.10",
//服务器异步通知地址notify_url 是String(64) 连连钱包支付平台在用户支付成功后通知商户服务端的地址。
@"notify_url":@"http://test.yintong.com.cn:80/apidemo/API_DEMO/notifyUrl.htm",
//商户唯一订单号no_order 是String(32) 商户系统唯一订单号
@"no_order":[NSStringstringWithFormat:@"%@%@",OrderPresentSign,PresentTimeString],
//商品名称name_goods 否String(40)
@"name_goods":@"琴女的皮肤",
//订单附加信息info_order 否String(255) 商户订单的备注信息,还可以详细描述。
@"info_order":PresentTimeString,
//分钟为单位,默认为10080分钟(7天),从创建时间开始,过了此订单有效时间此笔订单就会被设置为失败状态不能再重新进行支付。
@"valid_order":@"10080",
//@"shareing_data":@"201412030000035903^101001^10^分账说明1|201310102000003524^101001^11^分账说明2|201307232000003510^109001^12^分账说明3"
// 分账信息数据 shareing_data 否 变(1024)
//@"risk_item":@"{\"user_info_bind_phone\":\"13958069593\",\"user_info_dt_register\":\"20131030122130\"}",
//风险控制参数否 此字段填写风控参数,采用json串的模式传入,字段名和字段内容彼此对应好
//商户用户唯一编号否 该用户在商户系统中的唯一编号,要求是该编号在商户系统中唯一标识该用户
// user_id,一个user_id标示一个用户
// user_id为必传项,需要关联商户里的用户编号,一个user_id下的所有支付银行卡,身份证必须相同
// demo中需要开发测试自己填入user_id,可以先用自己的手机号作为标示,正式上线请使用商户内的用户编号
@"risk_item" : [ApplePayAlgorithmGetString:@{@"user_info_dt_register":@"20131030122130",@"cinema_name":@"大电影院",@"book_phone":@"18811520397"}],@"user_id":@"xu20160215",
@"oid_partner":kLLOidPartner
}];
self.OrderInfoMutableDict = PrameMutableDict;
// matech ID 预支付AP ID
self.OrderInfoMutableDict[@"ap_merchant_id"] =kAPMerchantID;
if (kLLOidPartner.length >0) {
// 商户秘钥
self.OrderInfoMutableDict[@"oid_partner"] =kLLOidPartner;
return;
}
}
// 进行支付
- (void)payWithSignedOrder:(NSDictionary*)signedOrder
{
[LLAPPaySDK sharedSdk].sdkDelegate =self;
[[LLAPPaySDKsharedSdk] payWithTraderInfo:signedOrder
inViewController:self];
}
6.Auth 授权。#pragma mark 预授权
- (void)authWithSignedOrder:(NSDictionary*)signedOrder{
[LLAPPaySDK sharedSdk].sdkDelegate =self;
[[LLAPPaySDKsharedSdk] preauthWithTraderInfo:signedOrder
inViewController:self];
}
#pragma mark 支付结果的返回
- (void)paymentEnd:(LLPayResult)resultCode withResultDic:(NSDictionary*)dic{
[selfalertView:[ApplePayAlgorithmGetString:dic]];
switch (resultCode) {
casekLLPayResultSuccess:{
// 支付成功
NSString* result_pay = dic[@"result_pay"];
if ([result_pay isEqualToString:@"SUCCESS"])
{
[self alertView:@"SUCCESS"];
}
else if ([result_payisEqualToString:@"PROCESSING"])
{
//@"支付单处理中";
[self alertView:@"支付单处理中"];
}
else if ([result_payisEqualToString:@"FAILURE"])
{
// @"支付单失败";
[self alertView:@"支付单失败"];
}
else if ([result_payisEqualToString:@"REFUND"])
{
// @"支付单已退款";
[self alertView:@"支付单已退款"];
}
}
break;
casekLLPayResultFail:{
// 支付失败
[self alertView:@"支付失败"];
}
break;
casekLLPayResultCancel:{
// 用户取消支付
[self alertView:@"用户取消支付"];
}
break;
casekLLPayResultInitError:{
[selfalertView:@"支付初始化错误,订单信息有误,签名失败等"];
//支付初始化错误,订单信息有误,签名失败等
}
break;
casekLLPayResultInitParamError:{
[selfalertView:@"支付订单参数有误,无法进行初始化,未传必要信息等"];
}
break;
casekLLPayResultUnknow:{
//其他
[self alertView:@"其他"];
}
break;
default:
break;
}
}
8.切记,不要忘记添加代理。LLPaySdkDelegate