Android App Payment Series (i): A detailed guide to micro-credit payment access (with official payment demo) _android

Source: Internet
Author: User
Tags error code response code


It's written in front.



A mobile internet company, in the final analysis, to make a profit always need to pay users, it is clearly unwise to develop payment system, the domestic already has a number of mature mobile payment providers, Tencent is one of them. Carding the next micro-letter payment of access, today to share under the Tencent's micro-letter Payment SDK Access process.
Access process



1. Application Developer Qualification



Address: https://open.weixin.qq.com/



Use the company manager/High level account login micro-letter open platform, enter the "Account Center", the developer qualification certification, need to fill in the company information, including but not limited to, the company registration number, the company's business license, the company's external office telephone, the company's public bank card information (card number, The audit time is about one week.



Note: Since October 1, 2015, the State to implement three certificates (Organization code card, business license, Tax registration Certificate) in unity, so the organization code to fill-business license registration number, the same, organization code card, upload-enterprise business license.



NOTE 2: The developer qualification certification needs to pay 300 yuan/year, only developers with the certification of developers can use the app payment, authorization login and other interfaces.



2. Apply for App_id/app_key



Each application/game to invoke the micro-letter interface needs to have a micro-beacon, this unique symbol usually becomes app_id or App_key, the open platform is not very different
Enter the management center, create mobile applications, each developer has 10 applications to create the opportunity, the creation of the application can be deleted at any time. has been on-line application does not suggest the hand cheap deletion!!



Apply AppID need to fill in application information: Application name, package name, signature (keystore MD5 value to semicolon lowercase), icon (28*28 & 108*108), app download address and other information can be assigned to a AppID.
Note: Test payment, be sure to use the application when the KeyStore file signed, the package name also need to check clearly, must not be adjusted to pay, return-1 error code.



3. Ability to apply for payment



In central management, look at the apps that need to be integrated to pay, find "micro-letter Payment" column, click on the right "application open", fill out some enterprise information waiting for audit, audit time for a week or so, after the audit, will get a business merchant number and password, the public bank card would receive a few cents, into the merchant platform, Enter the amount of money received to verify. The integration payment call can begin after validation has passed. Prior to this, the call to the payment interface was unable to complete the payment.



4. Code integration micro-credit payment



Payment flowchart



Micro-letter Payment Flowchart Vernacular version:



1. Client code gets the product information that the user buys, passes it to own company app server, the parameter contains but not limited to the following:


Params.put ("AppID", AppID);//micro-letter AppID, selective upload, the server can be written to death
Params.put ("Money", cash);//Payment amount, Unit: Min
params.put (" Name ", goodsname);//
Params.put (" Currencytype "," CNY ")//payment currency, only CNY, on behalf of RMB


Note: The above is the reference number segment is our company server definition, to the server to obtain the pre-payment order number Prepayid.



Note 2: Detailed fields Please refer to: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1



2.app Server calls micro-letter "Unified under single" interface, get Prepayid order number and return Prepayid to mobile phone client;



3. The handset client uses the Prepayid and the commodity information to adjust the micro-letter client to pay;



3.1 User operation: input password for payment, return key cancellation payment, network no connection payment failure, etc.



4. Micro-Credit Client callback payment results to our app client;



5. Micro-trust server to notify us asynchronously the Company app server payment results (server work, not client-side)



Micro-Credit Payment code:


IWXAPI mWxApi = WXAPIFactory.createWXAPI (mContext, WX_APPID, true);
mWxApi.registerApp (WX_APPID);
      / **
       * Request the callback result obtained by the app server
       * /
      @Override
      public void onGet (JSONObject jsonObject) {
        if (mWxApi! = null) {
          PayReq req = new PayReq ();

          req.appId = WX_APPID; // AppID approved by WeChat open platform
          try {
            req.partnerId = jsonObject.getString ("partnerid"); // The merchant number assigned by WeChat payment
            req.prepayId = jsonObject.getString ("prepayid"); // Prepayment order number, the app server calls the "unified order" interface to get
            req.nonceStr = jsonObject.getString ("noncestr"); // A random string, no longer than 32 bits, the server brother will generate for us
            req.timeStamp = jsonObject.getString ("timestamp"); // Time stamp, given by the app server brother
            req.packageValue = jsonObject.getString ("package"); // Fixed value Sign = WXPay, can be written directly, the server also returns this fixed value
            req.sign = jsonObject.getString ("sign"); // The signature is given by the little brother of the server, and he will be based on: ? chapter = 4_3 guide to get this
          } catch (JSONException e) {
            e.printStackTrace ();
          }
          mWxApi.sendReq (req);
          Log.d ("Initiating a WeChat Payment Application");
        }

      } 


Under the app package, create a new WXAPI package, such as the app package named Com.xiongit.app, the new package path is COM.XIONGIT.APP.WXAPI, the package under the new wxpayentryactivity, inherited from arbitrary activity. The activity is used to receive the payment result callback, and the manifest declares that the activity is in the format


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


Wxpayentryactivity code Example


public class Wxpayentryactivity extends activity implements iwxapieventhandler{

  private static final String TAG = "Mic RoMsg.SDKSample.WXPayEntryActivity ";

  Private IWXAPI API;
  private static final String app_id = "Your APP ID";

  @Override public
  void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.pay_result);

    API = Wxapifactory.createwxapi (this, app_id);
    Api.handleintent (Getintent (), this);
  }

  @Override
  protected void onnewintent (Intent Intent) {
    super.onnewintent (Intent);
    Setintent (intent);
    Api.handleintent (Intent, this);
  }

  @Override public
  void Onreq (Basereq req) {
  }

  /**
   * Get payment result callback
   /
  @Override
  Public void Onresp (Baseresp resp) {
    log.d (TAG, "onpayfinish, Errcode =" + Resp.errcode);//Payment result code
  }
}


List of Errcode values in callback:



0 Payment Success



-1 possible causes of errors: signature errors, unregistered AppID, incorrect project settings AppID, registered AppID and settings mismatch, other exceptions, and so on.



-2 User Cancellation scenario: User not paid, click Cancel, return app.



List Menifest the required permissions


 <uses-permission android:name= "Android.permission.INTERNET"/>
  <uses-permission android:name= " Android.permission.MODIFY_AUDIO_SETTINGS "/>
  <uses-permission android:name=" android.permission.WRITE_ External_storage "/>


Note: The payment result is ultimately based on the asynchronous notification received by the app server.



Micro-Letter SDK Obfuscation


-keep class com.tencent.mm.sdk.** {
  *;
}


Update 20160704



When paid, the micro-letter is not yet logged in, without any callback, if you have a pop-up frame when you pay, and you want to make it disappear after you get the response code, you'll need to process the progress bar in the OnStop, or you won't be logged in and the progress bar won't disappear when the user cancels the payment.



Update 20160722



When paid, the wxpayentryactivity will start in a transparent manner, so you may need to manually finish the activity after payment has been paid, otherwise you will need to return two times for the activity you wrote to the payment page to exit. Because the first time you press the Back button is actually finish off wxpayentryactivity. The second time is your own activity.



Finished, I wish you the integration of micro-letter payment Success!



Finally attach the micro-letter official Payment Demo Download: Https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1, Spit slot: Official demo download hidden a bit deep ... The open platform incredibly does not provide downloads, placed on the micro-letter payment merchant platform.


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.