Android and micro-channel access Alipay payment

Aar then downloaded package, project into the directory libs directory, the following dependence come gradle

// Alipay SDK AAR package configuration required
the compile (name: 'alipaySdk-15.6.0-20190226104104-noUtdid', EXT: 'AAR')
1
2
call the method of initiating the payment Alipay SDK
call Alipay SDK initiate a payment, only a parameter is returned from the server order information. So here is the first to pay the order we call the service side of the interface to create an order, the server then returns the order information to us, we take this APP order information to call Alipay.

// The following orderInfo that we own the server returns the order information, which in addition, there are signatures and other security information such as order ID
// use in accordance with the basic Alipay DEMO on the line inside the

final Runnable payRunnable = new Runnable () {

@Override
public void run() {
PayTask alipay = new PayTask(PayDemoActivity.this);
Map<String, String> result = alipay.payV2(orderInfo, true);
Log.i("msp", result.toString());

Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};

// must asynchronous call
the Thread payThread the Thread new new = (payRunnable);
payThread.start ();
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
seen from the above call, by Alipay message mechanism to receive callbacks, so we have to receive callbacks information Handler message.
Handler Handler mHandler = new new Private () {
@SuppressWarnings ( "unused")
public void handleMessage (the Message msg) {
Switch (msg.what) {
Case SDK_PAY_FLAG: {
// Alipay here to receive callback information
// Note that, pay their results be sure to call the service side is determined, not by Alipay callback result to determine
BREAK;
}
default:
BREAK;
}
};
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
It should be noted, the result of payment must be invoked to determine its own server, not through the payment callback determination result!

Other
practical situations where users need to be considered on the phone had not installed Alipay

Alipay already installed, will be called directly Alipay
not installed Alipay, Alipay will transfer from the H5 page payment
above that Alipay access the step is relatively simple, there is no pit. The following micro-channel will have to pay a pit ...

Micro-channel pay access
Access SDK micro-channel
official website at this

Compared to Alipay, micro-channel access SDK is relatively simple, get a line of code

// micro-channel pay the SDK
the compile 'com.tencent.mm.opensdk: WeChat Android-SDK-with-MTA-: +'
. 1
2
It should be noted that, SDK inside the micro-channel payment and other social login These are integrated together , can not be separated. So if you have been previously integrated micro-projects in the letter sign in, then you need to repeat integrate!

Call micro-channel launch SDK pay
String content = ... (This is the information returned by the service order)
// required to pay a registration APPID micro letter of
IWXAPI API = WXAPIFactory.createWXAPI (mContext, APPID);
JSONObject json = new new JSONObject (Content) ;
PayReq new new PayReq REQ = ();
req.appId = json.getString ( "AppID");
req.partnerId = json.getString ( "partnerid");
req.prepayId = json.getString ( "prepayid");
REQ. = json.getString nonceStr ( "noncestr");
req.timeStamp = json.getString ( "timestamp");
req.packageValue json.getString = ( "Package");
req.sign = json.getString ( "Sign");
api.sendReq (req);// here initiates a call to the micro-channel paid
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
received micro-channel payment callbacks
can be seen above code and Alipay not the same, with no Handler.

Micro-channel pay more special place is the information we need to use a special Activity to receive callbacks. This process is not very easy to encounter the situation can not receive callbacks.

We need to create a named WXPayEntryActivity of Activity, you can check out the contents of micro-channel inside the SDK. There are several points to note:

Activity WXPayEntryActivity this path to meet the requirements, such as APP package name is com.niubi.company, then this Activity path needs to be com.niubi.company.wxapi.WXPayEntryActivity
this WXPayEntryActivity of course need to register AndroidManifest file
<Activity
Android : name = "wxapi.WXPayEntryActivity."
Android: = exported "to true"
Android: launchMode = "singleTop" />
. 1
2
. 3
. 4
particular note that the above properties and exported launchMode properties must be added, otherwise it is not received to callback

After we complete the payment, usually we want to return to our own applications directly inside. This time we need to make the above WXPayEntryActivity not displayed, otherwise it will flash or stay in this dark page.
Words do not appear to pay attention to 2:00, it is not to have a layout, and the other is to timely finish off this Activity
public class WXPayEntryActivity the implements IWXAPIEventHandler the extends Activity {

...

@Override
public void onResp(BaseResp resp) {
Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

IF (resp.getType () == ConstantsAPI.COMMAND_PAY_BY_WX) {
<- AlertDialog.Builder Builder = new new AlertDialog.Builder (the this);! ->
! <- builder.setTitle (R.string.app_tip); - ->
<- builder.setMessage (getString (R.string.pay_result_callback_msg, String.valueOf (resp.errCode)));! ->
! <- builder.show (); ->
// certainly not here DEMO is like the above as a pop-up dialog, but let us pay for calls initiated by page
// then promptly finish off this page, posted a pseudo-code:
sendPayNotice ()
finish ();
}
}
}
1
2
3
4
5
6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
another
Similarly, the success of micro-channel pay, but also to view by calling their server, but can not rely on the callback state micro-letter, to remember this.
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/11069834.html