微信支付宝服务器在哪里,支付宝支付和微信支付,简单粗暴,哪里需要写哪里...

首先,大家10.24程序员节快乐!哈哈哈(然而还是单身,快乐个P哦) 这段时间一直在开发新的项目,1.0版本是和另外一个同事一个月协同开发完成的。然后修修改改什么的,今天发布了1.0.1版本。一直拖到了现在写。今天记录一下新项目和原来的项目都更新过的支付支付宝支付和微信支付,当然想了解ApplePay的也可以在下面留言哦。好了,下面进入正题。(支付的SDK ,环境什么的自己按照官网的文档一步一步来就可以了,包括支付流程原理什么的网上一大堆,这里只说在哪里需要什么代码)

一.支付宝支付

原来的那个项目中,支付宝支付所需的支付信息都是在项目里写成常量的,但上星期收到了一封来自支付宝的邮件指出了什么安全性的问题,然后加密,拼接什么的都在后台操作了,在客户端这边只需拿到后台给的接口去请求下来拿到后台处理好的字符串调起支付就行了。(至于后台的操作我就不清楚了,尴尬)

下面就是点击支付按钮的点击事件

- (void)aliPay {

//将商品信息赋予AlixPayOrder的成员变量

Order*order = [[Orderalloc]init];

order.tradeNO=self.orderId;//订单ID(由商家自行制定)

order.productName=@"测试";//商品标题

order.productDescription=@"商品描述";//商品描述

order.amount= [NSStringstringWithFormat:@"%.2f",self.payAmount];//商品价格

NSString*urlString = [NSStringstringWithFormat:@"%@/pays/app/alipay",host];

NSDictionary*param =@{@"orderId":self.orderId};

XSAPIManager*manager = [XSAPIManagermanager];

[managerGET:urlStringparameters:paramsuccess:^(idresponseObject) {

AliPay*model = [AliPaymj_objectWithKeyValues:responseObject];

[[AlipaySDKdefaultService]payOrder:model.data.payInfofromScheme:URL_SCHEMEScallback:^(NSDictionary*resultDic) {//调起支付并处理返还的结果

NSLog(@"reslut payOrder = %@",resultDic);

//NSLog(@"reslut = %@",resultDic);

if([resultDic[@"resultStatus"]isEqual:@"9000"]) {

[self.navigationControllerpopViewControllerAnimated:YES];

XSOrderCompletedViewController*vc = [[XSOrderCompletedViewControlleralloc]init];

vc.orderId=self.orderId;

[self.navigationControllerpushViewController:vcanimated:YES];

//支付成功;

}

elseif([resultDic[@"resultStatus"]isEqual:@"8000"]) {

//正在处理中;

}

elseif([resultDic[@"resultStatus"]isEqual:@"4000"]) {

//订单支付失败;

}

elseif([resultDic[@"resultStatus"]isEqual:@"6001"]) {

//[self.navigationController popViewControllerAnimated:YES];

//XSOrderCompletedViewController *vc = [[XSOrderCompletedViewController alloc]init];

//vc.orderId = self.orderId;

//[self.navigationController pushViewController:vc animated:YES];

//

}

elseif([resultDic[@"resultStatus"]isEqual:@"6002"]) {

//您的网络连接出错

}

else{

//支付失败

}

}];

//}

}failure:^(NSError*error) {

NSLog(@"%@",error);

} ];

}

值得注意的是,这里支付宝支付调起后就返回了结果,所以在这里就可以根据返回结果进行操作。而微信支付的回调是在AppDelegate里面,所以我打算在最后面说一AppDelegate里的代码。

二.微信支付

微信支付同样,也是通过一个接口向后台发起请求然后处理返回的数据调起支付就ok了。下面是点击微信支付按钮的点击事件代码。

- (void)wechatPay {

NSString*weixinBackStr = [selfwxPay];

if(![weixinBackStrisEqualToString:@""]) {

NSLog(@"微信支付返回的信息:%@",weixinBackStr);

}

}

- (NSString*)wxPay {

/**添加两个判断**/

//判断是否安装了微信

if( ![WXApiisWXAppInstalled]){

NSLog(@"没有安装微信");

returnnil;

}elseif(![WXApiisWXAppSupportApi]){

NSLog(@"不支持微信支付");

returnnil;

}

//向服务器端获取支付数据(商家自定义)

NSString*urlString = [NSStringstringWithFormat:@"%@/pays/app/wxpay",host];

NSDictionary*param =@{@"orderId":self.orderId};

XSAPIManager*manager = [XSAPIManagermanager];

[manager POST:url StringrequestType:0 parameters:paramsuccess:^(idresponseObject) {

NSLog(@"responseObject%@",responseObject);

XSWXPayReq*wxPayReq = [XSWXPayReqmj_objectWithKeyValues:responseObject];

if(wxPayReq.code==1) {

//调起微信支付

PayReq* req= [[PayReqalloc]init];

req.partnerId= wxPayReq.data.partnerId;

req.prepayId= wxPayReq.data.prepayId;

req.nonceStr= wxPayReq.data.nonceStr;

req.timeStamp= wxPayReq.data.timeStamp;

req.package= wxPayReq.data.packageValue;

req.sign= wxPayReq.data.sign;

[WXApisendReq:req];

}else{

}

}failure:^(NSError*error) {

NSLog(@"%@",error);

}];

return@"";

}

三 AppDelagate里所需的代码

- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {

return[WXApihandleOpenURL:urldelegate:self];

}

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

BOOLresult = [UMSocialSnsServicehandleOpenURL:url];

