10 minutes access to wo+ capacity sharing platform

Source: Internet
Author: User
Tags response code

based on the woplus.sdk of open source, this paper briefly introduces the development process of access wo+ capability sharing platform, taking the basic "Billing Capacity 2.0 Edition" as an example.


First, preparation (3 minutes)


Before you can access the technology, you need to register as a developer in http://open.wo.com.cn and create the app. The process of registering as a developer I'm not going to talk about it, just the process of creating an application, and how to get the parameters required for several technology accesses.


Login to the wo+ competency platform, in the "Home-developer-my app-web server app", click on the + sign to create a web app. When you create an app, you must fill in the fields such as where the app name will appear in the billing text message and send it to the user, so use the real app name. After that, don't forget to select the API package and select the "Billing Capability version 2.0" that this example needs to use.

If you do not find this ability in the pop-up selection box, you have not signed it yet, please find this ability in the mall and sign the contract (the competency is free).


After that, you can get the following two parameters: AppKey (i.e. app id) and Appsecret (i.e. app key).



Then, you need to submit your app for approval, and once you get through, you can start getting into the Technology access section.



Second, the development environment


"Must", the access discussed in this article is in Java as an example, so you should have a Java development environment.

"Must", WOPLUS.SDK is a MAVEN-based project, so you need to install the MAVEN environment.

"Optional", WOPLUS.SDK is a git project hosted in Oschina, so it is recommended that you install the GIT environment.


Access http://git.oschina.net/sharetop/, where WoPlus.SDK.Java is a development package implemented using HttpClient, while WoPlus.SDK.Spring is a development package for the spring environment. You can choose the right SDK based on your project.


This article uses the development of spring as an example. After obtaining the source code for WoPlus.SDK.Spring, use MAVEN to compile and install.


Third, Access code development (5 minutes)


Now get to the point, open Eclipse, and use Maven to create a webapp based on the spring framework. The name of the app created in this example is: Spring-web.


1) Modify MAVEN configuration file Pom.xml, add Spring Support package (this article, refer to the example project in the SDK), and add WoPlus.SDK.Spring support package.

<dependency><groupId>com.fasterxml.jackson.core</groupId>  <artifactid>jackson-core </artifactId>  <version>2.4.3</version></dependency><dependency><groupid >com.fasterxml.jackson.core</groupId>  <artifactId>jackson-databind</artifactId>  <version>2.4.3</version></dependency><dependency><groupId> Cn.chinaunicom.woplus.openapi.spring</groupid>  <artifactId>open.spring</artifactId> < Version>0.0.1</version></dependency>


2) Add WOPLUS.SDK assembly items to the applicationcontext.xml of the spring frame. Where parameter 0 is appkey, and parameter 1 is appsecret. (These two parameters are actually modified according to your application)

<bean id= "woplusclient" class= "cn.chinaunicom.woplus.openapi.spring.WoPlusClient" ><constructor-arg Index = "0" value= "492edbd25e65c7fc6113544e86e9f0d87b9eaae9"/><constructor-arg index= "1" value= " 53f7862c8951bb9f14f229daa8a3b88b06b947e1 "/></bean>


3) You can now call wo+ 's capability interface in your code. Take a look at our sample code, first of all the control class Democontroller in spring, you need to use the auto-assemble woplusclient, and all access to the wo+ interface is done through this class.

@AutowiredWoPlusClient woplusclient; @RequestMapping (value= "/api/paymentcodesms", Method=requestmethod.get, headers = {"Accept=application/json"}) public @ResponseBody woplusresponse paymentcodesms (httpservletrequest request, @RequestParam ("mobile") String mobile) {...} @RequestMapping (value= "/api/apppayment", Method=requestmethod.get,headers = {"Accept=application/json"}) public @ Responsebody woplusresponse apppayment (httpservletrequest request, @RequestParam ("mobile") String mobile,@ Requestparam ("Vcode") String Vcode) {...}


"Billing Capacity 2.0 version" is required two confirmation, so we build two URL requests, respectively, "Get Verification Code" and "confirmation deduction" two steps. This is the standard code of spring, the meaning is not elaborate. Below, is the time to witness miracles.


Look at the first interface to get the verification code. The parameters required to obtain the verification code according to the API documentation include: the phone number (Paymentuser) for the verification code to be received, the billing type (paymenttype) must be 0, the order number (Outtradeno), and the Payment account type (Paymentacount) must be "001 ", Commodity name (subject) and deduction amount (Totalfee). Other options are not required.


