iOS does not need the official SDK to implement WeChat and Alipay payment XHPayKit

iOS does not need the official SDK to implement WeChat and Alipay payment XHPayKit

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:

  1. 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.
  2. 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.
  3. 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:

  1. First register your application on WeChat and Alipay open platforms and obtain payment capabilities
  2. Import this library and add the weixin and alipay fields to the info.plist whitelist
  3. Add your own APP URL Schemes and WeChat callback URL Schemes, see the README document for details

Directions:

1. WeChat Pay

  1. // 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)
  2. //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
  3. XHPayWxReq *req = [[XHPayWxReq alloc] init];
  4. req.openID = @ "" ;
  5. req.partnerId = @ "" ;
  6. req.prepayId = @ "" ;
  7. req.nonceStr = @ "" ;
  8. req.timeStamp = 1518156229;
  9. req.package = @ "" ;
  10. req.sign = @ "" ;
  11.           
  12. //Input the order model and start WeChat payment
  13. [[XHPayKit defaultManager] wxpayOrder:req completed:^(NSDictionary *resultDict) {
  14. NSLog(@ "Payment result:\n%@" , resultDict);
  15. NSInteger code = [resultDict[@ "errCode" ] integerValue];
  16. if(code == 0){//Payment successful
  17.                   
  18. }
  19. }];

2. Alipay payment

  1. //Alipay order signature, this signature is generated by the backend after signing the order and returned to the client (consistent with the official SDK)
  2. //Note: Please set the value below to your real order signature to make actual payment
  3. NSString *orderSign = @ "A very long Alipay order signature" ;
  4.           
  5. // Pass in the Alipay order signature and your own App URL Scheme to launch Alipay payment
  6. [[XHPayKit defaultManager] alipayOrder:orderSign fromScheme:@ "XHPayKitExample" completed:^(NSDictionary *resultDict) {
  7. NSLog(@ "Payment result:\n%@" , resultDict);
  8. NSInteger status = [resultDict[@ "ResultStatus" ] integerValue];
  9. if(status == 9000){//Payment successful
  10.                   
  11. }
  12. }];

3. Add the following code in Appdelegate - Process the payment result URL carried by the third-party payment jump back to the merchant app

  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
  2. /** iOS9 and later */
  3. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<uiapplicationopenurloptionskey, id> *)options
  4. {
  5. BOOL result = [[XHPyKit defaultManager] handleOpenURL:url];
  6. if (!result) {//Here handle other SDKs (such as QQ login, Weibo login, etc.)
  7.           
  8. }
  9. return result;
  10. }
  11. #endif
  12. /** iOS9 and below */
  13. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  14. {
  15. BOOL result = [[XHPyKit defaultManager] handleOpenURL:url];
  16. if (!result) {//Here handle other SDKs (such as QQ login, Weibo login, etc.)
  17.           
  18. }
  19. return result;
  20. }</uiapplicationopenurloptionskey, id>

Other interfaces:

  1. /**
  2. Is WeChat installed?
  3. @ return YES if installed, NO if not installed  
  4. */
  5. +(BOOL)isWxAppInstalled;
  6. /**
  7. Is Alipay installed?
  8. @ return YES if installed, NO if not installed  
  9. */
  10. +(BOOL)isAliAppInstalled;

Payment result resultDict interpretation:

WeChat

  1. {
  2. "errCode" :0,
  3. "errStr" : "Success"  
  4. }
  5. //The following status codes have the same meaning as the official SDK
  6. errCode = 0, success
  7. errCode = -1, common error type
  8. errCode = -2, the user clicks cancel and returns
  9. errCode = -3, sending failed
  10. errCode = -4, authorization failed
  11. errCode = -5, WeChat is not supported

Alipay

  1. {
  2. "result" : "" ,
  3. "resultStatus" : "9000" ,
  4. "memo" : "Payment successful"  
  5. }
  6. //The following status codes have the same meaning as the official SDK
  7. resultStatus = 9000, payment successful
  8. resultStatus = 8000, processing, payment result unknown (may have been paid successfully), please check the payment status of the order in the merchant order list
  9. resultStatus = 4000, payment failed
  10. resultStatus = 5000, repeat request
  11. resultStatus = 6001, user canceled the request
  12. resultStatus = 6002, network connection error
  13. 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

<<:  What is Fuchsia, the successor to Android?

>>:  These unpopular iOS apps unlock new ways to use iPhone

Recommend

Do you know all the tricks about creative design of information flow?

The core of information flow is material. As info...

8 copywriting improvement methods to help you quickly improve user experience

The interface copy mentioned here mainly refers t...

Gartner predicts 10 strategic technology trends for 2016

[[152040]] Gartner recently presented the results...

How much does it cost to develop an appointment registration app in Shaoyang?

WeChat Mini Program is an application that users ...

Detailed experience of the latest version of WeChat: tap and feel comfortable

WeChat 7.0.18 is here. This update brings a featu...

Castration, sex change... "drug girl" trapped in a man's body

In this world, besides men and women, there is al...

How is the development of ultra-high-definition broadcasting abroad?

The noisy 2014 has finally passed. In this year, ...

HTC: Why did the "pig" that was once on the cusp of the trend fail to fly?

In response to rumors that HTC will be acquired n...