IOS doesn’t use WeChat SDK to evoke WeChat payments

To know how the WeChat SDK tune up the WeChat client, let’s look at what WeChat SDK has done

preparation in advance

  1. Pre access preparatory work includes merchant signing and key configuration, has been completed, businesses may skip.
  2. Download WeChat SDK WeChat SDK download address

SDK integration process

  1. Copy the extracted WeChat OpenSDK folder into the project folder and import it into the development environment. LibWeChatSDK.a,
    , WechatAuthSDK.h,
    , WXApi.h,
    , WXApiObject.h

Import system dependency Library

libc++.tbd
CoreTelephony.framework
libsqlite3.0.tbd add
libz.tbd
SystemConfiguration.framework
and libWeChatSDK.a link in Binary before the introduction of With Libraries.

To this end, the SDK integration has been completed

Configuration code

  • Add URL Schemes
    , click the project name, click the Info tab, in the URL Types option, click ‘+’, and enter the WeChat’s registration code in ‘URL Schemes’
IOS doesn't use WeChat SDK to evoke WeChat payments
C1000
  • Header files are referenced in the payment class:
#import "WXApi.h""
  • Configuring wxpay payments
#pragma mark ============== configuration wxpay (void) payAction{- ============== payment request to you / / launched network, server request 1 orders ID, 2 yuan (unit: 3 points), the best server generated content description / orders, in order to see you, so in the local generation! [NetWorkTool dingiD:[self generateTradeNO] andDetail:@ "(NSDictionary *responseObject)" success:^ {/ / server / data transfer from WeChat wxreq alloc] init] pay PayReq* = [[PayReq; wxreq.openID = responseObject[@ appid / * * * / "result", "appid"]; / * * @ caifutong business to business applications of ID / wxreq.partnerId responseObject[@ = "result"][@ "partnerid"]; / * * * / pre payment orders wxreq.prepayId = "result" responseObject[@ "prepayid"][@]; / * * * / random string, anti replay wxreq.nonceStr = "result" responseObject[@ "noncestr"][@]; / * * * / time stamp, anti replay wxreq.timeStamp = [re SponseObject[@ "result" timestamp "intValue]"][@]; / * * business according to the caifutong document fill data and signature / wxreq.package = responseObject[@ "result"][@ "package"]; / * * business according to the WeChat open platform document to the data signature / wxreq.sign responseObject[@ = "result"][@ "sign"]; [WXApi sendReq:wxreq];} failure:^ (NSError *error) {NSLog ("% @ @", error);}]; #pragma mark ==============} to generate random order number ============== - (NSString *) generateTradeNO static int {kNumber = 15; NSString = *sourceStr @ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; NSMutableString *resultStr = [[NSMutableString alloc] init]; srand ((unsigned) time (0 for (int)); I = 0; I < kNumber; i++) {unsigned index = rand ([sourceStr length] NSString)%; *oneStr = [sourceStr substringWithRange:NSMakeRange (index, 1)]; [resultStr appendString:oneStr];} return resultStr;}
  • Configure return processing code

In the AppDelegate.h file, increase the WeChat protocol:

#import "WXApi.h", @interface AppDelegate: UIResponder, <, UIApplicationDelegate, WXApiDelegate>

In the AppDelegate.m file, add reference code:

- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary *) launchOptions SDK [WXApi registerApp:@ {/ / initialize WeChat "Wx * * * * * * * * *"]; return YES;} - (BOOL) application: (UIApplication * application) openURL: (NSURL * URL) sourceApplication: (NSString *) sourceApplication annotation: (ID) annotation @brief {/ *! WeChat through URL start App transfer data in application:openURL:sourceApplication: or annotation: * * need application:handleOpenURL. * @param URL delivers the URL * @param delegate WXApiDelegate object passed by WeChat when it starts third party applications. It is used to receive messages triggered by WeChat. * @return successfully returns YES and fails to return NO. * / return [WXApi handleOpenURL:url delegate:self];} - (void) onResp: (BaseResp * RESP) {/ / payment return results, the actual payment results need to go to the WeChat server query NSString *strMsg = [NSString stringWithFormat:@ switch] "payment results"; (resp.errCode) {case WXSuccess: strMsg = @ "payment results! "; NSLog (@" successful payment PaySuccess =%d, resp.errCode, retcode); break default:; strMsg [NSString = stringWithFormat:@ "payment results: failure! Retcode =%d, retstr, resp.errCode, resp.errStr] =% @; NSLog (@ retcode "error =%d, retstr = resp.errCode, resp.errStr,"% @ break;}});

No WeChat SDK evokes WeChat payments

  1. As we all know, iOS is a closed system, and applications can not read files from each other. Apple uses URL Scheme to implement this function. Through the various APP designed in line with Apple’s unified specification of URL Scheme, Url Scheme is used to transfer information, URL Scheme is for the convenience of APP calls between each other and design. You can open the app through the system OpenURl through a link like URL, and you can pass some parameters. Each URL must be able to uniquely identify a APP, if you set the URL and other APP URL conflict, at this time, your APP will not be called up, the reason is that when APP when installed in the system has been registered with the Scheme URL this APP, but if you are consistent after installation then, the system does not call you APP, because the scheme URL your APP is set to be covered.
  2. Analysis, WeChat, Alipay and SDK share payment function, are passed through the URL scheme content
  3. Then we can check the WeChat SDK call WeChat customer service side payment, the delivery of the URL Scheme is what content, if you can find its coding rules, then you can not use SDK payment

