WeChat and Alipay to pay – look at me, I will be enough

I. Introduction

  • Before 1, wrote a Alipay WAP and Alipay wallet payment, unified callback, then there is a demand that can get a WeChat payment, block callback, actually WeChat paid API good, only one agent processing method of payment results, unlike the Alipay two back, of course, the use of simple block callback a lot, so I have a separate package of WeChat payment, block callback here do not explain the opening, we need it to my gitHub clone
  • 2, there is proposed to integrate Alipay and WeChat, this proposal is good, because the integrated payment function app generally have Alipay and WeChat, since the two kinds of needs, so it is very convenient for unified management! Therefore, this article focuses on the unified management tool package.

Analysis of two, Alipay and WeChat API

  • The author compares the Alipay and WeChat to pay API, analyze the similarities and differences of their interfaces: Alipay official document WeChat official documents (1) Alipay is not required to be registered in didFinishLaunchingWithOptions, while WeChat will need to call the registerApp registration (2) Alipay web callback, and WeChat isn’t of course, the integration of no effect (because will eventually be unified into a callback) (3) Alipay launched payment is the incoming order information (string type), while WeChat introduced a BaseReq class or its subclasses (payment is PayReq), on the basis of this difference can be passed to the ID type, then do internal judgment, for payment of the jump different, to look at their interface Alipay launched payment * / * * * * @param orderStr payment interface Order information * @param schemeStr call to pay the app registered in info.plist scheme * @param completionBlock Block wap for payment of the callback, the callback (non payment wallet payment jump) * / – (void) payOrder: (NSString * orderStr) fromScheme: (NSString *) schemeStr callback: (CompletionBlock) completionBlock; WeChat launched @brief send payment / *! Request to WeChat, waiting for WeChat to return onResp * * after the function call, will switch to WeChat’s interface. Third party applications wait for WeChat to return to onResp. WeChat will be called after the completion of asynchronous processing onResp. Support for the following types * SendAuthReq, SendMessageToWXReq, PayReq, etc.. * @param req specific send request, after calling the function, please release. * @return successfully returned to YES, failed to return to NO. * (BOOL) + sendReq: (BaseReq*) req; (4) Alipay launched payment not only incoming order information, will also need to pass appSchemes (Info – URL Types is configured in the App Schemes), and WeChat launched a payment only incoming order information, while its appSchemes registered in didFinishLaunchingWithOptions has been introduced, so I also can be considered in didFinishLaunchingWithOptions to bind a appSchemes Alipay, like WeChat, and then when you do not need to initiate the payment, only need to get on the line, of course, because the Url Scheme is stored in the Info.plist file, so it can be used for code acquisition, the caller does not need to pass, just need to follow the provisions of this tool on a fix (5) Alipay pay return not to return state enumeration type, In the resultStatus field callback returned in the dictionary, while WeChat is through enumeration returns, this time can be unified as enumeration, can refer to the WeChat
    WeChat and Alipay to pay - look at me, I will be enough
    Alipay to pay the return status code (screenshot from Alipay official document) WeChat and Alipay to pay - look at me, I will be enough
    WeChat to pay the return status code (screenshot from WeChat official documents) (6) Alipay each a status code corresponds to a state of information, and WeChat is only the wrong time (errCode = -1) have the corresponding state information, refer to Alipay, WeChat added manually to return status information

Three, integration

  • 1, Alipay payment integration (three steps) (1) because Alipay does not support Pod, then download the latest SDK, drag to the project WeChat and Alipay to pay - look at me, I will be enough
    only two resource file (2) according to Alipay official documents, import the required WeChat and Alipay to pay - look at me, I will be enough
    guide into the required Library (3) configuration in Info.plist Url Types Url Scheme WeChat and Alipay to pay - look at me, I will be enough
    add Alipay jump add Url Scheme
  • 2, WeChat payment integration (six steps) (1) as WeChat does not support Pod, download the latest SDK, drag to the project WeChat and Alipay to pay - look at me, I will be enough
    four documents (2) according to WeChat official documents, import the required WeChat and Alipay to pay - look at me, I will be enough
    document is old, shots from the official Demo
    * when you run the official Demo, found no problems, but their projects may appear below the situation, the next step to solve the WeChat and Alipay to pay - look at me, I will be enough
    if this is wrong, please go to the next step (3) or look at the official document, although old, you will find that in fact is a small libc++.tbd library, measured as CFNetwork.framework add is not the problem, the official Demo has not added, of course the best also added WeChat and Alipay to pay - look at me, I will be enough wpap60 3001br a small libc++.tbd Library of
    Command + B successfully * at this time, but when you run happily, you will find that the program crashes, the following tips, when the fact is found that breakpoint debugging calls WeChat registerApp method WeChat and Alipay to pay - look at me, I will be enough
    collapse (4) in build settings following Other Linker Flags added -ObjC, if still not, instead of -all_load at this time should be no problem WeChat and Alipay to pay - look at me, I will be enough
    add -all_load (5) Info.plist in the Url Types configuration added WeChat Url Scheme this jump, WeChat and Alipay to pay - look at me, I will be enough
    fully integrated add Url Scheme (6) of this operation should still have problems, suggesting that less Expected a type. Is WXAp IObject.h and WXApi.h less import the UIKit framework, because the official WeChat Demo used in the PCH file, the file into the UIKit framework, manually add into the problem

