IOS-WeChat payment details

Source: Internet
Author: User
Tags openid

IOS-payment details

The payment function is often involved in iOS development. Here we will provide a detailed description of common payments.

1. Payment 1.1 Open Platform account registration

Open Platform (if payment is required, AppKey/SecretKey will be used after registration, and SecrectKey will be handed over to the background for integration)
The open platform is the application portal for the merchant APP to access the open payment interface. Through this platform, you can apply for APP payment.

1.2 Project Modification and setting

1) first, you should download the SDK and import the project (here the umeng comes with the payment instructions, the specific content is similar)
If you use the umeng social sharing SDK in the project, you do not need to import the SDK again. umeng has already integrated payment. The SDK is as follows:

If your project does not use umeng, please download the latest SDK from the open platform and import it to your project. The specific SDK is as follows:

2) After the SDK is added, you need to add the corresponding library and files.

3) then add the corresponding url for calling.

4) Add a whitelist and disable Bitcode (modify the http/https adapter to iOS9.0)

1.3 SDK registration and callback

After completing the above operations, we can use the imported SDK, which is very simple.
1) first, we need to register
Import # import "WXApi. h"

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. viewController * userGuideViewController = [[ViewController alloc] init]; self. window. rootViewController = userGuideViewController; [WXApi registerApp: @ "add APPKey here"]; return YES ;}

2) Then we will write a call.

-(BOOL) application :( UIApplication *) app openURL :( NSURL *) url options :( NSDictionary
  
   
*) Options {// jump to the address configured in the URL scheme // NSLog (@ "Jump to the address configured in the URL scheme -- >%@", url); return [WXApi handleOpenURL: url delegate :( id
   
    
) Self];}
   
  

3) then the callback

-(BOOL) application :( UIApplication *) application openURL :( NSURL *) url sourceApplication :( NSString *) sourceApplication annotation :( id) annotation {// NSLog (@ "******************** url. host -- % @ ", url. host); if ([url. scheme isEqualToString: @ "Here AppKey"]) {return [WXApi handleOpenURL: url delegate :( id) self];} return YES;}-(void) onResp :( BaseResp *) resp {NSString * strMsg = [NSString stringWithFormat: @ "errco De: % d ", resp. errCode]; NSString * strTitle = @ "Payment result"; if ([resp isKindOfClass: [PayResp class]) {switch (resp. errCode) {case WXSuccess: strMsg = @ "Payment result: successful! "; Break; case WXErrCodeUserCancel: strMsg = @" Payment result: click Cancel! "; Break; case WXErrCodeSentFail: strMsg = @" Payment result: failed to send! "; Break; case WXErrCodeAuthDeny: strMsg = @" Payment result: Authorization failed! "; Break; default: strMsg = @" Payment result: Not Supported! "; Break;} UIAlertView * alert = [[UIAlertView alloc] initWithTitle: strTitle message: strMsg delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show] ;}}
-(Void) onResp :( BaseResp *) resp {NSString * strMsg = [NSString stringWithFormat: @ "errcode: % d", resp. errCode]; NSString * strTitle = @ "Payment result"; if ([resp isKindOfClass: [PayResp class]) {switch (resp. errCode) {case WXSuccess: strMsg = @ "Payment result: successful! "; Break; case WXErrCodeUserCancel: strMsg = @" Payment result: click Cancel! "; Break; case WXErrCodeSentFail: strMsg = @" Payment result: failed to send! "; Break; case WXErrCodeAuthDeny: strMsg = @" Payment result: Authorization failed! "; Break; default: strMsg = @" Payment result: Not Supported! "; Break;} UIAlertView * alert = [[UIAlertView alloc] initWithTitle: strTitle message: strMsg delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show] ;}}
1.4 SDK usage

1) The final part is the place where payment is required in the project (This is the most important and important part of the entire payment process.)
First, you need to make a network request. The background interface sends your product information to the background. After an order is generated, the returned information must contain at least six parameters. You can use these parameters to initiate a payment.
Import # import "WXApi. h"

// The return parameter calls PayReq * req = [[PayReq alloc] init]; NSMutableDictionary * dict = [result objectForKey: @ "resultCode"]; req. openID = [dict objectForKey: @ "appid"]; req. partnerId = [dict objectForKey: @ "mch_id"]; req. prepayId = [dict objectForKey: @ "prepay_id"]; req. nonceStr = [self md5: time_stamp]; req. timeStamp = [time_stamp intValue]; req. package = @ "Sign = WXpay"; // signature (this signature can be signed by yourself or in the background and returned by the background) NSMutableDictionary * signParams = [[NSMutableDictionary alloc] init]; [signParams setObject: req. openID forKey: @ "appid"]; [signParams setObject: req. nonceStr forKey: @ "noncestr"]; [signParams setObject: req. package forKey: @ "package"]; [signParams setObject: req. partnerId forKey: @ "partnerid"]; [signParams setObject: time_stamp forKey: @ "timestamp"]; [signParams setObject: req. prepayId forKey: @ "prepayid"]; req. sign = [self createMd5Sign: signParams]; // call the payment (** the parameter has an error and cannot be called **) [WXApi sendReq: req];

2) Self-encryption (this part can be completed in the background)

// Payment encryption-(NSString *) md5 :( NSString *) str {const char * cStr = [str UTF8String]; unsigned char digest [CC_MD5_DIGEST_LENGTH]; CC_MD5 (cStr, (unsigned int) strlen (cStr), digest); NSMutableString * output = [NSMutableString stringWithCapacity: CC_MD5_DIGEST_LENGTH * 2]; for (int I = 0; I <vertex; I ++) [output appendFormat: @ "% 02X", digest [I]; return output;}-(NSString *) createMd5Sign :( NSMutab LeDictionary *) dict {NSMutableString * contentString = [NSMutableString string]; NSArray * keys = [dict allKeys]; // sort NSArray * sortedArray = [keys sortedArrayUsingComparator alphabetically: ^ NSComparisonResult (id obj1, id obj2) {return [obj1 compare: obj2 options: NSNumericSearch] ;}]; // concatenated string for (NSString * categoryId in sortedArray) {if (! [[Dict objectForKey: categoryId] isEqualToString: @ ""] &! [CategoryId isEqualToString: @ "sign"] &! [CategoryId isEqualToString: @ "key"]) {[contentString appendFormat: @ "% =%@ &", categoryId, [dict objectForKey: categoryId];} // Add the key field [contentString appendFormat: @ "key = % @", @ "add SecretKey"]; NSString * md5Sign = [WXUtil md5: contentString]; return md5Sign ;}
1.5 Common exception Bug handling

1) A common problem is that the payment fails to be triggered. Each call can only start a page with your return confirmation button.
Cause: the parameter is incorrect. Pay special attention to the parameter (signature). The parameters returned in the background are incorrect.
Solution: Verify the parameters one by one. The signature can be encrypted locally. Do not rely on the backend to return the signature. In some cases, the returned result is incorrect.

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.