Android Alipay and micro-credit payment integration _android

Source: Internet
Author: User
Tags documentation

Scene
With the rise of mobile payments, there is often a need for integration payments in our app. This is usually the use of micro-mail and Alipay SDK to integrate

(i) Alipay payment
In the process of using Alipay to pay, we are generating orders on the server side, client access interface, and get order information, call interface payment, payment success after Alipay will asynchronously call the server side, and return the payment result to the client.

Development steps:

① Registered Alipay account--real name certification--Submit audit information-Audit through

Alipay Wireless Express Payment Interface:
B.alipay.com/order/productdetail.htm?productid=2014110308141993&tabid=4#ps-tabinfo-hash

PS: Application to upload your apk and product description documents, product screenshots, interface use scene, fee description, etc., after the audit will get the PID and secret key.

The developer can authenticate the message source through the Alipay public key, and can encrypt the information using its own private key.

② Download the Official SDK demo contains the documentation. The demo needs to be replaced with the PID obtained after the audit passes, and enter the Alipay account and the user's private key. Run the demo to see how the SDK is invoked,

There are three kinds of encryption methods for the corresponding key of PID, namely MD5, RSA, DSA. Java developers need to convert the key to PKCS8 format and upload the public key to Alipay. The build method is shown in the document. [Alipay Open Platform]

③ Import project, client invoke,. First, the jar package in the Alipay demo is imported into the project, which can be referenced in the demo, splicing parameters

Alipay Demo Class Import

Method Name: Paytask.pay

//Method prototype:
paytask paytask = new Paytask (activity); String result = Paytask.pay (orderInfo);

Method function: Provide to Merchant order payment function.

The request parameters are assembled into a string in the form of a key value pair, which is specified in the official documentation.

④ modifies manifest, adds com.alipay.sdk.app.H5PayActivity and uses-permission, and adds related obfuscation rules to the proguard-project.txt.

PS: Signature considerations:

In the request parameter list, except for the sign, sign_type two parameters, all other parameters that need to be used are the parameters to be signed
Sign value to do Utf-8 UrlEncode.
⑤ callback processing, see Alipay synchronization notification parameter description. Out_trade_no can interact as a unique flag and server-side,
When the Alipay synchronization notification, the client through the Out_trade_no to ask the server whether the payment is successful, because the true success of the flag is required to verify the server side.

 Public alipayresults (String result) {
  if (textutils.isempty) return
   ;

  string[] Resultparams = Result.split ("&");
  for (String resultparam:resultparams) {
   if (Resultparam.startswith ("Out_trade_no")) {
    Out_trade_no = Gatvalue (Resultparam, "Out_trade_no");}}

 

(ii) micro-credit payment
Similarly, have paid treasure to pay, it is also indispensable to micro-letter payment, micro-letter payments and Alipay payment process, generally the same, through the server to obtain order information returned to the client, the client invoke the SDK to pay, payment completed after the synchronization and asynchronous callback.

Micro-Credit Open Platform Android Access guide:
open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808 &token=&lang=zh_cn

① get AppID
Also need to obtain AppID, registration and selection of mobile applications to be set up, the application submitted for review, only audit through and obtain AppID application can be developed.

② download Android Micro-letter payment demo.

All we need to focus on is payactivity. Using the login and payment of the micro-letter, as you all know, you need to create a new Wxapi folder under the project to implement the callback.

③ Project integration, first we need to import Libammsdk.jar into the Lib directory, this is the core jar package. Also modify manifest

 <activity
   android:name= ". Wxapi. Wxpayentryactivity "
   android:exported=" true "
   android:launchmode=" Singletop "/>

④ interface invocation.

First we need to initialize a IWXAPI API and register it with the application,

Api.registerapp (constants.app_id); 
Payreq req = new Payreq ();
//.... Stitching req Parameter
api.sendreq (req);//Call Payment

⑤ payment callback.

In the Onresp (Baseresp resp) method of payment callback class Wxpayentryactivity, we can obtain the client and server unique flag Prepayid parameters to achieve the subsequent processing of payment success.

 Payment Success
    if (resp instanceof com.tencent.mm.sdk.modelpay.PayResp) {
     Com.tencent.mm.sdk.modelpay.PayResp Payresp = (PAYRESP) resp;
     String Prepayid = Payresp.prepayid;

PS: The payment callback method should be paid attention to in micro-credit payment. The payment callback must create a WXAPI directory in the project and name it wxpayentryactivity (the package name or class name inconsistency can cause an inability to callback), and the callback method gets the Prepayid.

Third-party applications that respond to a request processed by a micro-letter will be recalled to the method
 @Override public
 void Onresp (Baseresp baseresp) {
//  String results = "";
  Switch (baseresp.errcode) {case
   BaseResp.ErrCode.ERR_OK:
//Result    = "send Success";
   {
    Intent Intent = new Intent (constants.payaction);
    Intent.putextra (Constants.prepayid, (PAYRESP) baseresp). Prepayid);
    Mlocalbroadcastmanager.sendbroadcast (intent);
   }
   break;
   Case BaseResp.ErrCode.ERR_USER_CANCEL:
//Result    = "Send Cancel";
    break;
   Case BaseResp.ErrCode.ERR_AUTH_DENIED:
//Result    = "send is rejected";
    break;
   Default:
//Result    = "send return";
    break;
  Finish ();
 }

In short, the three parties to pay the official documents are very detailed, that is, the need to carefully deal with details and attention to parameters, micro-credit payment can not use debug, because to verify the signature. Otherwise the payment is unsuccessful.

Complete Demo:https://github.com/bobomee/thirdpay

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.

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.