IOS integrated micro-credit payment development _ios

Source: Internet
Author: User

The example of this article for everyone to share the iOS integrated micro-credit payment development code for your reference, the specific content as follows

The first step is to clarify the process:

1, the user uses the app client, selects the commodity to order.
2, the Merchant client (is your app) to the user's merchandise data to the merchant server, request to generate payment orders.
3, the merchant background call unified under the single API to the micro-letter server to send a request, micro-trust server to generate prepaid orders, and generate a prepay_id back to the merchant backstage.
4, the merchant backstage will return this prepay_id to the merchant client.
5, the user clicks to confirm the payment, this time merchant client invokes the SDK to open the micro-letter client, carries on the micro-letter payment.
6, micro-trust clients to the micro-trust server to initiate payment requests and return the payment results (they are interacting with the prepay_id this parameter, the micro-trust server to verify that the micro-letter client Pass the parameters of the past with the same ID generated in the third step).
7, the user input payment password, the micro-trust client to submit payment authorization, with the micro-trust server interaction, complete payment
8, micro-trust server to the micro-letter client to send payment results, and asynchronously to the merchant server to send payment results notification.
9, the Merchant client through the payment results callback interface query payment results, and to the background check the payment results are correct, back to pay results.
10, the Merchant client displays the payment result, completes the order, delivers the goods.

Although it looks a bit more, but it is not complicated to understand, and we usually buy things on the phone is the same. What our clients need to do is

To initiate a payment with a micro-letter client
Show Payment Results

Integration process

The first is to download the SDK

Micro-Letter SDK Download

Recommended headers and samples are downloaded to see. (Under the Spit, the official example is ugly, look Ching!) There are not many comments ... Despise)

Import micro-credit Payment SDK Library

Import the above iOS header files and libraries download the SDK package on the line, I am here is SDK1.6.2. Then you need to link on the library, in target-> buildphases-> link Binary with libraries-Click + Number-> search for the system library you need.

Systemconfiguration.framework
Libz.tbd
Libsqlite3.0.tbd
Coretelephony.framework
Quartzcore.framework

Set URL Scheme

When registering a micro-platform app, a unique identifier (APPID) is given, which is clearly stated in the app-side development step, and needs to be filled in the URL schemes this place,


URL scheme

Register AppID in Appdelegate
As follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
  Override point for customization after application launch.
  /**
   * to the micro-letter terminal Registration ID, where the AppID general recommendations written as a macro, easy to maintain. @ "Test Demo" does not require a tube. Here the ID is false, need to change here and target inside the URL Type
   * * *
  [Wxapi registerapp:@ "wxd930ea5d5a258f4f" withdescription:@ "Test Demo"];

  Return YES
}

Handles the data that is passed when the micro-letter is started through the URL

The previous two methods have been discarded by iOS9, and this is why Xcode7.2 online would appear to be unable to enter the Onresp callback method of micro-trust.
Originally I did not want to write two old methods, but a look at the official demo on the two written, I also wrote .... 9.0 ago method, in order to fit the low version retention-(BOOL) Application: (UIApplication *) application Handleopenurl: (Nsurl *) url{return [Wxapi Handleo
Penurl: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: (nsurl *) URL options: (nsdictionary<nsstring *,id> *) options{//Here to determine whether the initiation of the request for the micro-letter payment, if it is, use the Wxapi method to adjust the micro-letter client's Payment page (://pay before the string is your AppID,) return [Wxapi Handleopenurl:url D
Elegate:self]; }//micro-letter SDK with the method, processing from the micro-trust client after the return program after the callback method, display payment results-(void) Onresp: (baseresp*) RESP {//Start micro-letter payment response NSString *payres
  Oult = [NSString stringWithFormat: @errcode:%d, Resp.errcode];
   if ([Resp Iskindofclass:[payresp class]]) {//Payment return result, the actual payment result needs to go to the micro-trust server-side query switch (resp.errcode) {   Case 0:payresoult = @ Payment Result: Success!;
      Break
        Case-1: Payresoult = @ paid result: Failed!;
      Break
        Case-2: Payresoult = @ user has quit paying!;
      Break Default:payresoult = [NSString stringwithformat:@ payment Result: Failure!
        Retcode =%d, Retstr =%@, resp.errcode,resp.errstr];
    Break

 }
  }
}

The most important thing is to!!!.

Before calling a micro-credit payment, you need to order, sign, and so on to obtain the necessary parameters for the micro-letter payment. In order to improve security, the next single, signature operation is usually done in the background, in front of the words were captured change information is not happy ...

The required parameters include: AppID, PartID (merchant number), Prepayid (pre-payment order ID), NONCESTR (random string to participate in the signature), timestamp (time stamp to participate in the signature), sign (signature string), these six.
In the Pay-per-click Controller, use the core code to tune the micro-letter client to pay, and these parameters are sent back to you in the background. Added the annotation, should be very good understanding.

#pragma mark Micro-credit payment method
-(void) wxpay{

    //need to create this payment object
    payreq *req  = [[Payreq alloc] init];
    A unique identification consisting of the user's micro-signal and AppID, used to verify the
    Req.openid = @ "";

    Merchant ID, the
    req.partnerid = @ "" At the time of registering;

    Pre-payment order this is the background and micro-trust server interaction, the micro-trust server to your server, your server to you
    Req.prepayid = @ "";

    According to the Tenpay document fill in the data and signature
    //This relatively special, is fixed, can only be req.package = Sign=wxpay
    req.package  = @ "";

    Random coding, in order to prevent duplication, in the background to generate
    Req.noncestr = @ "";

    This is the timestamp, also generated in the background, in order to verify the payment of the
    NSString * stamp = @ "";
    Req.timestamp = Stamp.intvalue;

    This signature is also done backstage
    req.sign = @ "";

    Send request to micro-letter, wait for micro-letter return onresp
    [Wxapi sendreq:req];
}

The data in this JSON (the above parameter) is the backstage need to pass to you, as to how to come, also has the backstage document, lets him go to look down on the line ~ ~ ~

{"
 AppID": "Wxb4ba3c02aa476ea1", "
 noncestr": "d1e6ecd5993ad2d06a9f50da607c971c",
 "package": "sign= Wxpay ",
 " Partnerid ":" 10000100 ",
 " Prepayid ":" wx20160218122935e3753eda1f0066087993 ",
 " timestamp ":" 1455769775 ",
 " sign ":" F6dee4add82217782919a1696500af06 "
}

--Unified Next single API

--Adjusting the payment interface

Here, no accident should be able to pay the normal. The process is the most important, understand how to do it, strongly recommend the friends who need to do the first idea, do not rush to start.

PS: This article, the signature is done in the background, if you need to do in your client, you can refer to this article, and this article, their signature on the client, write a more detailed.

Problems that may be encountered

1. If the payment is complete and remain in the micro-letter, check the scheme setup problem under Urltype.

2. Able to open the micro-letter client, but open only in the middle of a white "OK button", click will return to the client, if so, it should be the problem of prepayid parameters, expired, or is not the real ID. There is nothing wrong with the code. The special note is that the micro-letter to two times signed, two times ~ ~ ~ ~ ~

3. If the app uses a friend union or SHARESDK to do the sharing, then no longer import the SDK, or there will be some strange problems, such as unable to adjust the handset micro-letter client, unable to tune the micro-letter client Web page, tuned up but a flash over ... All of this is basically because the SDK for the share has already included the micro-mail SDK. So if there's a weird mistake, see if it's two conflicts!

4. Micro-letter Payment Unit is divided, the people who have been in the pit know .... Hey.

If you encounter any problems in the integration process, you can discuss

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.