Android developers should have their own APP released to the market, can improve their own technology station, add advertising, and can have a small income.

But in this day and age, individual app markets are not welcoming individual developers with AD SDKS. Fortunately, we can use plug-in technology to add the advertising SDK, or directly think of a way to achieve the tipping function, so that users can transfer money to their accounts through Alipay or wechat.

Achieve wechat/Alipay transfer and reward function without SDK

Android individual developers if they want to implement the payment tip function in the App, the embedded SDK will not be able to pass the market approval, except the cost. So we can use limited resources, packaging components to achieve a mini type of tipping function.

I. Alipay transfer analysis

Alipay transfer and payment function, can open the relevant payment page through the TWO-DIMENSIONAL code, and scan with the browser can also call the normal payment page. So we simple analysis, Alipay’s QR code the whole jump certainly does not need the relevant integration of SDK, so what data format can be done?

In the Personal information page of Alipay, find my QR code, take a screenshot of my own Alipay QR code, and get the following information after identification:

Qr code online analysis website zhabu

You can see that the payment QR code of Alipay is a common HTTP URL, and then its main information exists in the suffix character: APAFm3KP91DF7YO517.

https://qr.alipay.com/apafm3kp91df7yo517

After searching online, I found that as expected, I could open the Alipay transfer page through Scheme. reference

I immediately implemented it to verify that the Scheme approach was valid. All that remains is how to encapsulate the next aspect call.

2. Analysis of wechat transfer

With alipay transfer experience, we also started from the TWO-DIMENSIONAL code information, and found that with the version update of wechat, there will be a lot of verification, directly using Scheme to dial wechat, wechat will open a blank page.

Analysis of the

Then we studied the wechat pay SDK, and found that the time of interface invocation requires merchant ID, appSecret and other information verification, which means that it is impossible for us to call wechat pay without any identity information. Then we settle for the next best thing, with the guide way to help users directly open wechat scan the page, copywriting guide users to open the QR code from the album, and then pay.

The next step is to find a way to open wechat and scan.

    private void toWeChatScan() {try {// Open wechat Uri with Intent Uri = uri.parse ()"weixin://dl/scan"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } the catch (Exception e) {/ / if not normal jump, error handling in the Toast. The makeText (DinpayWeChatActivity. This,"Cannot jump to wechat, please check if you have wechat installed!", Toast.LENGTH_SHORT).show(); }}Copy the code

These are the recommended methods on the Internet, so unfortunately, this method is estimated to work in the earlier version, but this method is not effective now.

So is there no method in the high version? Continue to Google and find that the following works:

  /*package*/ static void startWechatScan(Context c) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.tencent.mm"."com.tencent.mm.ui.LauncherUI"));
        intent.putExtra("LauncherUI.From.Scaner.Shortcut".true);
        intent.setFlags(335544320);
        intent.setAction("android.intent.action.VIEW");
    
        if (MiniPayUtils.isActivityAvailable(c, intent)) {
            c.startActivity(intent);
        } else {
            Toast.makeText(c, "Wechat is not installed ~", Toast.LENGTH_SHORT).show(); }}Copy the code

Qr code wechat photo album saved

After the realization of wechat scan jump, when the user selects the QR code from the album, how can we select our target transfer QR code at a glance?

It is observed that the TWO-DIMENSIONAL code selection album of wechat is sorted in chronological order, so as long as our picture generation time is the latest can be ranked in the first place, every time a new screenshot can be saved.

The logic for generating related pictures and saving albums is as follows:

    /*package*/
    static void startWeZhi(Context c, View view) {
        File dir = c.getExternalFilesDir("pay_img");
        if(dir ! = null && ! dir.exists() && ! dir.mkdirs()) {return;
        } else {
            File[] f = dir.listFiles();
            for(File file : f) { file.delete(); // Delete old screenshots and use new screenshots each time to ensure sorting of albums. } } String fileName = System.currentTimeMillis() +"weixin_qa.png";
        File file = new File(dir, fileName);
        if(! file.exists()) { file.delete(); } snapShot(c, file, view); startWechat(c); } private static void snapShot(Context Context, @nonnull File File, @NonNull View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap); view.draw(canvas); FileOutputStream fos = null; boolean isSuccess =false; try { fos = new FileOutputStream(file); / / IO flow ways to save the file compression isSuccess = bitmap.com press (bitmap.com pressFormat. PNG, 80, fos); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { MiniPayUtils.closeIO(fos); }if (isSuccess) {
            ContentResolver contentResolver = context.getContentResolver();
            ContentValues values = new ContentValues(4);
            values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
            values.put(MediaStore.Images.Media.ORIENTATION, 0);
            values.put(MediaStore.Images.Media.TITLE, "Donation");
            values.put(MediaStore.Images.Media.DESCRIPTION, "Qr Code for Donation"); values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); values.put(MediaStore.Images.Media.DATE_MODIFIED,System.currentTimeMillis()/1000); Uri url = null; try { url = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); / / the essence of which is to return to the Image. Meida. DATA Image path in the path of change and the uri of the OutputStream imageOut = contentResolver. OpenOutputStream (url); try { bitmap.compress(Bitmap.CompressFormat.PNG, 100, imageOut); } finally { MiniPayUtils.closeIO(imageOut); } long id = ContentUris.parseId(url); MediaStore.Images.Thumbnails.getThumbnail(contentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null); } catch (Exception e) {if(url ! = null) { contentResolver.delete(url, null, null); }}}}Copy the code

This involves the screenshot saving logic, and the scope of the screenshot needs to be specified. After saving, the system media database needs to be notified (the acquisition of wechat pictures should be the system media library to be read) to ensure data update.

3. Encapsulate SDK

Since the sdK-free payment tipping function can be implemented, we encapsulate this function into an SDK to facilitate project implantation.

So this convenient introduction, simple use, good experience and other principles, to achieve MiniPay open source project.

The project integrates wechat and Alipay into a payment page. Click the background to switch the tipping channel.

Just one line of code to introduce your own project

     compile 'com. Canking. Minipay: minipay: 1.0 x'
Copy the code

Just one line to start the MiniPay tip component

     MiniPayUtils.setupPay(this, config);
Copy the code

The code is completely open, source portal, can completely customize the logic.


Welcome to reprint, please indicate the source: Changxing E station Canking.win