1. Introduction

This article is a summary of the previous project. Due to the related payment SDK iteration, the original text does not meet the requirements, so it is updated as follows for your reference. In addition, a summary of common problems is added.

The payment function will be used in the project, including Alipay payment, wechat payment, UnionPay payment and Apple_pay. Therefore, I intend to summarize this, which is convenient for future reference and convenience for everyone, and avoid being cheated again where used.

Today we will mainly introduce wechat payment, other payment introduction will be updated as soon as possible.

Before integration, we should first look at the document. There are detailed fields and instructions in the wechat Payment development document. Wechat Pay requires signature. Like Alipay, it can be signed on the client or in the background (of course, it is recommended to sign on the server for security, and the logic is easy to understand).

Plus wechat: fby-Fan lead source code

2. Business processes

The following is the interaction sequence diagram. The unified ordering API, payment result notification API and order query API all involve the signature process, which must be completed on the merchant server.

Main interaction between merchant system and wechat Payment system:

    1. Users select goods in the merchant APP, submit an order and choose wechat Pay.
    1. The merchant background receives the user’s payment order and invokes the unified ordering interface of wechat Pay.
    1. The unified ordering interface returns the normal prepay_ID, and then generates the signature according to the signature specification, and transmits the data to APP. The participating fields are named appID, partnerId, PrepayID, NonCESTr, TIMESTAMP, Package.
    1. The merchant APP has switched to wechat Pay.
    1. Merchant background receives payment notification.
    1. Merchant background query payment results.

3. Download the wechat SDK

If you integrate wechat in Umeng Sharing, there is no need to download or configure the environment, because the configuration of Umeng sharing has already configured the wechat payment environment (including framework, schema jump, white list). If you have not integrated Umeng sharing, please download the SDK on wechat open platform.

It is recommended to download both the iOS header file and the payment sample

4. Import the library and integrate the SDK

4.1 Importing the SDK library

To do this, go to Target -> BuildPhases -> Link Binary With Libraries – click the + symbol -> search for the system Libraries you need.

  • SystemConfiguration.framework
  • libz.tbd
  • Libsqlite3.0. TBD
  • CoreTelephony.framework
  • QuartzCore.framework

4.2 Setting the URL Scheme

After merchants apply for APP development on wechat open platform, wechat open platform will generate the unique identification of APP APPID, which is clearly stated in APP development steps and should be filled in URL Schemes.

4.3 Registering an APPID in the Appdelegate

The first step is to reference the header file in the Appdelegate

#import "wxapi.h"Copy the code

Then register the APPID

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register ID with wechat terminal [WXApi registerApp:@" wxd930ea5D5A258F4f "]; return YES; }Copy the code

After the payment is successful, the payment result is returned, and the url needs to be obtained, which also needs to be done in the Appdelegate. The code is as follows:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication Annotation :(id)annotation {if ([url.host isEqualToString:@"safepay"]) { Handle payment results [[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback: ^ (NSDictionary * resultDic) { NSLog(@"result = %@",resultDic); }]; }else if ([url.host isEqualToString:@"pay"]) {WXApi handleOpenURL:url delegate:self]; } return YES; } // NOTE: - (BOOL)application (UIApplication *)app openURL (NSURL *)url options:(NSDictionary<NSString*, Id > *)options {if ([url.host isEqualToString:@"safepay"]) { Handle payment results [[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback: ^ (NSDictionary * resultDic) { NSLog(@"result = %@",resultDic); }]; }else if ([url.host isEqualToString:@"pay"]) {WXApi handleOpenURL:url delegate:self]; } return YES; }Copy the code

Wechat SDK built-in method, processing from the wechat client to complete the operation after returning to the program callback method, display the payment result:

