华为内购支付接入

前言:

各位同学大家好 有段时间没有跟大家见面了 具体多久我也记不清楚了哈, 最近公司这边接入华为内购支付有点问题,所以我这边就整理一个文档 那么废话不多说我们正式开始。

需要用到的三方库

    implementation 'com.huawei.hms:iap:4.0.4.301'

初始化建立链接

 IapClient mClient = Iap.getIapClient(this);
        IapRequestHelper.isEnvReady(mClient, new IapApiCallback<IsEnvReadyResult>() {
            @Override
            public void onSuccess(IsEnvReadyResult result) {
                Log.e(TAG, "onSuccess: "+"检查支付成功  --- >  " );
       
            }

            @Override
            public void onFail(Exception e) {
                Log.e(TAG, "isEnvReady fail, " + e.getMessage());
                ExceptionHandle.handle(HuaWeiPayActivity.this, e);
            }
        });

查询商品id

    /**
     *
     * @param productId
     *
     * 查询商品iD
     */
    private void queryProducts(final String productId ) {
        List<String> productIds = new ArrayList<String>();
        productIds.add(productId);
        IapRequestHelper.obtainProductInfo(mClient, productIds, IapClient.PriceType.IN_APP_CONSUMABLE, new IapApiCallback<ProductInfoResult>() {
            @Override
            public void onSuccess(ProductInfoResult result) {
                Log.i(TAG, "obtainProductInfo, success");
                if (result == null) {
                    return;
                }
                if (result.getProductInfoList() != null) {
                  //  consumableProducts = result.getProductInfoList();
                }

                ProductInfo productInfo=result.getProductInfoList().get(0);
                UserInfo.DataBean userInfo;
                if (DataStore.getInstance().getUserInfo() == null) {
                    Toast.makeText(mContext, R.string.please_login_first, Toast.LENGTH_SHORT).show();
                    AppManager.getAppManager().finishActivity(HuaWeiPayActivity.class);
                    return;
                } else {
                    userInfo = DataStore.getInstance().getUserInfo();
                }

                //TODO  华为支付下单逻辑

                Map<String, String> params = new HashMap<>();
                params.put("user_id", userInfo.getId());
                params.put("token", userInfo.getToken());
                params.put("gid", cosumerData.getGame_id());
                params.put("cp_sid", cosumerData.getCpsid());
                params.put("role_id", cosumerData.getRole_id());
                params.put("role_name", cosumerData.getRole_name());
                params.put("product_id", productInfo.getProductId());
                params.put("product_name",productInfo.getProductName());
                params.put("pay_channel_id","3");
                double price = 0;
                try {
                    price = CommonUtil.getDoubleValue(productInfo.getMicrosPrice()+"");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                params.put("price_amount", "" + price);
                params.put("product_price", productInfo.getPrice());
                params.put("currency_code", productInfo.getCurrency());
                if(DataStore.getInstance()!=null&&DataStore.getInstance().getDeviceInfo()!=null) {
                    params.put("device_id", DataStore.getInstance().getDeviceInfo().getDevice_id());//设备ID
                }
                UrlHttpUtil.post(Constant.DATA_PAY_TOKEN, params, new CallBackUtil.CallBackStringWarp() {

                    @Override
                    public void onFailureData(int code, String errorMessage) {
                        ToastUtil.showNetwork(mContext);

                    }

                    @Override
                    public void onResponseData(String response) {
                        Log.e(TAG, "onResponseData:  response    ---- >  "+ response );
                        try {
                            JSONObject jsonObject=new JSONObject(response);
                             String msg=jsonObject.optString("msg");
                            int code=jsonObject.optInt("code");
                            if(code==1){
                                token=jsonObject.optString("data");
                                gotoPay((Activity) mContext,productId, IapClient.PriceType.IN_APP_CONSUMABLE,token);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
              //  showProducts();
            }
            @Override
            public void onFail(Exception e) {
                Log.e(TAG, "obtainProductInfo: " + e.getMessage());
                ExceptionHandle.handle(HuaWeiPayActivity.this, e);
              //  showProducts();
            }
        });
    }

调起支付具体逻辑

  /**
     * create orders for in-app products in the PMS.
     * @param activity indicates the activity object that initiates a request.
     * @param productId ID list of products to be queried. Each product ID must exist and be unique in the current app.
     * @param type  In-app product type.
     *  华为支付调起具体逻辑
     *
     *
     *
     */
    private void gotoPay(final Activity activity, String productId, int type, String  token ) {
        Log.i(TAG, "call createPurchaseIntent");
        Log.e(TAG, "gotoPay:productId   ---  > "+productId +"type  --- >"+  type );
        IapClient mClient = Iap.getIapClient(activity);
        Task<PurchaseIntentResult> task = mClient.createPurchaseIntent(createPurchaseIntentReq(type, productId,token));
        task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {
            @Override
            public void onSuccess(PurchaseIntentResult result) {
                Log.i(TAG, "createPurchaseIntent, onSuccess");
                if (result == null) {
                    Log.e(TAG, "result is null");
                    return;
                }
                Status status = result.getStatus();
                if (status == null) {
                    Log.e(TAG, "status is null");
                    return;
                }
                // you should pull up the page to complete the payment process.
                if (status.hasResolution()) {
                    try {
                        status.startResolutionForResult(activity, REQ_CODE_BUY);
                    } catch (IntentSender.SendIntentException exp) {
                        Log.e(TAG, exp.getMessage());
                    }
                } else {
                    Log.e(TAG, "intent is null");
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.e(TAG, e.getMessage());
                Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
                if (e instanceof IapApiException) {
                    IapApiException apiException = (IapApiException)e;
                    int returnCode = apiException.getStatusCode();
                    Log.e(TAG, "createPurchaseIntent, returnCode: " + returnCode);
                    // handle error scenarios
                } else {
                    // Other external errors
                }

            }
        });
    }

    /**
     * Create a PurchaseIntentReq instance.
     * @param type In-app product type.
     * @param productId ID of the in-app product to be paid.
     *              The in-app product ID is the product ID you set during in-app product configuration in 
       AppGallery Connect.
     * @return PurchaseIntentReq
     */
    private PurchaseIntentReq createPurchaseIntentReq(int type, String productId,String token) {
        PurchaseIntentReq req = new PurchaseIntentReq();
        req.setProductId(productId);
        req.setPriceType(type);
        req.setDeveloperPayload(token);
        return req;
    }

   

支付回调

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQ_CODE_BUY) {
            if (data == null) {
                Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
                return;
            }
            PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data);

            Log.e(TAG, "onActivityResult: purchaseResultInfo.getInAppPurchaseData()   ----  > "+purchaseResultInfo.getInAppPurchaseData() );


            Log.e(TAG, "onActivityResult: purchaseResultInfo.getInAppDataSignature()   ----  > "+purchaseResultInfo.getInAppDataSignature() );


            switch(purchaseResultInfo.getReturnCode()) {
                case OrderStatusCode.ORDER_STATE_SUCCESS:
                    // verify signature of payment results.
                    boolean success = CipherUtil.doCheck(purchaseResultInfo.getInAppPurchaseData(), purchaseResultInfo.getInAppDataSignature(), Key.getPublicKey());
                    if (success) {
                        hwVerify(purchaseResultInfo.getInAppPurchaseData(),purchaseResultInfo.getInAppDataSignature());
                        // Call the consumeOwnedPurchase interface to consume it after successfully delivering the product to your user.
                        consumeOwnedPurchase(this, purchaseResultInfo.getInAppPurchaseData());
                    } else {
                        Toast.makeText(this, "Pay successful,sign failed", Toast.LENGTH_SHORT).show();
                        finish();;
                    }
                    return;
                case OrderStatusCode.ORDER_STATE_CANCEL:
                    // The User cancels payment.
                    Toast.makeText(this, "user cancel", Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                case OrderStatusCode.ORDER_PRODUCT_OWNED:
                    // The user has already owned the product.
                    Toast.makeText(this, "you have owned the product", Toast.LENGTH_SHORT).show();
                    // you can check if the user has purchased the product and decide whether to provide goods
                    // if the purchase is a consumable product, consuming the purchase and deliver product
                    finish();
                    return;

                default:
                    Toast.makeText(this, "Pay failed", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
            return;
        }

    }
  

商品支付消耗

    /**
     * Consume the unconsumed purchase with type 0 after successfully delivering the product, then the Huawei payment server will update the order status and the user can purchase the product again.
     * @param inAppPurchaseData JSON string that contains purchase order details.
     */
    private void consumeOwnedPurchase(final Context context, String inAppPurchaseData) {
        Log.i(TAG, "call consumeOwnedPurchase");
        IapClient mClient = Iap.getIapClient(context);
        Task<ConsumeOwnedPurchaseResult> task = mClient.consumeOwnedPurchase(createConsumeOwnedPurchaseReq(inAppPurchaseData));
        task.addOnSuccessListener(new OnSuccessListener<ConsumeOwnedPurchaseResult>() {
            @Override
            public void onSuccess(ConsumeOwnedPurchaseResult result) {
                Log.e(TAG, "onSuccess:result     --->  "+result );
                // Consume success
                Log.i(TAG, "consumeOwnedPurchase success");
                Toast.makeText(context, "Pay success, and the product has been delivered", Toast.LENGTH_SHORT).show();
                finish();


            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.e(TAG, e.getMessage());
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                if (e instanceof IapApiException) {
                    IapApiException apiException = (IapApiException)e;
                    int returnCode = apiException.getStatusCode();
                    Log.e(TAG, "consumeOwnedPurchase fail,returnCode: " + returnCode);
                } else {
                    // Other external errors
                }

            }
        });
    }

    /**
     * Create a ConsumeOwnedPurchaseReq instance.
     * @param purchaseData JSON string that contains purchase order details.
     * @return ConsumeOwnedPurchaseReq
     */
    private ConsumeOwnedPurchaseReq createConsumeOwnedPurchaseReq(String purchaseData) {
        ConsumeOwnedPurchaseReq req = new ConsumeOwnedPurchaseReq();
        // Parse purchaseToken from InAppPurchaseData in JSON format.
        try {
            InAppPurchaseData inAppPurchaseData = new InAppPurchaseData(purchaseData);
            req.setPurchaseToken(inAppPurchaseData.getPurchaseToken());
        } catch (JSONException e) {
            Log.e(TAG, "createConsumeOwnedPurchaseReq JSONExeption");
        }
        return req;
    }
  

支付完成数据回传校验

    /**
     *
     * @param inAppPurchaseData
     * @param inAppDataSignature
     *  华为支付完成之后的order_token 的校验
     *
     */

    private  void  hwVerify(String inAppPurchaseData  ,String inAppDataSignature ){
        Map<String,String>map=new HashMap<>();
        map.put("inapp_purchase_data",inAppPurchaseData);
        map.put("inapp_data_signature",inAppDataSignature);
        map.put("app_id",DataStore.getInstance().getContext().getPackageName());
        UrlHttpUtil.post(Constant.DATA_SDK_HWVEROFY, map, new CallBackUtil.CallBackStringWarp() {

            @Override
            public void onFailureData(int code, String errorMessage) {

                ToastUtil.showNetwork(mContext);

                Log.e(TAG, "onFailureData:errorMessage   -- > "+errorMessage );
            }

            @Override
            public void onResponseData(String response) {
                Log.e(TAG, "onResponseData:  response    ---- >  "+ response );
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    String msg=jsonObject.optString("msg");
                    int code=jsonObject.optInt("code");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

在支付回调里面我们拿到 inapp_purchase_data 和 inapp_data_signature 请求服务端做支付校验 。

最后总结:

华为的内购支付和google 旧版的内购支付的差不多 都是首先建立链接 然后去通过查询商品ID 去查询到商品档位配置的信息。然后我们去服务器预下单 然后调起内购支付。在 onActivityResult 方法里面接收支付回调 我们在回到里面拿到 回调的 inapp_purchase_data 和 inapp_data_signature 做数据回传校验即可 然后在客户端调用消耗的代码即可。 最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容