Unity3d 微信SDK接入
微信支付 Android和IOS 接入
微信官方文档
支付业务流程:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3
微信SDK和Demo下载地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1#
Android 篇
微信支付集成官方文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/Android.html
1、获取微信支付的APPID。
商户在微信开放平台申请开发应用后,微信开放平台会生成APP的唯一标识APPID,并开通微信支付功能。
2、创建Eclipse工程。
如果不太了解U3D和Android交互的可以参考以下链接:https://blog.csdn.net/wangningzk123/article/details/107158253
从官网上下载的Demo,导入Eclipse中
在自己的Eclipse工程中导入libammsdk.jar包,创建xxx.wxapi包名,复制WXPayEntryActivity.java文件到xxx.wxapi包名下,如你的bundleID是com.example.game就要创建com.example.game.wxapi。然后修改WXPayEntryActivity.java的onResp方法。
public void onResp(BaseResp resp) {
Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) //如果是微信支付
{
if(resp.errCode==0)//支付成功
{
}
else if(resp.errCode==-2)//支付取消
{
}
else//支付失败
{
}
UnityPlayer.UnitySendMessage("Main Camera","WeChatPayResult", String.valueOf(resp.errCode)); //通知U3D支付结果
}
this.finish();//别忘记最后要将这个页面finish()
}
最后在自己的MainActivity中添加如下方法:
/** 微信支付 */
private IWXAPI wxApi;
public void WeChatPay(String appId,String partnerId,String prepayId,String nonceStr,String timeStamp,String sign){
wxApi = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
PayReq request = new PayReq();
request.appId = appId;
request.partnerId =partnerId;
request.prepayId= prepayId;
request.packageValue = "Sign=WXPay";
request.nonceStr= nonceStr;
request.timeStamp= timeStamp;
request.sign = sign; //签名方式可以微信支付官方文档
Log.d("Unity",request.checkArgs()+"");//输出验签是否正确
wxApi.sendReq(request);
}
3、Unity中调用微信支付。
IOS篇
微信支付集成官方文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html
1、下载Demo导入WechtSDK1.8.2文件夹下的libWeChatSDK.a、WechatAuthSDK.h、WXApi.h、WXApiObject.h4个文件,导入Control文件夹下的WXApiManager.h、WXApiManager.m2个文件用于支付通知,然后修改WXApiManager.m的resp方法
#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp {
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
NSString *lat;
lat = [NSString stringWithFormat:@"%d", resp.errCode];
UnitySendMessage("Main Camera","WeChatPayResult", [lat UTF8String]);//通知U3D支付结果
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}else {
}
}
2、创建iOSUtilUnity3dBridge.h和iOSUtilUnity3dBridge.mm文件,调用微信支付SDK,如需了解U3D和IOS交互的可参考以下链接:https://blog.csdn.net/wangningzk123/article/details/107359522
参考代码如下:
#import "iOSUtilUnity3dBridge.h"
#import "WXApi.h"
@implementation iOSUtilUnity3dBridge
void _weChatPay(char *appid, char *partnerid, char *prepayid, char *noncestr, char *timestamp, char *sign, char *package)
{
NSLog(@"Msg ==========_weChatSpend");
PayReq *request = [[PayReq alloc] init];
request.openID = [NSString stringWithUTF8String: appid];
request.partnerId = [NSString stringWithUTF8String: partnerid];
request.prepayId = [NSString stringWithUTF8String: prepayid];
request.nonceStr = [NSString stringWithUTF8String: noncestr];
//NSMutableString *stamp = [[NSString stringWithUTF8String: timestamp] mutableCopy];
request.timeStamp = [[NSString stringWithUTF8String: timestamp] intValue];//stamp.intValue;
request.package = [NSString stringWithUTF8String: package];
request.sign = [NSString stringWithUTF8String:sign];
[WXApi sendReq:request];
}
@end
编写C#代码调用该方法
[DllImport("__Internal")]
private static extern void _weChatPay(string appid, string partnerId, string prepayId, string nonceStr, string timeStamp, string sign, string package); //微信支付
public static void WechatPay(string appId, string partnerId, string prepayId, string nonceStr, string timeStamp, string sign, string package)
{
_weChatPay(appId, partnerId, prepayId, nonceStr, timeStamp, sign, package);
}
3、 在 Xcode 中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序 id(如下图所示)。
4、在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin 和weixinULAPI(如下图所示)。
5、在 XCode 工程的 Build Phases -> Link Binary With Libraries 中点击下面的 + 号,添加类库
Security.framework、CoreTelephony.framework、SystemConfiguration.framework、libz.dylib、libsqlite3.0.dylib、libc++.dylib。
6、Unity中调用微信支付。
本文地址:https://blog.csdn.net/wangningzk123/article/details/107378537