iOS第三方支付集成-微信支付
Posted xj_love
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS第三方支付集成-微信支付相关的知识,希望对你有一定的参考价值。
序言:说来惭愧,终于有支付的需求给我做了。。。哇嘎嘎,开动。。。文章尽量写的详细点,从自身出发,希望能给大家一点帮助。欢迎大佬指正。
支付流程:
步骤1:用户在商户APP中选择商品,提交订单,选择微信支付。
步骤2:商户后台收到用户支付单,调用微信支付统一下单接口。
步骤3:统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。
注意:package的值格式为Sign=WXPay
步骤4:商户APP调起微信支付。
步骤5:商户后台接收支付通知。
步骤6:商户后台查询支付结果。
一 下载微信支付Demo学习观摩
传送门
工欲善其事必先利其器,先看下demo总不是坏处,也可以学习一下腾讯大佬编码风格。总的来说,微信支付demo还是不难的。
二 正式接入准备工作
1.把demo里面的sdk导入到项目中
2.Command+B编译,如果报错,加入以下文件
3.Project-Info中,添加微信的AppKey
4.设置微信白名单
三 正式接入
1.AppDelegate设置
6.代码都在PayManager里面
PayManager.h
//
// PayManager.h
//
// Created by superButton on 2018/9/5.
// Copyright © 2018年 com.from. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "WXApi.h"
@protocol PayManagerDelegate <NSObject>
@optional
@end
@interface PayManager : NSObject<WXApiDelegate>
/**
代理
*/
@property (nonatomic, assign) id<PayManagerDelegate> delegate;
/**
单例
@return 单例
*/
+ (instancetype)sharedManager;
/**
微信支付
@param orderID 订单ID
@param success 成功回调
@param fail 失败回调
*/
- (void)payWithWeChatMethod:(NSString*)orderID successBlock:(GetBackBoolBlock)success failBlock:(GetBackBoolBlock)fail;
/**
支付宝支付
@param orderID 订单ID
@param success 成功回调
@param fail 失败回调
*/
- (void)payWithZhifubaoMethod:(NSString*)orderID successBlock:(GetBackBoolBlock)success failBlock:(GetBackBoolBlock)fail;
@end
PayManager.m
//
// PayManager.m
//
// Created by superButton on 2018/9/5.
// Copyright © 2018年 com.from. All rights reserved.
//
#import "PayManager.h"
@interface PayManager ()
@property (copy,nonatomic) GetBackBoolBlock successBlock;
@property (copy,nonatomic) GetBackBoolBlock failBlock;
@end
@implementation PayManager
+(instancetype)sharedManager
static dispatch_once_t onceToken;
static PayManager *instance;
dispatch_once(&onceToken, ^
instance = [[PayManager alloc] init];
);
return instance;
#pragma mark - 微信支付
- (void)payWithWeChatMethod:(NSString*)orderID successBlock:(GetBackBoolBlock)success failBlock:(GetBackBoolBlock)fail
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@""] = orderID;
kWeakSelf(weakSelf)
//自行请求自己后台,拿到订单必要参数
[[NetWorkingUtil netWorkingUtil] postDataWithPath:@"" parameters:params result:^(id obj, int status, NSString *msg)
if (status == 1)
weakSelf.successBlock = success;
weakSelf.failBlock = fail;
if(obj != nil)
NSMutableString *retcode = [obj objectForKey:@"retcode"];
if (retcode.intValue == 0)
NSMutableString *stamp = [obj objectForKey:@"timestamp"];
//调起微信支付,参数后台返回就行
PayReq* req = [[PayReq alloc] init];
req.openID = kWeXinKeyID;
req.partnerId = [obj objectForKey:@"partnerid"];
req.prepayId = [obj objectForKey:@"prepayid"];
req.nonceStr = [obj objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [obj objectForKey:@"package"];
req.sign = [obj objectForKey:@"sign"];
if (![WXApi sendReq:req])
//微信调起支付失败
if (weakSelf.failBlock)
weakSelf.failBlock(NO);
//日志输出
NSLog(@"appid=%@\\npartid=%@\\nprepayid=%@\\nnoncestr=%@\\ntimestamp=%ld\\npackage=%@\\nsign=%@",[obj objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
else
fail(NO);
else
fail(NO);
];
#pragma mark - 支付宝支付
- (void)payWithZhifubaoMethod:(NSString*)orderID successBlock:(GetBackBoolBlock)success failBlock:(GetBackBoolBlock)fail
#pragma mark - WXApiDelegate
//收到微信回调
- (void)onResp:(BaseResp *)resp
if([resp isKindOfClass:[PayResp class]])
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg = [NSString stringWithFormat:@"支付结果"];
switch (resp.errCode)
case WXSuccess:
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
[MBProgressHUD showSuccessOnView:kMAIN_WINDOW withMessage:strMsg];
if (_successBlock)
_successBlock(YES);
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
[MBProgressHUD showErrorOnView:kMAIN_WINDOW withMessage:strMsg];
if (_failBlock)
_failBlock(NO);
break;
else
///给微信发回调
- (void)onReq:(BaseReq *)req
@end
四 注意
剩下的就是对接了,这个视情况而定。后面还有篇文章,我会集合所有支付可能遇到的坑和解决方案。
iOS第三方支付集成-踩坑处理合集
iOS第三方支付集成-支付宝支付
高级ios开发交流群:855808379
以上是关于iOS第三方支付集成-微信支付的主要内容,如果未能解决你的问题,请参考以下文章