preface
Some time ago, due to the requirements of the project, we removed the wechat Payment SDK and Alipay payment SDK from the project. In this case, we need to handle the payment by ourselves. We considered using OpenShare at first, but after downloading OpenShare, we found that the payment interface of OpenShare cannot directly replace the official SDK payment interface The implementation logic of the official SDK is that after the background order signature is completed, the customer service end sends 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. However, the payment interface of OpenShare directly transmits the protocol URL address of payment, and the operation of generating protocol URL address is also entrusted to the background. In this case, the background needs to modify the code. Is it possible to create a payment interface to directly replace the official SDK payment interface without modifying the background code, so as to achieve seamless docking? Therefore, I studied the communication relationship between wechat and Alipay payment apps, and finally packaged into XHPayKit.
Features:
1.XHPayKit has a similar interface to the official SDK, which can directly replace the official SDK payment interface. If you have used the official SDK, it only takes you a very short time to convert to this library. 2.XHPayKit is only 10KB in size and can be used for wechat pay and Alipay payment without importing any dependent libraries. If you want to slim down your project or for some reason don’t want to use the official SDK to implement payment functions, this library is a good choice. 3. When XHPayKit is used, there is no need to configure the appID and other information of wechat and other platforms, but the server can be configured, because the background will return the appID and other information to the client when signing the order.
Note:
1. Register your app on the open platforms of wechat and Alipay to gain the ability to pay 2. Import this library and please add weixin, Alipay fields to info.plist whitelist 3. Add APP URL Schemes and wechat callback URL Schemes, please see the README documentation
Usage:
1. Wechat Pay
// wechat payment parameters, the following 7 parameters are generated by the background after signing the order and returned to the customer service (consistent with the official SDK)
// Note: Please set the following parameters as your own real order signature server return parameters, 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 = @ "";
// Pass in the order model and pull up wechat Pay
[[XHPayKit defaultManager] wxpayOrder:req completed:^(NSDictionary *resultDict) {
NSLog(Payment result :\n%@",resultDict);
NSInteger code = [resultDict[@"errCode"] integerValue];
if(code == 0) {// The payment was successful}}];Copy the code
2. Alipay payment
// Alipay order signature, which is generated by the background after signing the order and returned to the client (consistent with the official SDK)
// Note: Please set the face value as your own real order signature, then you can make the actual payment
NSString *orderSign = @" a long string of Alipay order signatures";
// Enter the alipay order signature and your App URL Scheme to activate alipay payment
[[XHPayKit defaultManager] alipayOrder:orderSign fromScheme:@"XHPayKitExample" completed:^(NSDictionary *resultDict) {
NSLog(Payment result :\n%@",resultDict);
NSInteger status = [resultDict[@"ResultStatus"] integerValue];
if(status == 9000) {// The payment was successful}}];Copy the code
3. Add the following code to the Appdelegate to handle third-party payment hopping back to the payment result Url carried by 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 = [[XHPayKit defaultManager] handleOpenURL:url];
if(! result) {// Other SDKS are handled here (e.g. QQ login, Weibo login, etc.)
}
return result;
}
#endif
/** iOS9 below */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[XHPayKit defaultManager] handleOpenURL:url];
if(! result) {// Other SDKS are handled here (e.g. QQ login, Weibo login, etc.)
}
return result;
}
Copy the code
Other interfaces:
/** Whether wechat @return is installed YES, NO */+ (BOOL)isWxAppInstalled;
/** Whether alipay @return is installed YES, NO */+ (BOOL)isAliAppInstalled;
Copy the code
Payment result resultDict
{
"errCode":0."errStr":"Success"
}
// The following status codes are consistent with the official SDK
errCode = 0, success errCode =- 1, common error type errCode =2 -, the user clicks cancel and returns errCode =- 3ErrCode =4 -, authorization fails errCode =- 5, wechat does not supportCopy the code
Alipay
{
"result":""."resultStatus":"9000"."memo":"Payment successful"
}
// The following status codes are consistent with the official SDK
resultStatus = 9000ResultStatus =8000, is being processed, the payment result is unknown (payment may have been successful), please check the payment status of the order in the merchant order list resultStatus =4000ResultStatus =5000, repeat request resultStatus =6001, the user cancels the resultStatus =6002, network connection error resultStatus =6004, the payment result is unknown (payment may have been successful), please check the payment status of the order in the merchant's order listCopy the code
Summary:
The implementation of XHPayKit is very simple, and interested students can download it to study the communication between apps during payment. Code address :github.com/CoderZhuXH/…