Preface Due to project requirements some time ago, the WeChat payment SDK and Alipay payment SDK were removed from the project. In this case, you need to handle the payment yourself. The first thing you considered is to use openshare, but after downloading openshare, you find that openshare's payment interface cannot directly replace the official SDK payment interface. The implementation logic of the official SDK is that after the backend order signature is completed, the client transmits the signature information and parameters to the payment SDK, the payment SDK generates the protocol URL, and then pulls up the third-party payment APP. The payment interface of openshare directly transmits the protocol URL address for pulling up the payment, and the operation of generating the protocol URL address is also handed over to the backend to generate. In this case, the background code needs to be modified. Is it possible to create a payment interface to directly replace the official SDK payment interface without modifying the background code to achieve seamless connection? So I studied the communication relationship between WeChat and Alipay APPs during payment, and finally encapsulated it into XHPayKit. WeChat, Alipay payment
characteristic: - XHPayKit has a similar interface to the official SDK and can directly replace the official SDK payment interface. If you have used the official SDK, it only takes a very short time to convert to this library.
- XHPayKit is only 10kb in size. It can realize WeChat payment and Alipay payment without importing any dependent libraries. If you want to slim down your project or for some reason, you don't want to use the official SDK to realize the payment function, this library will be a good choice.
- When using XHPayKit, there is no need to configure the appid and other information of WeChat and other platforms. The server configuration is sufficient, because the appid and other information will be returned to the client when the backend signs the order.
Notice: - First register your application on WeChat and Alipay open platforms and obtain payment capabilities
- Import this library and add the weixin and alipay fields to the info.plist whitelist
- Add your own APP URL Schemes and WeChat callback URL Schemes, see the README document for details
Directions: 1. WeChat Pay - // WeChat payment parameters, the following 7 parameters are generated by the backend after signing the order and returned to the customer service end (consistent with the official SDK)
- //Note: Please set the following parameters to the parameters returned by the server after signing your real order, and then you can make the actual payment
- XHPayWxReq *req = [[XHPayWxReq alloc] init];
- req.openID = @ "" ;
- req.partnerId = @ "" ;
- req.prepayId = @ "" ;
- req.nonceStr = @ "" ;
- req.timeStamp = 1518156229;
- req.package = @ "" ;
- req.sign = @ "" ;
-
- //Input the order model and start WeChat payment
- [[XHPayKit defaultManager] wxpayOrder:req completed:^(NSDictionary *resultDict) {
- NSLog(@ "Payment result:\n%@" , resultDict);
- NSInteger code = [resultDict[@ "errCode" ] integerValue];
- if(code == 0){//Payment successful
-
- }
- }];
2. Alipay payment - //Alipay order signature, this signature is generated by the backend after signing the order and returned to the client (consistent with the official SDK)
- //Note: Please set the value below to your real order signature to make actual payment
- NSString *orderSign = @ "A very long Alipay order signature" ;
-
- // Pass in the Alipay order signature and your own App URL Scheme to launch Alipay payment
- [[XHPayKit defaultManager] alipayOrder:orderSign fromScheme:@ "XHPayKitExample" completed:^(NSDictionary *resultDict) {
- NSLog(@ "Payment result:\n%@" , resultDict);
- NSInteger status = [resultDict[@ "ResultStatus" ] integerValue];
- if(status == 9000){//Payment successful
-
- }
- }];
3. Add the following code in Appdelegate - Process the payment result URL carried by the third-party payment jump back to the merchant app - #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
- /** iOS9 and later */
- - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<uiapplicationopenurloptionskey, id> *)options
- {
- BOOL result = [[XHPyKit defaultManager] handleOpenURL:url];
- if (!result) {//Here handle other SDKs (such as QQ login, Weibo login, etc.)
-
- }
- return result;
- }
- #endif
- /** iOS9 and below */
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- BOOL result = [[XHPyKit defaultManager] handleOpenURL:url];
- if (!result) {//Here handle other SDKs (such as QQ login, Weibo login, etc.)
-
- }
- return result;
- }</uiapplicationopenurloptionskey, id>
Other interfaces: - /**
- Is WeChat installed?
- @ return YES if installed, NO if not installed
- */
- +(BOOL)isWxAppInstalled;
- /**
- Is Alipay installed?
- @ return YES if installed, NO if not installed
- */
- +(BOOL)isAliAppInstalled;
Payment result resultDict interpretation: WeChat - {
- "errCode" :0,
- "errStr" : "Success"
- }
- //The following status codes have the same meaning as the official SDK
- errCode = 0, success
- errCode = -1, common error type
- errCode = -2, the user clicks cancel and returns
- errCode = -3, sending failed
- errCode = -4, authorization failed
- errCode = -5, WeChat is not supported
Alipay - {
- "result" : "" ,
- "resultStatus" : "9000" ,
- "memo" : "Payment successful"
- }
- //The following status codes have the same meaning as the official SDK
- resultStatus = 9000, payment successful
- resultStatus = 8000, processing, payment result unknown (may have been paid successfully), please check the payment status of the order in the merchant order list
- resultStatus = 4000, payment failed
- resultStatus = 5000, repeat request
- resultStatus = 6001, user canceled the request
- resultStatus = 6002, network connection error
- resultStatus = 6004, the payment result is unknown (it may have been paid successfully). Please check the payment status of the order in the merchant order list.
summary: The implementation of XHPayKit is very simple. Interested students can download it and study the communication between apps during payment. Code address: https://github.com/CoderZhuXH/XHPayKit |