A:

The payment function needs to be used in the project, including Alipay payment, Alipay web payment transfer to the client, wechat Pay, UnionPay and Apple_pay. Therefore, I intend to summarize this, which is convenient for future reference and convenience for everyone, and avoid being deceived again where used. Today we will mainly introduce wechat payment, other payments have also written corresponding tutorials, and give links.

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).

Two: 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.

Three: 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

Four: Import library integration 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.2 Registering an APPID in the Appdelegate

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

// Wechat Pay#import "WXApi.h"
Copy the code

Then register the APPID

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register with wechat terminal ID [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"] {// Jump to alipay wallet for payment, Handle payment results [[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback: ^ (NSDictionary * resultDic) { NSLog(@"result = %@",resultDic);
        }];
    }else if ([url.host isEqualToString:@"pay"[WXApi handleOpenURL:url delegate:self]; }returnYES; } // NOTE: - (BOOL)application (UIApplication *)app openURL (NSURL *)url options:(NSDictionary<NSString*, id> *)options {if ([url.host isEqualToString:@"safepay"] {// Jump to alipay wallet for payment, 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 {// Start wechat pay response NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    if([resp isKindOfClass:[PayResp class]]){// The payment result is returned. The actual payment result needs to be checked on the wechat server.case 0:
                payResoult = @"Payment result: success!";
                break;
            case -1:
                payResoult = @"Payment result: failure!";
                break;
            case -2:
                payResoult = @"User has exited payment!";
                break;
            default:
                payResoult = [NSString stringWithFormat:@"Payment result: failure! retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                break; }}}Copy the code

4.3 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:

#pragma mark wechat payment method- (void)WechatPay{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.4 Check 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.

// Check whether the phone has wechatif ([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.

Follow-up: FAQ resolved

  1. If the payment has been left in wechat after completion, check the Scheme setting problem in URLType.
  2. 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.
  3. The unit of wechat pay is minutes
  4. Project -> Build setttings -> search other Linker flags and add -objc-all_load. Running project may crash because the SDK library is not found.
  5. Whitelist: The payment can be initiated without the whitelist configuration. If the payment cannot be adjusted, check whether the problem is whitelist. . 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
  1. #import

    in wxapiObject.h.

Pay attention to [net development] wechat public number, net the world method, convenient for you and ME to develop, more iOS technology dry goods waiting to receive, all documents will continue to update, welcome to pay attention to grow together!

I hope I can help you. If there is anything wrong or inadequate, I hope you can give me some suggestions