It is important to note the type, number, or string of individual parameters. Also note that this interface does not have to be signed.

@RequestMapping (/* See previous code example */) public @ResponseBody woplusresponse paymentcodesms (/* See previous code example */) {Woplusresponse resp = New Woplusresponse (); String api_url= "https://open.wo.com.cn/openapi/rpc/paymentcodesms/v2.0"; hashmap<string,object> params = new hashmap<string,object> (); Long num=new Random (). Nextlong ();p arams.put ("Paymentuser", mobile);p arams.put ("Paymenttype", 0);p arams.put ("Outtradeno", long.tostring (num));p arams.put (" Paymentacount "," 001 ");p arams.put (" subject "," Coin pile ");p arams.put (" Totalfee ", 0.01f); try {resp = woplusclient.post (API _url, Params,false), if (Resp.resultCode.equals ("0")) {request.getsession (). SetAttribute ("Paymentcodesms_param", params);}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Return resp;}






The code is simple, Api_url is the API interface address to access, and the params is a hashmap, which is our collection of parameters. Regardless of the interface, and no matter how the parameters change, the thing we have to do is to set the correct api_url and fill in the parameters in the params. Then, submit the request with the Post method in Woplusclient.

Where the last parameter in resp = Woplusclient.post (api_url,params,false) means that no signature is required. Also, because these parameters are required when confirming the payment, we will save these parameters in the session once the submission is successful, for later use.


Resp is an woplusresponse instance in which the ResultCode encapsulates the interface's response code. We can process the result of the interface call based on this response code.


Next, we add Apppayment's function body code, the thing to do here is to construct the corresponding parameter set, and call another billing interface "pay-per-class interface".

@RequestMapping (/* See previous code example */) public @ResponseBody woplusresponse apppayment (/* See previous code example */) {Woplusresponse resp = new Woplusresponse (); String api_url= "https://open.wo.com.cn/openapi/rpc/apppayment/v2.0"; @SuppressWarnings ("Unchecked") hashmap< string,object> params = (hashmap<string,object>) (Request.getsession (). getattribute ("Paymentcodesms_param "), if (!params.get (" Paymentuser "). Equals (mobile)) return Resp;params.put (" Paymentcodesms ", Long.decode (Vcode). Longvalue ());p arams.put ("TimeStamp", New SimpleDateFormat ("YYYYMMDDHHMMSS"). Format (new Date ()));p Arams.remove (" Paymenttype "); try {resp = woplusclient.post (Api_url, params);} catch (Exception e) {//TODO auto-generated catch BLOCKE.PR Intstacktrace ();} Return resp;}


The code is similar to the previous function by removing the params parameter set from the session, supplementing two parameters paymentcodesms and timestamp, and removing the Paymenttype parameter. The same woplusclient post method, at this time, requires a signature, so only two parameters Api_url and params (the Post method is signed by default).

Next, we implement the call to Democontroller. I chose Bootstrap as a Web style in the example, using jquery as a front-end script to make asynchronous calls to the controller.


You can choose other technical methods to implement the call to Democontroller, so I'm not going to go into that. The basic code reference in the SDK package to the source.

$ ("#paymentcodesms"). Click (function () {var mobilestr=$ ("#mobile"). Val (); $.ajax ({url: ' api/paymentcodesms?mobile= ') +mobilestr,type: ' Get ', ContentType: ' Application/json;charset=utf-8 ', datatype: ' JSON ', success:function (data) {var Str=json.stringify (data.content); $ ("#resultDesc"). Val (str);});}); $ ("#apppayment"). Click (function () {var mobilestr=$ ("#mobile"). Val (), Var vcode=$ ("#vcode"). Val (); $.ajax ({url: ' api/ Apppayment?mobile= ' +mobilestr+ "&vcode=" +vcode,type: ' Get ', ContentType: ' Application/json;charset=utf-8 ', DataType: ' JSON ', success:function (data) {var str=json.stringify (data.content); $ ("#resultDesc"). Val (str);});

All code development is complete.




Four, commissioning (2 minutes)


Let's take a look at the execution effect, start the WebApp application first, and we'll see a page like this:



Enter your phone number, click "Send Verification Code", your phone will receive a 6-digit verification code, in the box below to fill in the Verification code, click on the "Confirm payment", at this time, your phone will receive payment success notification SMS, then, congratulations, you have successfully access to the wo+ ability sharing platform, and completed a small payment operation.







10 minutes access to wo+ capacity sharing platform

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.