So, to initiate the payment process, the system will arouse the WeChat client, our idea is to see the WeChat SDK send URL Scheme WeChat client, to forge a WeChat, is to declare a WeChat URL system and the same Scheme address

View the WeChat URL Scheme address

After testing: WeChat’s URL Scheme is: weixin://

Well, we have a new project named GetPayURLScheme

Then sign up for custom URL Scheme

Click info.plist in the item (info.plist in non test) and select right Open As – Source Code:
to join:

< key> CFBundleURLTypes< /key> < array> < dict> < key> CFBundleURLSchemes< /key> < array> < string> weixin< /string> < /array> < key> CFBundleURLName< /key& gt; < string> 1111< /string> < /dict> < /array>

At this point, click info.plist in the item (not info.plist in test) and select the right Open As – list:

This generates the URLscheme as shown below:

IOS doesn't use WeChat SDK to evoke WeChat payments
C1000

Add in AppDelegate.m

The application of APP / / receiving urlScheme will respond to this method by value - (BOOL) application: (UIApplication * application) openURL: (NSURL * URL) sourceApplication: (NSString *) sourceApplication annotation: (ID) annotation{/ UIAlertView *alert display the interception of the urlscheme = [[UIAlertView alloc]initWithTitle:@ received urlScheme message:url.absoluteString delegate:nil cancelButtonTitle:nil "otherButtonTitles:@ nil]," OK "[alert; show]; UIPasteboard *pasteboard to the clipboard [UIPasteboard = generalPasteboard]; pasteboard.string = url.absoluteString; return YES;}

Run to the cell phone, this time, you, no matter what the app to WeChat payment, the call was just new named GetPayURLScheme project,
, as shown in the following:

IOS doesn't use WeChat SDK to evoke WeChat payments
IOS doesn't use WeChat SDK to evoke WeChat payments

You can see: that urlScheme:

Weixin://app/wxdf261c3b90ffbc25/pay/? NonceStr=Ho7nAFOALQpVqSM7& package=Sign%3DWXPay& partnerId=1236537302& prepayId=wx201606052201506009de63980169148758& timeStamp=1465135310& sign=5A3EF234382FD61D36CEC104723387ED& signType=SHA1

It can be seen that the stitching method is:

NSString *str = [NSString stringWithFormat:@ nonceStr=%@& package=Sign%%3DWXPay& weixin://app/%@/pay/? PartnerId=%@& prepayId=%@& timeStamp=%@& sign=%@& signType=SHA1, appid, noncestr, partnerid, prepayid, NSString [stringWithFormat:@%d, [timestamp intValue]], sign];

No SDK, just need to configure such a piece of code, WeChat payment can be completed!!!!!!!!

- (void) payAction{/ / initiated network requests to your server request 1 orders ID, 2 yuan (unit: 3 points), the best server generated content description / orders, in order to see you, so in the local generation! [NetWorkTool dingiD:[self generateTradeNO] andDetail:@ "(NSDictionary *responseObject)" success:^ appid NSString *appid {/ * * * responseObject[@ = "result"][@ "appid"]; / * * merchants caifutong merchants to apply for ID / NSString *partnerId responseObject[@ = "result"][@ "partnerid"]; / * * * NSString pre payment orders *prepayId = responseObject[@ result "][@" prepayid "]; / * * * NSString random string, anti replay *nonceStr =" result "responseObject[@" noncestr "][@]; / * * * / NSString time stamp, anti replay *timeStamp =" result "responseObject[@" timestamp "]; / * *][@ caifutong business according to the document data and fill in the signature / NSString *pac Kage responseObject[@ = "result"][@ "package"]; / * * business according to the WeChat open platform document to the data signature / NSString *sign responseObject[@ = "result"][@ "sign"]; / / *str = [NSString stringWithFormat:@ NSString generated URLscheme weixin:/ /app/%@/pay/? NonceStr=%@& package=Sign%%3DWXPay& partnerId=%@& prepayId=%@& timeStamp=%@& sign=%@& signType=SHA1 appid, nonceStr, partnerId, prepayId, [NSString, stringWithFormat:@%d, [timeStamp intValue], sign] [[UIApplication sharedApplication]; / / arouse the payment interface through the openURL openURL:[NSURL URLWithString:str]] method (NSError *error);} failure:^ {NSLog ("% @ @", error);}];}
IOS doesn't use WeChat SDK to evoke WeChat payments

Be careful:

  1. Best order server generation
  2. After that, you must go to the server to verify the payment status!!!

Be accomplished!!!!!!

Demo download (reprinted please indicate the source!)

  • GetPayURLScheme

Download address: click here to download

  • WXPay

Download address: click here to download