It can be said that Alipay payment access is the best access among all SDKs, and there is no one.
The client doesn't need to sign, don't care about the package name or signature file, just return the order on the interface and give it to Alipay SDK for calling. Success or failure will be returned to you in the current interface. Please inform the interface again.
Payment flow chart
Official document address
! Alipay payment document address
Access the SDK and related configuration according to the document, so I won't go into details here.
Client payment key code = = = Call payment interface (adjust payment box)
Remember that the call to the payment interface must be executed in an independent non-ui thread, that is, it needs to be called in a new thread. You can use the new threading method just like the official demonstration.
Below I give an example code in an observable way.
At PayUtils.
/**
* desc: AliPay payment
* Created by Cong Ge on 20 18/8/27 17:20.
Orders returned by * @param orderInfo interface
**/
public static void aliPay(final Activity Activity,final String orderInfo,final order listener order listener){
Observable.just(orderInfo)
. Map (new function () {
@ Overlay
The public string application (string order information) threw an exception {
//If the user clicks Pay in the merchant app, do you need a load as a transition before the wallet is aroused? The value is set to true.
Return to a new payment task (activity). pay(orderInfo,true);
}
})
. subscribeOn(Schedulers.io())
. observer ion(androidschedulers . main thread())
. Subscription (new consumer () {
@ Overlay
The public void accept (string payResult) throws an exception {
order listener . onpayresult(pay result);
}
});
}
Payment result return processing
Back to the example:
Result Status = {9000}; memo = { }; result = { { " AliPay _ trade _ app _ pay _ response ":{ " code ":" 100000 "," msg":"Success ", " app _ id ":" 20 1609 1300503896 "," auth _ app _ id ":" 20 1609 13000503896 "," charset":"utf-8 "," timestamp "
You can also record it yourself.
Processing sample code:
//Alipay payment
PayUtils.aliPay(this,result.getSignDataStr(),new PayUtils。 OrderListener() {
@ Overlay
public void onPayResult(String pay result){
PayResult pr = new payresult (payment result);
string RS = pr . getresultstatus();
string r = pr . get result();
Switch (RS)
Case AliPayResultStatus. Payment succeeded:
toastutils . show(r . string . pay _ success);
//Notify the interface that the payment was successful.
Break;
Case AliPayResultStatus. Payment _ Processing:
Case AliPayResultStatus. Payment _ unknown:
toastutils . show(r . string . pay _ fail);
//Payment may be successful, and it needs to be checked through the interface.
Break;
Default value:
toastutils . show(r . string . pay _ fail);
//Notify the interface that payment failed and cancel the order.
}
}
});
In the above method:
//Notify the interface that payment was successful//Payment may be successful, and you need to query the interface//Notify the interface that payment failed, and cancel the order. According to your product requirements, should I inform you of the operation of the server? Normal is a necessary condition to change the order status.
PayResult.class
Public class payment result {
Private string resultStatus
Private string result;
Private string memo;
Public payment result (string rawresult) (
if (TextUtils.isEmpty(rawResult))
Return;
string[]result params = raw result . split(";" );
for(String result param:result params){
if(result param . starts with(" result status "){
result status = gat value(result param," result status ");
}
if(result param . starts with(" result "){
result = gatValue(resultParam," result ");
}
if(result param . starts with(" memo "){
memo = gatValue(resultParam," memo ");
}
}
}
@ Overlay
Common string toString() {
Returns "ResultStatus = {"+ResultStatus+"}; Memo = {"+Memo
+ "}; result = { "+result+" } ";
}
Private string gatValue (string content, string key) (
String prefix = key+" = {";;
Returns content.substring (content.indexof (prefix)+prefix.length (),
content . lastindexof(" } ");
}
Public string outOrder() {
String order = " " out _ trade _ no
if (result.contains(order)) {
string begin = result . substring(result . index of(order));
String ss = begin.split(","[0];
String newS = ss.replace(" ","")
. Replace ("}", "")
. Replace (":","")
. replace("out_trade_no "," ");
Try {
Return message;
Catch (exception e) {
e . printstacktrace();
}
}
Return'';
}
/**
* @ Returns the result status
*/
Public string getResultStatus() {
Return the result status;
}
/**
* @ Return memo
*/
Public string getMemo() {
Return the memo;
}
/**
* @ Return the result
*/
Common string getResult() {
Return the result;
}}
Finally, return the watch.
AliPayResultStatus.class
Public class AliPayResultStatus {
/**
* Order payment is successful, and the only thing that must be successful is payment.
*/
public static final String PAY _ SUCCESS = " 9000 ";
/**
* Being processed, the payment result is unknown (payment may have been successful). Please check the payment status of the order in the merchant order list.
*/
public static final String PAY _ PROCESSING = " 8000 ";
/**
* Order payment failed.
*/
public static final String PAY _ FAIL = " 4000 ";
/**
* Duplicate request
*/
public static final String PAY _ REPEAT = " 5000 ";
/**
* The user cancels midway.
*/
public static final String PAY _ PROCESS _ CANCEL = " 600 1 ";
/**
* Network connection error
*/
Public static final string PAY _ NET _ ERROR = "6002
/**
* The payment result is unknown (payment may have been successful). Please check the payment status of the order in the merchant order list.
*/
public static final String PAY _ UNKNOWN = " 6004 "; }
There is also a debugging mode that directly abandons the sandbox, otherwise you may not even know what the fault is when the payment fails. Afraid of a large amount, consult the interface and test the server with 0.0 1.