IOS development integrates WeChat payment and ios development payment

Source: Internet
Author: User

Integrated payment for iOS development and payment for ios development

First, you need to clarify the process:

Although it looks a little more, it is not complicated to understand. It is the same as buying things on our mobile phones. What the client needs to do is

  • Call the client to initiate a payment
  • Show payment result
The first thing to do is to download the SDK,
Download SDK


We recommend that you download the header files and examples. (The Official example is ugly and dizzy! There are few annotations... Contempt)

Import the payment SDK Library

Import the aboveIOS header file and library downloadJust download the SDK package. Here I am using SDK1.6.2. then I need to link to the dependent library, go to Target-> BuildPhases-> Link Binary With Libraries-click + and search for the desired system library.

  • SystemConfiguration. framework
  • Libz. tbd
  • Libsqlite3.0.tbd
  • CoreTelephony. framework
  • QuartzCore. framework
Set URL Scheme

When registering the APP on the platform, a unique identification identifier (APPID) will be provided. It is clearly stated in the APP development step and must be filled in the URL Schemes,


URL scheme registers APPID in Appdelegate

As follows:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. /*** register the ID with the terminal. The APPID here is generally recommended to be written into a macro, which is easy to maintain. @ "Test demo" is not required. The id here is false. You need to change the URL Type in the target */[WXApi registerApp: @ "wxd930ea5d5a258f4f" withDescription: @ "test demo"]; return YES ;}

 

Process data transmitted during URL startup

// The previous two methods are deprecated by iOS9. If Xcode7.2 is online, the onResp callback method cannot be accessed, which is the reason. I didn't want to write the two old methods, but I wrote the two in the official demo ....
// Methods earlier than 9.0, reserved for earlier versions-(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url {return [WXApi handleOpenURL: url delegate: self];}-(BOOL) application :( UIApplication *) application openURL :( NSURL *) url sourceApplication :( NSString *) sourceApplication annotation :( id) annotation {return [WXApi handleOpenURL: url delegate: self];} // method after 9.0-(BOOL) application :( UIApplication *) app openURL :( NSUR L *) url options :( NSDictionary <NSString *, id> *) options {// check whether the request is paid. If yes, use WXApi to call the client's payment page (the string before: // pay is your APPID) return [WXApi handleOpenURL: url delegate: self];} // The method that comes with the SDK. It processes the callback method after the client completes the operation and returns the program. The-(void) onResp :( BaseResp *) of the payment result is displayed *) resp {// start the response NSString * payResoult = [NSString stringWithFormat: @ errcode: % d, resp. errCode]; if ([resp isKindOfClass: [PayResp class]) {// return the payment result. The actual payment result must be Query switch (resp. errCode) {case 0: payResoult = @ payment result: Success !; Break; case-1: payResoult = @ payment result: Failed !; Break; case-2: payResoult = @ the user has exited payment !; Break; default: payResoult = [NSString stringWithFormat: @ payment result: failed! Retcode = % d, retstr =%@, resp. errCode, resp. errStr]; break ;}}}

 

 
The most important thing is coming !!

Before making a payment, you need to place an order, sign the order, and perform other operations to obtain the necessary parameters for payment. In order to improve security, the ordering and signature operations are generally completed in the background. If you do so in the foreground, it will be hard to capture and modify the information .....

The required parameters include:: Appid, partid, prepayid, noncestr, timestamp, sign) these six.
UseCore codeTo call the client to pay, these parameters are sent to you in the background. It should be easy to understand with comments.

# Pragma mark payment method-(void) WXPay {// you need to create this payment object PayReq * req = [[PayReq alloc] init]; // a unique identifier consisting of the user ID and AppID, used to verify the user req. openID = @ ""; // merchant id, which is the req. partnerId = @ ""; // pre-payment order. After the background interacts with the server, the server sends the order to your server, and your server sends it to your req. prepayId = @ ""; // according to the data and signature entered in the caifu document, this is special and is fixed. It can only be req. package = Sign = WXPay req. package = @ ""; // random encoding. In order to prevent duplication, req is generated in the background. nonceStr = @ ""; // This is a timestamp generated in the background. To verify the NSString * stamp = @""; Req. timeStamp = stamp. intValue; // This signature is also the req created in the background. sign = @ ""; // send the request, wait for the onResp [WXApi sendReq: req] to be returned;} the data in this JSON (the above parameter) is the data that needs to be sent to you by the background, as for how to come, there are also background documents. Just let him see them ~~~ {"Appid": "wxb4ba3c02aa476ea1", "noncestr": "signature", "package": "Sign = WXPay", "partnerid": "10000100", "prepayid ": "wx20160218122935e3753eda1f0066087993", "timestamp": "1455769775", "sign": "F6DEE4ADD82217782919A1696500AF06 "}

 

  • Unified order API

  • Call the payment Interface

In this case, you should be able to pay normally. The process is the most important. Once you understand it, you will know how to do it. We strongly recommend that you do your best to manage your ideas first.

PS: in this article, signatures are all done in the background. If you need to do so on your client, you can refer to this article and this article. Their signatures are made on the client, the description is also detailed.

Possible problems

1. If the payment is complete, stayCheck the Scheme settings in URLType.

2. You can open the client, but there is only one white "OK" button in the middle. After you click it, it will return to the client.In this case, it should be a problem with the prepayid parameter, expired, or not a real id. The code is correct. Note that two signatures are required ~~~~

3. If the APP uses umeng or ShareSDK for sharingYou do not need to import the SDK any more. Otherwise, some strange problems may occur, such as the inability to call the mobile client and the inability to call the web page of the client, it was called, but it was a flash... This is basically because the shared SDK already contains the SDK. So if there is a strange error, check if there are two conflicts!

4. The payment unit is minuteAll people who have been pitted know this .... Ah,

If you encounter any problems during the integration process, let's discuss them and let me know if I have recorded any errors! Thank you!

 

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.