Home WeChat Applet WeChat Development Detailed explanation of the steps for developing payment on IOS WeChat

Detailed explanation of the steps for developing payment on IOS WeChat

Apr 27, 2017 pm 02:18 PM

Foreword: The following introduces the details of the development process of WeChat payment, with pictures and texts. You can follow my essay process to go through the code. You’ve also learned how to pay via WeChat. And payment is also a frequently asked question in interviews.

Text:

1. First of all, before starting to use WeChat payment, there are some things that developers must know. Open the link below :

 pay.weixin.qq.com/wiki/doc/api/app.php?chapter=3_1 

Then you can see the following page, this is the development of WeChat payment merchant platform Documents, many things can be consulted and understood. When developing and using the WeChat SDK payment function, if you encounter problems, you can also find relevant information here:

  

Then , and also tell readers to click "Payment Account>Payment Account" in this development document, and then scroll the current page to the bottom to see the APPID:

  

Note: This APPID is necessary to use WeChat payment during development, and this APPID can only be used by merchants to register on the WeChat payment platform, spend 300 yuan, fill in a lot of relevant important information, and upload the business License and other necessary procedures are required to obtain the APPID.

As for commercial app applications, when customers use the app WeChat to make purchases, the program will find the merchant based on this unique APPID, and then transfer the consumer's amount to the merchant's account.

 

Benefits to developers: For developers, the WeChat payment platform provides a demo for testing and also provides useful functions in the demo source code. The APPID of the test code. In this way, developers eliminate the need to spend 300 yuan to buy an APPID.

2. In order for readers to learn the use process of this WeChat SDK more conveniently and more specifically, I will create an ordinary project, directly use the WeChat SDK directly on it, and complete WeChat payment.

Pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1

Click in to download the SDK, and also download the Demo. You can Let you refer to the use of the source code, or you can save it for further exploration in the future:

  

In the newly created project, we drag the downloaded SDK into the downloaded SDK file There are five files in it. You don’t need to keep the read_me.txt in the project as you like, but you can open and read the prompt information inside:

  

Let’s open read_me first. txt file, in fact, it talks about the problems solved in the recent updates of several versions, as well as the precautions for using the SDK. I will use the parts framed in red in subsequent operations. SO, this read_me file is very important. .

 

3. Okay, let’s do the necessary process according to read_me.txt:

After Xcode 7 version, you need to import the framework and link library :

   

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

Then, according to the prompts of read_me.txt, we 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>
Copy after login

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

  

App Transport Security Settings requires manual addition of settings during development after XCode7, because iOS9 restricts access to the http protocol by default.

LSApplicationQueriesSchemes can whitelist the URL Schemes to be used, so that the current application can use WeChat's related capabilities (sharing, collection, payment, login, etc.).

There is one final operation. Set the APPID used for WeChat payment to URL Schemes [English skiːm].

 

