1. Wechat payment
Play Naughty
Download or add dependencies first, Wechat lib or dependency , and then add it in Android manifest. Remember to build wxapi in the root directory. The name can't be changed. Remember to have the payment authority after applying for wechat payment account. Besides, wechat signature may need to be lowercase and remove the colon, which may lead to unsuccessful payment due to the big write and colon. Wechat payment must be tested with the packaged signature, not packaged The signature of cannot be crossed
<!-- ***************************************WeChat Start********************************************** --> <!-- Wechat sharing --> <activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar"/> <!-- Wechat payment --> <activity android:name=".wxapi.WXPayEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar"/> <!-- ************************************WeChat End************************************************* -->
The wechat payment code in Activity is
/** * Wechat payment and data are all returned in the background */ private void weChatPay(PayOrderBean bean) { IWXAPI mApi = WXAPIFactory.createWXAPI(FillinOrderActivity.this, null); if (mApi.isWXAppInstalled()) { mApi.registerApp(WXPayEntryActivity.APP_ID); PayReq req = new PayReq(); req.appId = bean.getAppid(); req.partnerId = bean.getPartnerid(); req.prepayId = bean.getPrepayid(); req.nonceStr = bean.getNoncestr(); req.timeStamp = bean.getTimestamp(); req.packageValue = bean.getPackages(); req.sign = bean.getSign(); //The value of the extra value can be used to judge whether it comes from the top up or the order req.extData = WXPayEntryActivity.WXPAY_ORDER_PAY; mApi.sendReq(req); } else { showToast("Please install wechat"); } }
The code in the wechat payment callback interface is
** * Wechat payment callback succeeded * * @author WangZhiHui * @date 2018/6/11 */ public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler { public static final String APP_ID = "WeChat AppId"; private IWXAPI mApi; private final String EXT_DATA_KEY = "_wxapi_payresp_extdata"; /** * Recharge payment */ public static final String WXPAY_TOP_UP_PAY = "WXPAY_TOP_UP_PAY"; /** * Order payment */ public static final String WXPAY_ORDER_PAY = "WXPAY_ORDER_PAY"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onStart() { mApi = WXAPIFactory.createWXAPI(this, WXPayEntryActivity.APP_ID); mApi.handleIntent(getIntent(), this); super.onStart(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); mApi.handleIntent(intent, this); } @Override public void onReq(BaseReq baseReq) { } @Override public void onResp(BaseResp resp) { String extData = getIntent().getStringExtra(EXT_DATA_KEY); switch (resp.errCode) { /*Successful payment*/ case BaseResp.ErrCode.ERR_OK: showToast("Successful payment"); // Wechat payment success event callback, EventBus is used here EventBus.getDefault().post(WeChatPaySucEvent(extData); break; /*Payment cancellation*/ case BaseResp.ErrCode.ERR_USER_CANCEL: showToast("Payment cancellation"); break; /*Payment authorization denied*/ case BaseResp.ErrCode.ERR_AUTH_DENIED: showToast("Payment authorization denied"); break; /*Failure to pay*/ default: showToast("Failure to pay"); break; } WXPayEntryActivity.this.finish(); } private void showToast(String msg) { ToastUitl.showShort(WXPayEntryActivity.this, msg); } }
If wechat payment is successful, you can use EventBus to know that wechat payment is successful and skip the payment success interface, Use of EventBus Is it very simple
2, Alipay payment
Alipay also wants to download related lib or add dependency. Alipay lib And then add it to the Android manifest
<!-- ***********************************payment Start************************************************** --> <!-- Alipay --> <activity android:name="com.alipay.sdk.app.H5PayActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden"/> <activity android:name="com.alipay.sdk.app.H5AuthActivity" android:configChanges="orientation|keyboardHidden|navigation" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden"/> <!-- *************************************payment End************************************************ -->
Here is the Activity code
/*Alipay payment result processing*/ private AliPayHandler mAliPayHandler; /** * Alipay payment */ private void aliPay(final PayOrderBean bean) { mAliPayHandler = new AliPayHandler(FillinOrderActivity.this); ThreadPoolUtil.getInstance().execute(new Runnable() { @Override public void run() { PayTask payTask = new PayTask(FillinOrderActivity.this); Map<String, String> map = payTask.payV2("Background information returned by Alipay, true); Message msg = mAliPayHandler.obtainMessage(); msg.obj = map; mAliPayHandler.sendMessage(msg); } }); } /** * Alipay payment result processing */ public static class AliPayHandler extends Handler { private WeakReference<FillinOrderActivity> weakReference; AliPayHandler(FillinOrderActivity activity) { weakReference = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); Map<String, String> map = (Map<String, String>) msg.obj; AliPayResultBean resultBean = new AliPayResultBean(map); if (resultBean.isSuccess()) { FillinOrderActivity activity = weakReference.get(); if (null != activity) { activity.showToast("Successful payment"); // Payment succeeded, jump to payment succeeded interface activity.paySuccess(); } } else { FillinOrderActivity activity = weakReference.get(); if (null != activity) { activity.showToast(resultBean.getPromptText()); } } } } /** * Successful payment */ private void paySuccess() { // Some successful operations of payment finish(); }
Now that mobile payment has been completed, remind me again that wechat payment must be tested with a packaged signature. A signature that is not packaged cannot be paid successfully