IOS Micro-credit payment development case _ios

Source: Internet
Author: User
Tags lowercase md5 md5 encryption rar

Micro-letter payment has a lot of holes, the official documents are not comprehensive, and demo is also more "shy" difficult to understand. A lot of attention details are not reflected, took a lot of detours. Therefore, the development process of the system is as follows. (Full demo attached to the last side of the blog) This includes the micro-letter payment development and Alipay development call client compatibility processing. (two times the signature on the mobile side, the blog does not mention not install the micro-letter client, but also very simple [Wxapi sendreq:request] Call this method of the return value of 0 can be judged. )

First, the Environment configuration

1, first is the HTTPS protocol access, in the Plist set white list

2, when your project has the micro-letter sharing, collection, payment, login, etc., need to add the following code in "Info.plist" (lsapplicationqueriesschemes)

3, solve the problem of Bitcode compilation

4, set the URL types

5, Import SDK (can be transferred from the micro-letter official demo)

6. Import System Dependencies Library

7. Special attention is paid to

Micro-credit payments are divided into units, that is, if you pay the amount is to retain two decimal places, the amount paid must be *100 and then passed to the micro-credit payment platform, and so on.

Second, code development

1, in the program entrance (that is, APPLEGATE.M) need to do the operation

2, add the following agent method, otherwise do not callback micro-letter customer service End (also in DELEGATE.M file)

3, micro-credit payment success callback function

This is necessary to explain the official document is as follows: Can not be paid for the success of customer service to pay the results directly as a result of the success of the order to pay, to be returned by the server order status, that is, customer service after the successful payment, micro-credit payment platform will send the server to pay the success of the message, the background modified order status, Re-return to the client. The simple thing is that payment will be sent to a particular view controller (a view controller with a micro-credit payment function) to send a notification, allowing this particular view to be controlled to request the status of the server order. This callback function must be written in the DELEGATE.M!!!

4. Code for a particular view controller (view controller with micro-credit payment function)
4.1 Submit a pre-payment order to get the booking ID (this process must be signed two times, with no parameter signature and carry parameter signature, of course, these methods have been encapsulated in the Payrequsesthandler class, you only need to pass the parameter call method on it) This is also the action method of clicking on the Payment button

4.2 Receive the Advance payment order, then can adjust the micro letter to pay the customer service end (4.2 and 4.1 code is continuous)


With the sign parameter signature of the independent package of two methods, no Payrequsesthandler encapsulation method, write their own, because of the problem (the screenshot below), the first code screenshot, and then provide a direct copy of the pasted code block
Screenshot: (I believe many people have encountered, 100% is the problem of sign signature)


Method One:


Method Two:

4.3 The above code can completely solve the problem of micro-credit payment, leaving the last step to pay the successful return of the app call Delegate-(void) Onresp: (baseresp*) Resp method, so here to send a notification to a specific view controller, Ask him to request the status of the order in the background. Here to explain is that you from the interface of the micro-letter payment, return or in situ. Only the callback method must be in delegate, so a successful notification must be sent in the callback method. Then, in the way that the view of a particular view controller is about to appear, monitor the notification and request the status of the order in the background. Note here that you need to rewrite the Dealloc method to remove the notification.

Third, Alipay and micro-credit payment callback client's proxy method compatible handling

Four, demo share address

Http://xiazai.jb51.net/201608/yuanma/IOSwxPay (jb51.net). rar

Code to copy sticky (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]; Alphabetical sort Nsarray *sortedarray = [keys Sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {return [Obj1 Co 
Mpare:obj2 Options:nsnumericsearch]; 
}]; concatenation string 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]];} 
//Add Merchant key field [contentstring appendformat:@ "key=%@", @ "fill in merchant key here"]; 
NSString *result = [self md5:contentstring];

return result; 
}//creates the SiGe signature at the time the payment was initiated-(NSString) MD5: (nsstring) str {const char *CSTR = [Str utf8string]; 
unsigned char result[16]= "0123456789abcdef"; 
CC_MD5 (CStr, (Cc_long) strlen (CStr), result); Here the x is lowercase, the resulting MD5 is also lowercase, x is uppercase MD5 is uppercase, here can only be capitalized, micro-letter case verification very funny 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 encryption

Micro-Credit Payment download Demo:http://xiazai.jb51.net/201608/yuanma/iosweixinpay (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.