IOS development-WeChat payment, ios development --

Source: Internet
Author: User

IOS development-payment, ios development --

The following describes the details of the payment development process. You also learned to pay for the package. Payment is also a frequently asked question during the interview.

    

 

Body:

 

1. Before you start using the payment method, you must know something about it. Open the following link:

Https://pay.weixin.qq.com/wiki/doc/api/app.php? Chapter = 3_1

Then we can see the following page. This is the Development document of the payment merchant platform. Many things can be viewed and understood. When developing and using the SDK payment function, if you encounter any problems, you can find the relevant instructions here:

    

Then, you can also tell the reader to click "payment account> payment account" in this development document, and then scroll to the bottom of the current page to see about APPID:

    

Note: This APPID is required for payment during development. This APPID is only used by merchants who have registered on the payment platform. This payment platform costs 300 yuan and fills in a lot of important information, you must upload the necessary procedures such as the Business License to obtain the APPID.

The commercial app finds the merchant based on the unique APPID when the customer uses the app for consumption, and then transfers the consumer's amount to the merchant's account.

  

Benefits for developers: for developers, the payment platform provides a test Demo and an APPID for testing code in the Demo source code. In this way, developers need to spend 300 yuan to buy an APPID.

2. To help readers learn how to use this SDK more conveniently and in a more targeted manner, I will build a common project, directly use the SDK on top, and complete the payment.

Https://pay.weixin.qq.com/wiki/doc/api/app.php? Chapter = 11_1

Click here to download the SDK. In addition, you need to download the Demo. You can refer to the learning source code and save it for further exploration:

    

In the new project, we drag the downloaded sdks. The downloaded sdks contain five files. The read_me.txt file does not need to be saved in the project. If you like it, you can open and read the prompt information:

    

Open the read_me.txt file first. In fact, it refers to the problems solved in the latest versions and the precautions for using the SDK. I will use the red boxes in subsequent operations, SO this read_me file is very important.

    

3rd, we should follow read_me.txt to do the necessary process:

After Xcode 7, you need to import the framework and Link Library:

    

If it is before XCode 7, it is estimated that you need to manually import the Foundation. framework, UIKit. framework, and other frameworks.

Follow the instructions in read_me.txt to copy the plist code to the info. plist file:

1 <key>LSApplicationQueriesSchemes</key>2 <array>3 <string>weixin</string>4 </array>5 <key>NSAppTransportSecurity</key>6 <dict>7 <key>NSAllowsArbitraryLoads</key>8 <true/>9 </dict>

Switch the info. plist file to the Property list display view, and you will see two more items:

    

App Transport Security Settings are manually added in development after XCode7, because iOS9 restricts http access by default.

LSApplicationQueriesSchemes is a whitelist of URL Schemes that can be used, so that the current application can use the relevant capabilities (SHARE, add to favorites, pay, login, etc ).

Another operation is to set the APPID used for payment to URL Schemes [ski sans m].

  

4. Well, we can start coding:

We can open the Demo program downloaded from the payment platform, and find the APPID used for testing in the source code of its AppDelegate:

  

Go back to the project you created and write down the payment process:

  

Now that we want to register the SDK, we can check it in the SDK header file and find that only two registration methods are provided, and the annotations are clearly written:

  

After importing this header file, we can directly register it based on the existing APPID:

  

Step 1: import the payment SDK and register for payment. 2. Set APPID to URL Schemes.

Then we need to make 3. Initiate the payment and make the payment. Before that, let's take a look at the Demo officially provided to us:

  

  

Finally, we found the complete source code that can be directly used in the Demo to initiate the payment:

  

Copy this section directly to my project. Some experienced developers will notice that, for example, the Demo source code uses MRC's autorelease, which can be removed manually, you can replace the class method with the instance method based on your actual project development needs:

1-(NSString *) jumpToBizPay {2 3 // ====================================== =======================================4 // V3 & V4 payment process Implementation 5 // note: for parameter configuration, check the server Demo 6 // Update Time: january ======================================= 8 NSString * urlString = @ "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php? Plat = ios "; 9 // Resolution Server Returns json data 10 NSError * error; 11 // load an NSURL object 12 NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]; 13 // put the requested url data in the NSData object. 14 NSData * response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 15 if (response! = Nil) {16 NSMutableDictionary * dict = NULL; 17 // the built-in parsing class NSJSONSerialization of IOS5 parses the data from response and puts it into the dictionary 18 dict = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableLeaves error: & error]; 19 20 NSLog (@ "url: % @", urlString); 21 if (dict! = Nil) {22 NSMutableString * retcode = [dict objectForKey: @ "retcode"]; 23 if (retcode. intValue = 0) {24 NSMutableString * stamp = [dict objectForKey: @ "timestamp"]; 25 26 // start payment 27 PayReq * req = [[PayReq alloc] init]; 28 req. partnerId = [dict objectForKey: @ "partnerid"]; 29 req. prepayId = [dict objectForKey: @ "prepayid"]; 30 req. nonceStr = [dict objectForKey: @ "noncestr"]; 31 req. timeStamp = stamp. intValue; 32 req. package = [dict objectForKey: @ "package"]; 33 req. sign = [dict objectForKey: @ "sign"]; 34 [WXApi sendReq: req]; 35 // log output 36 NSLog (@ "appid =%@\ npartid =%@\ nprepayid =%@\ nnoncestr =%@\ ntimestamp = % ld \ npackage =% @\ nsign = % @", [dict objectForKey: @ "appid"], req. partnerId, req. prepayId, req. nonceStr, (long) req. timeStamp, req. package, req. sign); 37 return @ ""; 38} else {39 return [dict objectForKey: @ "retmsg"]; 40} 41} else {42 return @ "server return error, no json object retrieved "; 43} 44} else {45 return @" server return error "; 46} 47}

Oh, by the way, there is another simple but necessary operation that we forgot to display:

  

Further, we can find two useful methods in the SDK source code header file. You can also go to the developer documentation on the payment platform to find the introduction of these two methods:

  

Then I applied it to my project.

  

Okay. In this case, step 3. Initiate the payment and the call is completed here.

The final thing to do is to process the returned payment information and use the "OK" function. Whether the payment is successful or failed, or even the user cancels the payment by himself, the current application must be returned, and return relevant information.

Here we need to use the SDK's proxy protocol and proxy method for processing returned information:

  

In the SDK header file, we can find the protocol:

  

Okay. Let's also go to the official Demo to see how it is used:

  

  

Instead, we only need to copy some code in the red box below and use it directly:

  

Go back to my simple project and paste it in it:

  

There are two main items in the returned information: resp. errCode error codes and resp. the error cause of errStr is often encountered in actual development, so it is also a detail asked during the interview.

Then you can join by clicking: https://pay.weixin.qq.com/wiki/doc/api/app.php? Chapter = 8_5 can be found in the official development documentation:

    

Then, based on the actual development needs, we may need to return the app information:

Add a proxy method to the current AppDelegate. m file:

  

5. Here, the entire payment process is completed. You can use your real machine to test it, because the simulator is not easy to install.

 

 

 

Reprinted with the source: http://www.cnblogs.com/goodboy-heyang/p/5255818.html, respect for labor results.

 

At last, I added the explanation and source code to github, a great god that has no intention of discovery. You can also learn:

Https://github.com/renzifeng/WXPay

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.