4. Okay, we can start typing the code:

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

  

  然后回到自己建立的工程中,写下了微信支付的流程:

  

  既然要注册微信,那么我们先到微信SDK的头文件中查看一下,发现只提供了两个注册方法,注释也写的很清楚:

  

  然后我们导入这个头文件之后,直接根据已经有的APPID进行注册:

  

  好,其实步骤:1、导入微信支付SDK,注册微信支付。然后2、设置微信APPID为URL Schemes前面已经做好了。

  然后我们需要进行3、发起支付,调其微信支付,在这之前,我们直接看看微信官方提供给我们的Demo:

  

  

  最后我们找到了Demo中完整的可以直接用的这部分发起微信支付的源码:

  

  将这段直接拷贝到我的工程中,有那么一点经验的开发者就会注意到一些,比如Demo源码使用了MRC的autorelease,你可以手动去掉,类方法可以换成实例方法,根据你的实际项目开发需求:


 1 - (NSString *)jumpToBizPay { 2  3 //============================================================ 4     // V3&V4支付流程实现 5     // 注意:参数配置请查看服务器端Demo 6     // 更新时间:2015年11月20日 7     //============================================================ 8     NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios"; 9     //解析服务端返回json数据10     NSError *error;11     //加载一个NSURL对象12     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];13     //将请求的url数据放到NSData对象中14     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];15     if ( response != nil) {16         NSMutableDictionary *dict = NULL;17         //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中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                 //调起微信支付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                 //日志输出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 @"服务器返回错误,未获取到json对象";43         }44     }else{45         return @"服务器返回错误";46     }47 }
Copy after login

  哦,对了,还有一个很简单但很必要的操作忘记展示出来了:

  

  进一步,我们在微信的SDK源码头文件中,可以找到两个很有用的方法,你也可以在微信支付平台打开开发者文档找到这两个方法的介绍:

  

  然后我将其运用在我的工程中

  

  好,就这样,步骤:3、发起微信支付,调起微信 到这里就完成了。

  最后还需要做的就是,处理返回微信支付返回信息,使用了微信知否功能,不管是支付成功和失败,甚至还是用户自己取消支付,都会需要返回当前应用,并返回相关的信息。

  这里就需要用到微信SDK的处理返回信息的代理协议和代理方法了:

  

  在微信SDK的头文件中,我们可以找到protocol协议:

  

  好,我们也官方Demo中看看它是如何使用的:

  

  

  而我们只需要使用下面红色框框起来的部分代码,直接拷贝拿来使用:

  

  回到我的简易工程中,直接粘贴在里面用:

  

  那么这里面的返回信息中主要就有两个东西:resp.errCode错误码 和 resp.errStr错误原因,这两个东西在实际开发中经常遇到,所以也是面试会问到的一个细节。

  接着你可以通过点进连接:pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5 可以在官方开发文档中找到:

    

  然后,根据实际开发需求,我们可能还需要回传微信app的相关信息:

  在当前AppDelegate.m文件添加一个代理方法:

  

5. At this point, the entire WeChat payment usage process is completed. Now you can use your real machine to test it, because the simulator is not easy to install WeChat.

Reprint and indicate the source: www.cnblogs.com/goodboy-heyang/p/5255818.html, respect the fruits of labor.

Finally, I accidentally discovered that the master github also has WeChat explanations and source codes. You can also learn from them:

github.com/renzifeng/WXPay

But, for those who don’t I spent 99 US dollars to buy a developer account. The source code downloaded from github cannot be directly tested on a real machine. The reason is that the above project instance was created relatively early. At that time, XCode did not support real machines without a developer account. Machine testing.

The above is the detailed content of Detailed explanation of the steps for developing payment on IOS WeChat. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1238
24
Apple re-releases iOS/iPadOS 18 Beta 4 update, version number raised to 22A5316k Apple re-releases iOS/iPadOS 18 Beta 4 update, version number raised to 22A5316k Jul 27, 2024 am 11:06 AM

Thanks to netizens Ji Yinkesi, xxx_x, fried tomatoes, Terrence, and spicy chicken drumsticks for submitting clues! According to news on July 27, Apple today re-released the iOS/iPadOS 18 Beta 4 update for developers. The internal version number was upgraded from 22A5316j to 22A5316k. It is currently unclear the difference between the two Beta 4 version updates. Registered developers can open the "Settings" app, enter the "Software Update" section, click the "Beta Update" option, and then toggle the iOS18/iPadOS18 Developer Beta settings to select the beta version. Downloading and installing the beta version requires an Apple ID associated with a developer account. Reported on July 24, iO

Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Aug 18, 2024 am 06:34 AM

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Apple releases open source Swift package for homomorphic encryption, deployed in iOS 18 Jul 31, 2024 pm 01:10 PM

According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

Apple iOS/iPadOS 18 Developer Preview Beta 4 released: Added CarPlay wallpapers, sorted out settings options, enhanced camera control Apple iOS/iPadOS 18 Developer Preview Beta 4 released: Added CarPlay wallpapers, sorted out settings options, enhanced camera control Jul 24, 2024 am 09:54 AM

Thanks to netizens Spicy Chicken Leg Burger, Soft Media New Friends 2092483, Handwritten Past, DingHao, Xiaoxing_14, Wowotou Eat Big Kou, Feiying Q, Soft Media New Friends 2168428, Slades, Aaron212, Happy Little Hedgehog, Little Earl, Clues for the little milk cat that eats fish! [Click here to go directly to the upgrade tutorial] According to news on July 24, Apple today pushed the iOS/iPadOS18 developer preview version Beta4 update (internal version number: 22A5316j) to iPhone and iPad users. This update is 15 days after the last release. . Carplay Wallpaper Apple has added wallpapers to CarPlay, covering light and dark modes. Its wallpaper style is similar to iPhone

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Haqu K2 projector brings Olympic passion and dreams within reach Haqu K2 projector brings Olympic passion and dreams within reach Jul 24, 2024 pm 01:34 PM

In the just-concluded European Cup final, did you cheer crazily for the team you supported? In the upcoming Paris Olympics, are you also looking forward to perfectly capturing the highlight moments of each event? Among them, having a high-quality viewing equipment is crucial. The Haqu K2 projector is well-deserved to be a good choice for watching games due to its high cost performance and excellent performance. It not only has high brightness and clear picture quality, but also provides an immersive viewing experience, making every exciting moment of the game feel as if it is close at hand. Are you already attracted by such a device? It will definitely allow you to enjoy the passion and dreams of the Olympic Games at home. The most intimate highlight of Haqu K2 is its 210° super angle adjustment, which makes it convenient to watch movies whether on the ceiling or on the wall.

Apple iOS 18 and iPadOS 18 public beta version Beta 2 update released Apple iOS 18 and iPadOS 18 public beta version Beta 2 update released Jul 30, 2024 pm 04:19 PM

Thanks to netizens Mo 6_, Uh-huh-huh-huh, Cat-Eating Salted Fish, Yaochi Qinglian, Spicy Chicken Leg Burger, Siyan, and Tim Apple for submitting clues! According to news on July 30, Apple today launched the iOS18 and iPadOS18 public beta version Beta2 version update for iPhone and iPad users, two weeks after the last public beta version. The update content of this public beta version is similar to the developer preview version Beta4, with new CarPlay wallpapers, combing setting options, enhanced camera control, dark/light mode icons, etc. For details, please refer to the previous detailed reports. ##How to upgrade iOS/iPadOS/watchOS/macOS development version and public beta version? iOS/iPadOS upgrade iOS/iPa

Apple iOS / iPadOS 17.6 Developer Preview Beta 4 released Apple iOS / iPadOS 17.6 Developer Preview Beta 4 released Jul 18, 2024 pm 01:43 PM

Thanks to netizen Ji Yinkesi for submitting the clue! [Click here to go directly to the upgrade tutorial] According to news on July 17, Apple today pushed the iOS/iPadOS17.6 developer preview version Beta4 update (internal version number: 21G5075a) to iPhone and iPad users. This update is separated from the last release. 7 days. iOS Test Cycles and Updates Apple typically runs two separate test cycles each summer: Major Fall Updates: For major iOS updates rolled out in the fall. Revision of previous year's update: Usually the last revision of the previous year's update. iOS18 and iOS17.6 beta iPhone users currently have a choice: iOS18 developer beta iOS17.6 developer beta iOS1

See all articles