1. The first step is to ignore the application process of Wechat platform.
(N words are omitted here)
2. Personally, I just finished the Wechat Payment. I had a day and tried three different versions of Wechat Payment.
The result is that they are not very useful... Maybe because they have made corresponding changes according to their own preferences, I think I should stand up and write a blog that pays for the unmodified version of micro-mail.
So the following code goes through.
3. In fact, the most important part of Wechat payment is signature.
The package name applied for on the Wechat Open Platform, etc.... Must be exactly the same as your local signature, exactly the same (important thing to say three times)
My humble person made the payment by Wechat this time, which was also wrong in signing. Please be more careful.
4. Time to start coding
(1) The way I use here is to guide the libs package.
Brick friends in need, please move Wechat Developer Platform Brick friends, go grab your SDK
(2) Guifen, the code is good.~
// Wake Up Membership Variables of Wechat Payment
private IWXAPI api;
// Two lines of code to wake up Wechat Payment
api = WXAPIFactory.createWXAPI(this, null); // Register the app to Wechat api.registerApp("Own AppId");
// The parameters that must be transmitted to wake up the Wechat
PayReq request = new PayReq(); //Call up the object of Wechat APP // The following is to set the necessary parameters, that is, the parameters mentioned above. Where do these parameters come from? request.appId = appId ; request.partnerId = partnerId; request.prepayId = prepayId; request.packageValue =packageValue; request.nonceStr = nonceStr; request.timeStamp = timeStamp; request.sign = sign; api.sendReq(request);
// Create a folder wxapi under the root directory of the package name (root directory, root directory important thing to say three times)
// Creating a WXPayEntry Activity requires the implementation of the IWXAPIEventHandler interface
What actions did the user do to return
// The code in this class is the same. Brick friends can bring it up by themselves.
The article recommended today is:
private IWXAPI api; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.pay_result); api = WXAPIFactory.createWXAPI(this, "Own AppId"); 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) { Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show(); } //Payment result @Override public void onResp(BaseResp resp) { if (TextUtils.isEmpty(resp.openId)) { Log.i("xml", "Nothing"); } if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { // AlertDialog.Builder builder = new AlertDialog.Builder(this); //builder.setTitle("Wechat Payment Tips"); switch (resp.errCode) { case 0://Payment success h Toast.makeText(getApplicationContext(), "Successful payment", Toast.LENGTH_SHORT).show();//builder.setMessage("Successful Payment"); break; case -1://Payment failure, usually backend signature failure, etc. Toast.makeText(getApplicationContext(), "Failure to pay"+resp.errCode, Toast.LENGTH_SHORT).show(); // builder.setMessage("Payment failed, please clean up the Wechat Cache or reinstall it"); break; case -2://The user cancelled the payment Toast.makeText(getApplicationContext(), "Payment cancelled.", Toast.LENGTH_SHORT).show(); //builder.setMessage("Payment cancelled"); break; } // builder.show(); } finish(); }
You also need to configure it in the manifest file
<activity android:name=".wxapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop"/>
Here we are. Wechat payment is finished. Is it simple?
Still want to remind your friends: signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature, signature.... (Important things say three times, not N times)
Want to know more interesting blog posts?
Today's recommendation is:
Android about memory leaks and optimization:
https://blog.csdn.net/as89751/article/details/82377436