-(void) onResp:(BaseResp*)resp {// Response NSString *payResoult = [NSString stringWithFormat:@" errCode :%d", resp.errCode]; If ([resp isKindOfClass:[PayResp class]]){if([resp isKindOfClass:[PayResp class]]){if([resp isKindOfClass:[PayResp class]]){ PayResoult = @" Payment result: success!" ; break; Case-1: payResoult = @" Payment result: failed!" ; break; Case-2: payResoult = @" User has exited payment!" ; break; Default: payResoult = [NSString stringWithFormat:@" Payment result: failed! Retcode = %d, retstr = %@", resp.errcode, resp.errstr]; break; }}}Copy the code

4.4 Invoking the Payment Interface

In the call wechat pay class, first add the header file reference.

#import "WXApi.h"
Copy the code

In the method of calling up payment, the parameters to be uploaded include appid, partid (merchant number), prepayid (pre-payment order ID), noncestr (random character string of participating signature), timestamp (timestamp of participating signature) and sign (signature character string). Click to pay in the controller using the core code to tune up the wechat client payment, these parameters are background to you. It’s annotated. It should make a lot of sense. The code is as follows:

PayReq *req = [[PayReq alloc] init]; OpenID = AppID; // A unique identifier consisting of the user's wechat id and AppID, used to verify wechat user req.openID = AppID; PartnerId = partnerId; // Merchant ID, given at registration time; Req.prepayid = prepayId; req.prepayId = prepayId; req.prepayId = prepayId; Req.package = package; req.package = package; Req.noncestr = nonceStr; NSString * stamp = timestamp; NSString * stamp = timestamp; req.timeStamp = stamp.intValue; Req. sign = sign; // Send a request to wechat and wait for wechat to return onResp [WXApi sendReq:req]; }Copy the code

4.5 Checking whether the mobile phone is installed with the wechat client

After calling the encapsulated class method in the place where wechat payment is needed, it will jump to wechat APP. If it is not installed, there will be no response. Note here that since wechat is not installed, you must provide webview to log in to wechat to pay, otherwise Apple will reject the application. But wechat does not come with webview, (Alipay is brought with it) so it is necessary to judge whether the user has installed wechat, if not installed wechat will not display the wechat pay button.

If ([WXApi isWXAppInstalled]) {wechatButton.hidden = NO; }else{ wechatButton.hidden = YES; }Copy the code

Here wechat payment is basically completed, if you encounter any problems in the integration process, you can leave a message to me or add QQ to help you solve online.

5. Solve common problems

5.1 Unable to return to App after payment

If the payment has been left in wechat after completion, check the Scheme setting problem in URLType.

5.2 Parameter Expiration

You can open the wechat client, but only a white “OK button” in the middle will return to the client, if this is the case, it should be prepayID parameter problem, expired, or not a real ID.

5.3 The unit of wechat Payment is minutes

5.4 Program did not find SDK library

Project -> Build setttings -> search other Linker flags and add -objc-all_load. Running project may crash because the SDK library is not found.

5.5 white list

If the payment cannot be adjusted, check whether the payment is whitelisted.

. Open the info in the project file to add LSApplicationQueriesSchemes array and add wechat and weixin string.

Or right-click info.plist -> Source Code to open and add the following code

<key>LSApplicationQueriesSchemes</key>
<array><string>wechat</string>
  <string>weixin </string>
</array>
Copy the code

5.6 Project error of Chinese name reported

#import <UIKit/ uikit.h > in wxapiObject.h.

5.7 No Corresponding Compilation Package Is Found

Wechat SDK problem, can not find the corresponding compilation package, import the libc++ to solve the problem

I hope I can help you if there is anything wrong or inadequate, but also hope the reader more comments or suggestions iOS technology exchange group: 668562416

Source code Demo access method

Source code: pay attention to “net development” reply “Alipay payment” can be obtained

reprinted

This article has been published in the public account “Net development”, if reprinted changbai, please add wechat: fby-fan, remarks reprinted Changbai

welfare

11 categories of interview questions, including: Multithreading, memory management, design patterns, data security and encryption, data results and Algorithms, networks, performance optimization, Block, RunLoop, Runtime, AND UI