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.

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

Before making payment, I also searched online information. Most of them said that there were too many payment access pits, most wechat pits and too complicated UnionPay documents. In fact, if access more words, those routines can go around. Most of the experience on the Internet is relatively old, relatively new can be used to the article is not much (but good articles or some, I help a lot), the following to give you a detailed introduction, to help you bypass the pit.

2. Interactive process

It is recommended to take a look at the development document carefully first, be sure to see, at the beginning of the time there is no honest to finish, the results encountered a lot of pits, waste a lot of time, so it is recommended to take a good look, especially this part of the interactive process.

2.1 Function Flow

2.2 Data Interaction

Compared with wechat Pay, the functional process of Alipay payment is very simple. As shown in the above two pictures, our App (i.e. merchant client) only has three steps:

  • To generate orders
  • Call the Alipay interface and send the order
  • Return the order payment result and process it

3. Download the Alipay SDK

Alipay business is numerous, really want to find the desired payment SDK or to take some time, here gives the latest SDK address note is downloaded out of the SDK package there is no legendary development documents, need to find other places or look at the web page.

The use of public key, private key, PID, sellerID, and key and how to obtain them are all described in detail in the documentation, which will not be described here. You must clearly define the concepts before you do them, otherwise you will get confused later. We can discuss it together if we run into problems.

4. Import the library and integrate the SDK

4.1 Import files and key libraries

Import through CocoaPods

pod  'AlipaySDK-iOS' 
Copy the code

Manual import

Official Demo Screenshot

Add framework and other files to open iOS project, new SDK file Order file, you need to copy and paste into your project:

  • AlipaySDK.framework
  • AlipaySDK.bundle
  • Until the folder
  • Openssl folder
  • Libcrypto. A and libssl. A

Some of the files above are not visible in the open project and need to be found by opening the project folder.

Add the following dependencies to the Link Binary With Libraries in the Build Phases TAB:

Among them, attention should be paid to:

  • For Xcode versions later than 7.0, add libc++. TBD and libz.tbd.
  • For versions prior to Xcode 7.0, you need to add libc++. Dylib, libz.dylib (as shown below).

At this point, if you start the project, chances are you’ll find an error.

4.2 Importing header Files

Add a header reference to the file where AlipaySDK needs to be called.

import <AlipaySDK/AlipaySDK.h>
Copy the code

4.3 Invoking the Payment Interface

In the access document of Alipay, the step of generating an order is put on the client side to do, but this is best put on the server side to do.

Background generation order and then splicing, signature, and then the server side directly to the client side of an encrypted signature of the parameters can be, so it is safer.

All order information, merchant information and so on are in their own hands, so that the APP side is not afraid of being intercepted data, and it is particularly simple to call, just need to call the interface of payment.

If all you need to do is send the order and process the payment return result, you just need to add alipaysdK. bundle and AlipaysdK. framework, which are easy to find in the downloaded SDK. The quick payment method is this:

-(void)payOrder:(NSString *)orderStr fromScheme:(NSString *)schemeStr callback:(CompletionBlock)completionBlock;
Copy the code

In the button of payment, use alipay this class, and then call this method! Such as:

// NOTE: PayOrder :orderString fromScheme:@"FBYAlipayDemo" callback:^(NSDictionary *resultDic) { NSLog(@"reslut = %@",resultDic); }];Copy the code

4.4 Configuring the processing method of url Returned by alipay Client

As in the example AliSDKDemo\ apappdelegate. m file, add the reference code:

import <AlipaySDK/AlipaySDK.h>
Copy the code

Change the NSLog in the *@*implementation AppDelegate code to the actual business processing code:

- (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); }]; } return YES; }Copy the code

4.5 Project Configuration

Finally, don’t forget to also write a URL Scheme, the last one can be found under Targets -> Info, click the “Info” TAB, in the “URL Types” option, click “+”.

5. Frequently Asked Questions

5.1 Unable to return to App after payment

Generally, the whitelist is not set correctly

// NOTE: PayOrder :orderString fromScheme:@"FBYAlipayDemo" callback:^(NSDictionary *resultDic) { NSLog(@"reslut = %@",resultDic); }];Copy the code

The appScheme parameter in the above code needs to be the same as URL Schemes set in article 4.5 project Configuration in order to return App.

5.2 #include <openssl/opensslconf.h> not find

This is an amazing pit. I googled it for a long time and couldn’t figure it out. Then I remembered the difference between #import “” and #import <>.

Targets -> Header Search Paths under Build Settings Add the following directory “$(SRCROOT)/ project name/absolute address of file “as shown:

5.3 ‘openssl/ ASn1.h ‘file not found

Header Search Paths: $(PROJECT_DIR)/ project name/openSSL

5.4 Undefined symbol: OBJC_METACLASS$_WKWebView

If the above problem occurs, you need to add the system class library: WebKit.framework to the project configuration

5.5 If an error Occurs after the system runs, the following information is displayed:

Cannot find interface declaration for 'NSObject', superclass of 'Base64'
Copy the code

You need to open the error file and add the header file.

# import <Foundation/Foundation.h>
Copy the code

5.6 Problems During SDK Access to Swift

If the project uses Swift as the development language, you need to add Bridging files, such as bridge-header.h

Also, set the location of the bridge file in the project Build Settings.

If the following error occurs at runtime, in the bridge file, say #import <UIKit/ uikit.h >

5.7 Click Pay to jump to the Launch page of alipay card

This situation is stuck in the page will appear the corresponding error message, generally there are the following situations:

  • Product information concatenation string error
  • Payment account expired and waiting for renewal status
  • Incorrect merchant ID
  • Failed to check the order information

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

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