if(result ==FALSE) {

//调用其他SDK,例如支付宝SDK等

/**

*跳转支付宝钱包进行支付,处理支付结果

*/

if([url.hostisEqualToString:@"safepay"]) {

[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {

NSLog(@"result safepay = %@",resultDic);

}];

}

if([url.hostisEqualToString:@"platformapi"]) {

[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {

NSLog(@"result platformapi = %@",resultDic);

}];

}

//微信的支付回调

if([url.hostisEqualToString:@"pay"]) {

return[WXApihandleOpenURL:urldelegate:self];

}

}

returnYES;

}

- (void)onResp:(BaseResp*)resp

{

NSString* strMsg = [NSStringstringWithFormat:@"errorCode: %d",resp.errCode];

NSLog(@"strMsg: %@",strMsg);

NSString* errStr= [NSStringstringWithFormat:@"errStr: %@",resp.errStr];

NSLog(@"errStr: %@",errStr);

NSString* strTitle;

//判断是微信消息的回调

if([respisKindOfClass:[SendMessageToWXRespclass]])

{

strTitle = [NSStringstringWithFormat:@"发送媒体消息的结果"];

}

NSString* wxPayResult;

//判断是否是微信支付回调(注意是PayResp而不是PayReq)

if([respisKindOfClass:[PayRespclass]])

{

//支付返回的结果,实际支付结果需要去微信服务器端查询

strTitle = [NSStringstringWithFormat:@"支付结果"];

switch(resp.errCode)

{

caseWXSuccess:

{

strMsg =@"支付结果:";

NSLog(@"支付成功: %d",resp.errCode);

wxPayResult =@"success";

break;

}

caseWXErrCodeUserCancel:

{

strMsg =@"用户取消了支付";

NSLog(@"用户取消支付: %d",resp.errCode);

wxPayResult =@"cancle";

break;

}

default:

{

strMsg = [NSStringstringWithFormat:@"支付失败! code: %derrorStr: %@",resp.errCode,resp.errStr];

NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr);

wxPayResult =@"faile";

break;

}

}

//发出通知

NSNotification* notification = [NSNotificationnotificationWithName:@"WXPay"object:wxPayResult];

[[NSNotificationCenterdefaultCenter]postNotification:notification];

}

}

上面的- (void)onResp:(BaseResp)resp就是调起微信支付后返回的结果,那么问题来了,支付后的操作总不能在AppDelegate里面吧,所以- (void)onResp:(BaseResp)resp最后的那个通知就是为了避免这个尴尬用的。发出通知,然后在点击调起支付的controller里接收通知,代码如下:

- (void)viewDidLoad {

[superviewDidLoad];

//默认选择支付宝

self.option=XSOnlinePayOptionAliPay;

//监听通知

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getResult:)name:@"WXPay"object:nil];

}

- (void)dealloc {

[[NSNotificationCenterdefaultCenter]removeObserver:self];

}

#pragma mark微信支付结果回调通知操作

- (void)getResult:(NSNotification*)notification {

NSLog(@"userInfo: %@",notification.userInfo);

if([notification.objectisEqualToString:@"success"]){

XSOrderCompletedViewController*vc = [[XSOrderCompletedViewControlleralloc]init];

vc.orderId=self.orderId;

[self.navigationControllerpushViewController:vcanimated:YES];

}

elseif([notification.objectisEqualToString:@"cancle"]){

[selfalert:@"提示"msg:@"您已经取消了支付"];

}else{

[selfalert:@"提示"msg:@"支付失败!请重新支付"];

}

}

- (void)alert:(NSString*)title msg:(NSString*)msg

{

UIAlertView*alter = [[UIAlertViewalloc]initWithTitle:titlemessage:msgdelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

[altershow];

}

四 补充

一般来说,当同时有微信支付和支付宝支付的时候,一般页面只有一个确认支付按钮,如下图

0258e9c122f2

这时只需在定义个枚举类型,

typedefNS_ENUM(NSInteger, XSOnlinePayOption) {

XSOnlinePayOptionAliPay,

XSOnlinePayOptionWeChatPay,

XSOnlinePayOptionApplePay

};

然后在支付页面的controller中定义个对象

@property(assign,nonatomic)XSOnlinePayOption option;

在viewDidLoad里面设置默认支付方式为支付宝,然后上图1按钮的点击事件代码如下

- (IBAction)paySwitch:(UIButton*)sender {

if(sender ==self.aliButton) {

self.option=XSOnlinePayOptionAliPay;

[self.aliButtonsetImage:[UIImageimageNamed:@"selected_non"]forState:UIControlStateNormal];

[self.wechatButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

[self.applePayButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

}elseif(sender ==self.wechatButton) {

self.option=XSOnlinePayOptionWeChatPay;

[self.wechatButtonsetImage:[UIImageimageNamed:@"selected_non"]forState:UIControlStateNormal];

[self.aliButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

[self.applePayButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

}elseif(sender ==self.applePayButton) {

self.option=XSOnlinePayOptionApplePay;

[self.applePayButtonsetImage:[UIImageimageNamed:@"selected_non"]forState:UIControlStateNormal];

[self.wechatButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

[self.aliButtonsetImage:[UIImageimageNamed:@"unselected"]forState:UIControlStateNormal];

}

}

图中2的点击事件代码如下

- (IBAction)payNow:(UIButton*)sender {

if(self.option==XSOnlinePayOptionAliPay) {

[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];

[self.orderManagervalidatePayInfo:self.orderIdpayType:@"zhifubao"];

}elseif(self.option==XSOnlinePayOptionWeChatPay) {

[self.orderManagervalidatePayInfo:self.orderIdpayType:@"wxpay"];

}elseif(self.option==XSOnlinePayOptionApplePay) {

//[self applePay];

}

}

这样就能自由选择支付方式调起支付了。

微信支付和支付宝支付网上的资料一大堆,写法也各不一样,反正能调起支付完成功能就OK啦。有不足的地方欢迎指出,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值