Four, packaging API

1, single case model, the only project to facilitate unified management

@author gitKong / * * * * * * / single case management + (instancetype) shareManager;

2, processing callback URL, need to be implemented in AppDelegate

* @author gitKong * * / * * jump URL, back to the application, need to achieve in the delegate * - (BOOL) fl_handleUrl: (NSURL * URL);

3, registered app, need to call in didFinishLaunchingWithOptions, binding URL Scheme

@author gitKong / * * * * * registered App, need to call in didFinishLaunchingWithOptions * - (void) fl_registerApp;

4, to initiate payment, incoming order parameter type is ID, if the incoming string corresponds to jump is Alipay payment; if the incoming PayReq object, WeChat to pay attention, this jump, not into the empty string or nil, inside the corresponding assertion; unified callback, not Alipay WAP or app, or WeChat pay, is through the block callback, the callback state codes have corresponding state information

/ * * * @author gitKong * * * * @param orderMessage to initiate the payment order information, if it is a string, the corresponding jump Alipay payment; if the incoming PayReq object, WeChat to pay attention, this jump, not into the empty string or nil * @param callBack callback, return state information * / - (void) fl_payWithOrderMessage: (ID) orderMessage callBack: (FLCompleteCallBack) callBack;

Five, usage (based on SDK integration)

1, in the AppDelegate callback processing, generally only need to achieve the following two methods can be, in order to avoid unnecessary trouble, the best of the three are written

@author gitKong * / * * * * the old version of the best write * / - (BOOL) application: (UIApplication * application) handleOpenURL: (NSURL *) URL [FLPAYMANAGER {return fl_handleUrl:url];} / * * * @author gitKong * * iOS 9 before the call - * / (BOOL) application: (UIApplication * application * NSURL (openURL:) URL) sourceApplication: (NSString *) sourceApplication annotation: (ID) annotation [FLPAYMANAGER {return fl_handleUrl:url];} / * * * @author * gitKong * iOS 9 (including iOS9.0) * / - (BOOL) application: (UIApplication *) application openURL: (nonnull NSURL *) URL options: (nonnull NSDictionary< NSString id> options{*, return *) [FLPAYMANAGER fl_handleUrl:url];}

2, registered in the app didFinishLaunchingWithOptions, the internal binding in accordance with the corresponding Url Info Types URL Scheme

- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary *) launchOptions Override point for customization after {/ / application / / launch. app [FLPAYMANAGER fl_registerApp] return YES registration;;}

3, initiated payment

  • Alipay to pay
NSString *orderMessage = "@ Demo in order to test the information"; [FLPAYMANAGER fl_payWithOrderMessage:orderMessage callBack:^ (FLErrCode errCode, NSString *errStr) {NSLog (@ errCode =%zd, errStr = errCode, errStr% @ ",}]);
  • WeChat payment
WeChat PayReq* req / / transferred payment = [[PayReq alloc] init]; req.partnerId [dict objectForKey:@ = "partnerid"]; req.prepayId [dict objectForKey:@ = "prepayid"]; req.nonceStr [dict objectForKey:@ = "noncestr"]; req.timeStamp = stamp.intValue; req.package = [dict objectForKey:@ req.sign = "package"; [dict objectForKey:@ "sign"]; [FLPAYMANAGER fl_payWithOrderMessage:req callBack:^ (FLErrCode errCode, NSString *errStr) {NSLog (@ errCode =%zd, errStr = errCode, errStr% @ ",}]);

Six, the advantages of this tool

  • 1, isolation framework, unified management, easy maintenance
  • 2, for the payment function to package a set of API, the use of simple, readable
  • The advantages of Alipay and WeChat, 3 fusion interface, such as WeChat perfect return status code corresponding to the status information
  • 4, Alipay and WeChat on the callback processing are unified into a callback block
  • 5, the tool to add a more complete assertion WeChat and Alipay to pay - look at me, I will be enough
    relatively perfect assertion, to avoid unnecessary errors

Seven, pay attention to points:

  • 1, Info.plist Url Types Identifier configuration must be corresponding guarantee and tools, the default Identifier of WeChat Weixin, Alipay Identifier zhifubao, can modify the gitKong * * * @author / * * here must be guaranteed in the Info.plist URL Types Identifier #define FLWECHATURLNAME to a corresponding “Weixin” * @ #define FLALIPAYURLNAME @ zhifubao”
  • 2, because the tool to add a relatively complete assertion, incomplete configuration or transmission is not correct, the program will inevitably collapse
  • 3, because the tools are coupled Alipay SDK and WeChat SDK, if the project only need to use a single payment, this does not apply, of course, also have independent:
    Alipay WAP and Alipay wallet payment unified WeChat paid -block callback callback

Eight, summary

  • 1, the internal implementation of the code are relatively simple, here do not make a detailed analysis of the Demo are compared with the notes, gitHub address in this, to give a star support ~!
  • 2, packaging ideas and analysis have been described in detail, if you have any doubts or new ideas can leave a message to me, I will reply!
  • 3, we welcome the attention I love to like, playing tour will be unashamed to accept, I will update the original dry cargo.