1
This commit is contained in:
1
lib_huawei/.gitignore
vendored
Normal file
1
lib_huawei/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
32
lib_huawei/build.gradle
Normal file
32
lib_huawei/build.gradle
Normal file
@@ -0,0 +1,32 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion rootProject.ext.android.compileSdkVersion
|
||||
buildToolsVersion rootProject.ext.android.buildToolsVersion
|
||||
defaultConfig {
|
||||
minSdkVersion minSdkVersion
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
versionCode versionCode
|
||||
versionName versionName
|
||||
targetSdkVersion targetSdkVersion
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation rootProject.ext.dependencies["appcompat-androidx"]
|
||||
|
||||
api 'com.huawei.hms:iap:4.0.4.301'
|
||||
}
|
||||
0
lib_huawei/consumer-rules.pro
Normal file
0
lib_huawei/consumer-rules.pro
Normal file
21
lib_huawei/proguard-rules.pro
vendored
Normal file
21
lib_huawei/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.shayu.lib_huawei;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
assertEquals("com.shayu.lib_huawei.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
5
lib_huawei/src/main/AndroidManifest.xml
Normal file
5
lib_huawei/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.shayu.lib_huawei">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.shayu.lib_huawei.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentSender;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.huawei.hmf.tasks.OnCanceledListener;
|
||||
import com.huawei.hmf.tasks.OnFailureListener;
|
||||
import com.huawei.hmf.tasks.OnSuccessListener;
|
||||
import com.huawei.hmf.tasks.Task;
|
||||
import com.huawei.hms.iap.Iap;
|
||||
import com.huawei.hms.iap.IapApiException;
|
||||
import com.huawei.hms.iap.IapClient;
|
||||
import com.huawei.hms.iap.entity.ConsumeOwnedPurchaseReq;
|
||||
import com.huawei.hms.iap.entity.ConsumeOwnedPurchaseResult;
|
||||
import com.huawei.hms.iap.entity.InAppPurchaseData;
|
||||
import com.huawei.hms.iap.entity.OwnedPurchasesReq;
|
||||
import com.huawei.hms.iap.entity.OwnedPurchasesResult;
|
||||
import com.huawei.hms.iap.entity.PurchaseIntentReq;
|
||||
import com.huawei.hms.iap.entity.PurchaseIntentResult;
|
||||
import com.huawei.hms.iap.entity.PurchaseResultInfo;
|
||||
import com.huawei.hms.support.api.client.Status;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class HuaWeiPayManage {
|
||||
|
||||
IapClient mClient;
|
||||
|
||||
Activity mContext;
|
||||
|
||||
public HuaWeiPayManage(Activity mContext) {
|
||||
this.mContext = mContext;
|
||||
mClient = Iap.getIapClient(mContext);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.DONUT)
|
||||
public void pay(String changeid) {
|
||||
Task<PurchaseIntentResult> task = mClient.createPurchaseIntent(createPurchaseIntentReq(0, changeid, "35ee54225cc803bb233d9ec2aa8ae65618f131de77c2a37761dda822f4debeee"));
|
||||
task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {
|
||||
@Override
|
||||
public void onSuccess(PurchaseIntentResult result) {
|
||||
result.getReturnCode();
|
||||
if (result == null) {
|
||||
Log.e("TAG", "result is null");
|
||||
return;
|
||||
}
|
||||
Status status = result.getStatus();
|
||||
if (status == null) {
|
||||
Log.e("TAG", "status is null");
|
||||
return;
|
||||
}
|
||||
if (status.hasResolution()) {
|
||||
try {
|
||||
status.startResolutionForResult(mContext, 6666);
|
||||
} 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) {
|
||||
if (e.getMessage() != null) {
|
||||
if (!e.getMessage().contains("60004")) {
|
||||
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addOnCanceledListener(new OnCanceledListener() {
|
||||
@Override
|
||||
public void onCanceled() {
|
||||
Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private PurchaseIntentReq createPurchaseIntentReq(int type, String productId, String token) {
|
||||
PurchaseIntentReq req = new PurchaseIntentReq();
|
||||
req.setProductId(productId);
|
||||
req.setPriceType(type);
|
||||
req.setDeveloperPayload(token);
|
||||
return req;
|
||||
}
|
||||
|
||||
public JSONObject getPayResult(Intent data) {
|
||||
JSONObject object = new JSONObject();
|
||||
PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(mContext).parsePurchaseResultInfoFromIntent(data);
|
||||
try {
|
||||
object.put("code", purchaseResultInfo.getReturnCode());
|
||||
object.put("inAppPurchaseData", purchaseResultInfo.getInAppPurchaseData());
|
||||
object.put("inAppPurchaseDataSignature", purchaseResultInfo.getInAppDataSignature());
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 消耗所有商品
|
||||
*/
|
||||
public void consume() {
|
||||
OwnedPurchasesReq ownedPurchasesReq = new OwnedPurchasesReq();
|
||||
// priceType: 0:消耗型商品; 1:非消耗型商品; 2:订阅型商品
|
||||
ownedPurchasesReq.setPriceType(0);
|
||||
// 获取调用接口的Activity对象
|
||||
// 调用obtainOwnedPurchases接口获取所有已购但未发货的消耗型商品的购买信息
|
||||
Task<OwnedPurchasesResult> task = Iap.getIapClient(mContext).obtainOwnedPurchases(ownedPurchasesReq);
|
||||
task.addOnSuccessListener(new OnSuccessListener<OwnedPurchasesResult>() {
|
||||
@Override
|
||||
public void onSuccess(OwnedPurchasesResult result) {
|
||||
// 获取接口请求成功的结果
|
||||
Log.e("TAG", "获取接口请求成功的结果");
|
||||
if (result != null && result.getInAppPurchaseDataList() != null) {
|
||||
for (int i = 0; i < result.getInAppPurchaseDataList().size(); i++) {
|
||||
String inAppPurchaseData = result.getInAppPurchaseDataList().get(i);
|
||||
|
||||
Log.e("TAG", "未消耗订单:" + i + "___" + inAppPurchaseData);
|
||||
|
||||
ConsumeOwnedPurchaseReq req = new ConsumeOwnedPurchaseReq();
|
||||
String purchaseToken = "";
|
||||
try {
|
||||
InAppPurchaseData inAppPurchaseDataBean = new InAppPurchaseData(inAppPurchaseData);
|
||||
purchaseToken = inAppPurchaseDataBean.getPurchaseToken();
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
req.setPurchaseToken(purchaseToken);
|
||||
Task<ConsumeOwnedPurchaseResult> task = mClient.consumeOwnedPurchase(req);
|
||||
task.addOnSuccessListener(new OnSuccessListener<ConsumeOwnedPurchaseResult>() {
|
||||
@Override
|
||||
public void onSuccess(ConsumeOwnedPurchaseResult consumeOwnedPurchaseResult) {
|
||||
}
|
||||
}).addOnFailureListener(new OnFailureListener() {
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
if (e instanceof IapApiException) {
|
||||
IapApiException apiException = (IapApiException) e;
|
||||
Status status = apiException.getStatus();
|
||||
int returnCode = apiException.getStatusCode();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addOnFailureListener(new OnFailureListener() {
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
if (e instanceof IapApiException) {
|
||||
IapApiException apiException = (IapApiException) e;
|
||||
Status status = apiException.getStatus();
|
||||
int returnCode = apiException.getStatusCode();
|
||||
} else {
|
||||
// 其他外部错误
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.shayu.lib_huawei;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user