Unity3d access payment for IOS, unity3dios
I am unity3D development because the project requires access to payment, step by step fill in. And everyone's experience in risk!
This is because the server is not in place. So all processing is done on the client!
I would like to share some of the main files with you!
1. The initialization parameter info is passed from unity.
[Html] view plaincopy
// Specific implementation of the C function UNity3d call
//**********************************
Void_startUp (char * info)
{
[NativeIOSManagerinstance];
// NSLog (@ "sent parameters: >>% s", info );
NSString * initInfo = [nsstringstringwithuf8string: info];
NSDictionary * dic = [iOSManagerdictionaryWithJsonString: initInfo];
If (dic! = Nil)
{
IOSManager. WX_AppID = dic [@ "AppID"];
IOSManager. WX_MCHID = dic [@ "MCH_ID"];
IOSManager. WX_APP_Secret = dic [@ "APP_Secret"];
IOSManager. WX_AppKey = dic [@ "APP_Key"];
IOSManager. WX_PayArticleInfo = dic [@ "weChatPayArticleInfo"];
IOSManager. WX_PayArticleCheck = dic [@ "weChatPayArticleCheck"];
[WXApiregisterApp: iOSManager. WX_AppID];
NSLog (@ "register ......");
} Else
{
NSLog (@ "parameter assignment failed ");
[IOSManagerResoultWeChatPayFail: @ "initialization failed"];
}
// NSLog (@ "iOSManager. WX_AppID: % @", iOSManager. WX_AppID );
// NSLog (@ "iOSManager. WX_MCHID: % @", iOSManager. WX_MCHID );
// NSLog (@ "iOSManager. WX_APP_Secret: % @", iOSManager. WX_APP_Secret );
// NSLog (@ "iOSManager. WX_AppKey: % @", iOSManager. WX_AppKey );
// NSLog (@ "iOSManager. WX_PayArticleInfo: % @", iOSManager. WX_PayArticleInfo );
// NSLog (@ "iOSManager. WX_PayArticleCheck: % @", iOSManager. WX_PayArticleCheck );
}
2. This step is to construct the unified order data. The parameters here are the parameters of the project. You do not need to delete them.
[Html] view plaincopy
Void_WechatPay (char * gameid, char * articleid)
{
If ([WXApiisWXAppSupportApi]! = 0)
{
IOSManager. WX_GameID = [nsstringstringwithuf8string: gameid];
IOSManager. WX_ArticledID = [nsstringstringwithuf8string: articleid];
NSString * urlTag = [NSStringstringWithFormat: @ "% @ GameID =%@ & ProductID =%@", iOSManager. WX_PayArticleInfo, iOSManager. WX_GameID, iOSManager. WX_ArticledID];
NSLog (@ "urlTag: % @", urlTag );
NSURL * url = [NSURLURLWithString: urlTag];
NSError * err = nil;
NSString * jsonString = [NSStringstringWithContentsOfURL: urlencoding: NSUTF8StringEncodingerror: & err];
NSLog (@ "App server return: % @", jsonString );
If (jsonString! = Nil)
{
NSDictionary * dic = [iOSManagerdictionaryWithJsonString: jsonString];
If (dic! = Nil)
{
IOSManager. WX_Noncestr = dic [@ "SjNumber"];
IOSManager. WX_OutTradNO = dic [@ "OrderID"];
IOSManager. WX_PrduceName = dic [@ "ProductName"];
IOSManager. WX_Price = dic [@ "Price"];
// Intprice = [dic [@ "Price"] intValue] * 100;
// IOSManager. WX_Price = [NSStringstringWithFormat: @ "% d", price];
DataMD5 * date = [[DataMD5alloc] initWithAppid: iOSManager. WX_AppID
Mch_id: iOSManager. WX_MCHID
Nonce_str: iOSManager. WX_Noncestr
Partner_id: iOSManager. WX_AppKey
Body: @ "912 YULE"
Out_trade_no: iOSManager. WX_OutTradNO
Total_manager: iOSManager. WX_Price
Spbill_create_ip: [getIPhoneIPgetIPAddress]
Notify_url: @ "www.baidu.com"
Trade_type: @ "APP"];
NSString * sign = [dategetSignForMD5];
// Set parameters and convert them to xml format
NSMutableDictionary * dic = [NSMutableDictionarydictionary];
[DicsetValue: iOSManager. WX_AppIDforKey: @ "appid"]; // public account ID
[DicsetValue: iOSManager. WX_MCHIDforKey: @ "mch_id"]; // merchant ID
[DicsetValue: iOSManager. WX_NoncestrforKey: @ "nonce_str"]; // random string
[DicsetValue: signforKey: @ "sign"]; // Signature
[DicsetValue: @ "912 YULE" forKey: @ "body"]; // product description
[DicsetValue: iOSManager. WX_OutTradNOforKey: @ "out_trade_no"]; // Order Number
[DicsetValue: iOSManager. WX_PriceforKey: @ "total_fee"]; // amount
[DicsetValue: [getIPhoneIPgetIPAddress] forKey: @ "spbill_create_ip"]; // Terminal IP
[DicsetValue: @ "www.baidu.com" forKey: @ "yy_url"]; // notification address
[DicsetValue: @ "APP" forKey: @ "trade_type"]; // transaction type
// Convert to an xml string
NSString * string = [dicXMLString];
[IOSManagergetProduceArgs: string];
} Else
{
NSLog (@ "An error occurred while communicating with the APP server ");
[IOSManagerResoultWeChatPayFail: @ "An error occurred while communicating with the APP server! "];
}
} Else
{
NSLog (@ "An error occurred while communicating with the APP server ");
[IOSManagerResoultWeChatPayFail: @ "An error occurred while communicating with the APP server! "];
}
} Else
{
[IOSManagerResoultWeChatPayFail: @ "not supported"];
}
}
3. Unified order of requests. The AFNetWorking library should be used for post requests,
I use version 3.0 for this library, and then manually add it. After adding the library, I need to import it to xcode in unity to modify the project. pch file, and add reference libraries for reference.
[Html] view plaincopy
# Pragmamark ================= obtain the unified order interface ======================
-(Void) getProduceArgs :( NSString *) httpString
{
AFHTTPSessionManager * manager = [AFHTTPSessionManagermanager];
Manager. responseSerializer = [[AFHTTPResponseSerializeralloc] init];
[Manager. requestSerializersetValue: @ "text/xml; charset = UTF-8" forHTTPHeaderField: @ "Content-Type"];
[Manager. requestSerializersetValue: @ "https://api.mch.weixin.qq.com/pay/unifiedorder" forHTTPHeaderField: @ "SOAPAction"];
[Manager. requestSerializersetQueryStringSerializationWithBlock: ^ NSString * (NSURLRequest * request, NSDictionary * parameters, NSError * _ autoreleasing * error ){
ReturnhttpString;
}];
// NSLog (@ "httpString ====> 1: % @", httpString );
[ManagerPOST: wxuniiedorderurlparameters: httpStringprogress: ^ (NSProgress * _ NonnulluploadProgress ){
} Success: ^ (NSURLSessionDataTask * _ Nonnulltask, id_NullableresponseObject)
{
NSString * responseString = [[NSStringalloc] initWithData: responseObjectencoding: NSUTF8StringEncoding];
NSLog (@ "responseStringis % @", responseString );
NSDictionary * dic = [NSDictionarydictionaryWithXMLString: responseString];
// Determine the returned permission
If ([[dicobjectForKey: @ "result_code"] isrequired tostring: @ "SUCCESS"] & [[dicobjectForKey: @ "return_code"] isrequired tostring: @ "SUCCESS"]) {
// Initiate payment and set parameters
NSLog (@ "initiate payment, set parameters ");
PayReq * request = [[PayReqalloc] init];
Request. openID = [dicobjectForKey: @ "appid"];
Request. partnerId = [dicobjectForKey: @ "mch_id"];
Request. prepayId = [dicobjectForKey: @ "prepay_id"];
Request. package = @ "Sign = WXPay ";
Request. nonceStr = [dicobjectForKey: @ "nonce_str"];
IOSManager. WX_Prepay_id = request. prepayId;
// Convert the current event to a timestamp
NSDate * datenow = [NSDatedate];
NSString * timeSp = [NSStringstringWithFormat: @ "% ld", (long) [datenowtimeIntervalSince1970];
UInt32timeStamp = [timeSpintValue];
Request. timeStamp = timeStamp;
// Signature encryption
DataMD5 * md5 = [[DataMD5alloc] initWithAppid: iOSManager. WX_AppID
Mch_id: iOSManager. WX_MCHID
Nonce_str: iOSManager. WX_Noncestr
Partner_id: iOSManager. WX_AppKey
Body: @ "912 YULE"
Out_trade_no: iOSManager. WX_OutTradNO
Total_manager: iOSManager. WX_Price
Spbill_create_ip: [getIPhoneIPgetIPAddress]
Notify_url: @ "www.baidu.com"
Trade_type: @ "APP"];
Request. sign = [md5createMD5SingForPay: request. openID
Partnerid: request. partnerId
Prepayid: request. prepayId
Package: request. package
Noncestr: request. nonceStr
Timestamp: request. timeStamp];
//// Call
// NSLog (@ "request. openID: % @", request. openID );
// NSLog (@ "request. partnerId: % @", request. partnerId );
// NSLog (@ "request. prepayId: % @", request. prepayId );
// NSLog (@ "request. package: % @", request. package );
// NSLog (@ "request. nonceStr: % @", request. nonceStr );
// NSLog (@ "request. timeStamp: % u", (unsignedint) request. timeStamp );
// NSLog (@ "request. sign: % @", request. sign );
[WXApisendReq: request];
// [WXApion]
// [MBProgressHUDhideHUD];
} Else {
// LXLog (@ "the parameter is incorrect. Check the parameter ");
// [MBProgressHUDhideHUD];
// [MBProgressHUDshowError: @ "payment error! "];
[IOSManagerResoultWeChatPayFail: @ "An error occurred while communicating with the server! "];
}
} Failure: ^ (NSURLSessionDataTask * _ Nullabletask, NSError * _ Nonnullerror)
{
NSLog (@ "erroris % @", error );
[IOSManagerResoultWeChatPayFail: @ "An error occurred while communicating with the server! "];
}];
}
Then unity3D end
4. After the payment is triggered and completed, the payment is called back.
-(void)onResp:(BaseResp *)resp {Not executed. Then, write the callback in the exported xcode project's UnityAppController. mm to get the callback.
Header file