导入支付宝SDK后,在info.plist文件中设置一下
代码如下:
-(void)preparePayWithOrderId:(NSString *)orderid amount:(NSString *)amount productId:(NSString *)productId{
NSString *partner =PartnerID;
NSString *seller =SellerID;
NSString *privateKey =PartnerPrivKey;
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Orderalloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = orderid;//订单ID(由商家自行制定)
order.productName =@"客户端测试商品"; //商品标题
order.productDescription = productId;//商品描述
order.amount = [NSStringstringWithFormat:@"%@",amount];//商品价格
order.notifyURL =kpay_CreatAlipayUrl; //回调URL
order.service =@"mobile.securitypay.pay";
order.paymentType =@"1";
order.inputCharset =@"utf-8";
order.itBPay =@"30m";
order.showUrl =@"m.alipay.com";
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme =@"alisdkdemo";
//将商品信息拼接成字符串
NSString *orderSpec = [orderdescription];
NSLog(@"orderSpec = %@",orderSpec);
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer =CreateRSADataSigner(privateKey);
NSString *signedString = [signersignString:orderSpec];
//将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString =nil;
if (signedString !=nil) {
orderString = [NSStringstringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDKdefaultService] payOrder:orderStringfromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
//支付结果
if ([[resultDicobjectForKey:@"resultStatus"]isEqualToString:@"9000"]) {
//9000为支付成功
[MBProgressHUDhideHUDForView:self.viewwithDetailText:@"已成功参加该活动"afterDelay:kDismisHudForFalseanimated:YES];
[[NSNotificationCenterdefaultCenter]postNotificationName:kHaveFinishJoinActivityNOtiNameobject:nil];
[self.navigationControllerpopViewControllerAnimated:YES];
}
else
{
[MBProgressHUDhideHUDForView:self.viewwithDetailText:@"支付未完成"afterDelay:kDismisHudForFalseanimated:YES];
}
}];
}
}
再在appdelegate.m设置
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
return YES;
}