Home WeChat Applet WeChat Development Detailed explanation of iOS WeChat payment development case code

Detailed explanation of iOS WeChat payment development case code

Mar 27, 2017 pm 01:30 PM
ios WeChat Pay

This article mainly introduces the iOS WeChat payment development case in detail, which has certain reference value. Interested friends can refer to

WeChat payment has many pitfalls, and the official documents are provided It is not comprehensive at all, and the demo is also "shy" and difficult to understand. Many of the details that were paid attention to were not reflected and many detours were taken. Therefore, the system development process is as follows. (The complete demo is attached at the end of the blog) This includes the compatibility processing of WeChat payment development and Alipay development calling client. (The two signatures are placed on the mobile side. The blog does not mention the situation of not installing the WeChat client. It is also very simple to judge by the return value of 0 when calling this method [WXApi sendReq:request].)

1. Environment configuration

1. First, https protocol access, set the whitelist in plist

Detailed explanation of iOS WeChat payment development case code

2. When you have For WeChat sharing, collection, payment, login, etc., you need to add the following code (LSApplicationQueriesSchemes) in "Info.plist"

Detailed explanation of iOS WeChat payment development case code

3. Solve the problem that bitcode cannot be compiled

Detailed explanation of iOS WeChat payment development case code

4. Set URL types

Detailed explanation of iOS WeChat payment development case code

5. Import SDK (can be transplanted from WeChat official demo)

Detailed explanation of iOS WeChat payment development case code

6. Import system dependency library

Detailed explanation of iOS WeChat payment development case code

7. Special attention should be paid to the fact that

WeChat payment is divided into points. Unit, that is to say, if your payment amount is kept to two decimal places, the payment amount must be *100 before being sent to the WeChat payment platform, and so on for the others.

2. Code development

1. Operations required at the program entrance (i.e. applegate.m)

Detailed explanation of iOS WeChat payment development case code

2. Add the following Proxy method, otherwise the WeChat customer service side will not be called back (also in the delegate.m file)

Detailed explanation of iOS WeChat payment development case code

3. The callback function for successful WeChat payment

This is necessary To explain, the official document explains this: The result of successful payment on the customer service side cannot be directly regarded as the result of successful payment of the order. It must be based on the order status returned by the server. In other words, after the payment on the customer service side is successful, WeChat Pay The platform will send a payment success message to the server, modify the order status in the background, and return it to the client. The simple thing is that if the payment is successful, a notification must be sent to a specific view controller (a view controller with WeChat payment function), so that this specific view control can request the status of the server order. This callback function must be written in delegate.m!!!

Detailed explanation of iOS WeChat payment development case code

4. Code for a specific view controller (view controller with WeChat payment function)
4.1 Submit a prepayment order to obtain the pre-order id (this process must be signed twice, namely without parameters and signature Carrying parameter signatures, of course, these methods have been encapsulated in the payRequsestHandler class, you only need to pass the parameters to call the method) This is also the action method of clicking the payment button

Detailed explanation of iOS WeChat payment development case code

4.2 Get the prepaid order, then you can adjust the customer service side of WeChat payment (4.2 and 4.1 codes are consecutive)

Detailed explanation of iOS WeChat payment development case code

Two independent packages with sign parameter signature Method, I did not use the method encapsulated by payRequsestHandler, I wrote it myself, because there was a problem after using it (the screenshot of the problem is as follows), take a screenshot of the code first, and then provide a code block that can be directly copied and pasted
Screenshot of the problem: (I believe many people have encountered this I have been here before, 100% it is a signature issue)

Detailed explanation of iOS WeChat payment development case code

Method one:

Detailed explanation of iOS WeChat payment development case code

Method two:

Detailed explanation of iOS WeChat payment development case code

4.3 The above code can completely solve the WeChat payment problem. The last step is left. The payment is successful and returns to the app to call the delegate's -(void)onResp:(BaseResp*)resp method. So here we need to send a notification to a specific view controller and let him request the background order status. What I want to explain here is that after you adjust WeChat Pay from that interface, you will still be in the same place when you return. It’s just that the callback method must be in the delegate, so a successful notification must be sent in the callback method. Then you need to listen to this notification in the method of the specific view controller's view that is about to appear, and then request the background order status. What needs to be noted here is that the dealloc method needs to be rewritten to remove the notification.

Detailed explanation of iOS WeChat payment development case code

3. Compatibility processing of proxy methods of Alipay and WeChat payment callback clients

Detailed explanation of iOS WeChat payment development case code

4. demo

Code for copy and paste (sign signature)

-(NSString )createMD5SingForPay:(NSString )appid_key partnerid:(NSString)partnerid_key prepayid:(NSString )prepayid_key package:(NSString )package_key noncestr:(NSString)noncestr_key timestamp:(UInt32)timestamp_key 
{ 
NSMutableDictionary *signParams = [NSMutableDictionary dictionary]; 
[signParams setObject:appid_key forKey:@”appid”]; 
[signParams setObject:noncestr_key forKey:@”noncestr”]; 
[signParams setObject:package_key forKey:@”package”]; 
[signParams setObject:partnerid_key forKey:@”partnerid”]; 
[signParams setObject:prepayid_key forKey:@”prepayid”]; 
[signParams setObject:[NSString stringWithFormat:@”%u”,(unsigned int)timestamp_key] forKey:@”timestamp”];
NSMutableString *contentString =[NSMutableString string]; 
NSArray *keys = [signParams allKeys]; 
//按字母顺序排序 
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch]; 
}]; 
//拼接字符串 
for (NSString *categoryId in sortedArray) { 
if ( ![[signParams objectForKey:categoryId] isEqualToString:@”“] 
&& ![[signParams objectForKey:categoryId] isEqualToString:@”sign”] 
&& ![[signParams objectForKey:categoryId] isEqualToString:@”key”] 
) 
{ 
[contentString appendFormat:@”%@=%@&”, categoryId, [signParams objectForKey:categoryId]];
} 
} 
//添加商户密钥key字段 
[contentString appendFormat:@”key=%@”, @”这里填写商户密钥”]; 
NSString *result = [self md5:contentString]; 
return result;

}//创建发起支付时的sige签名

-(NSString ) md5:(NSString )str 
{ 
const char *cStr = [str UTF8String]; 
unsigned char result[16]= “0123456789abcdef”; 
CC_MD5(cStr, (CC_LONG)strlen(cStr), result); 
//这里的x是小写则产生的md5也是小写,x是大写则md5是大写,这里只能用大写,微信的大小写验证很逗 
return [NSString stringWithFormat: 
@”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”, 
result[0], result[1], result[2], result[3], 
result[4], result[5], result[6], result[7], 
result[8], result[9], result[10], result[11], 
result[12], result[13], result[14], result[15] 
]; 
}//MD5 加密
Copy after login


The above is the detailed content of Detailed explanation of iOS WeChat payment development case code. 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)

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

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.

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

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.

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

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