Skip to content

Commit

Permalink
ohos新增自动订阅支付
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmYunyang committed Nov 1, 2024
1 parent ae5ec84 commit 2bb58a9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/src/tobias_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class MethodChannelTobias extends TobiasPlatform {
/// [universalLink] only supports iOS
@override
Future<Map> pay(String order,
{AliPayEvn evn = AliPayEvn.online, String? universalLink}) async {
{AliPayEvn evn = AliPayEvn.online,
String? universalLink,
bool isOhosAutoSub = false}) async {
return await methodChannel.invokeMethod("pay",
{"order": order, "payEnv": evn.index, "universalLink": universalLink});
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/tobias_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ abstract class TobiasPlatform extends PlatformInterface {
}

/// [evn] only supports Android due to native AliPaySDK
Future<Map> pay(String order, {AliPayEvn evn = AliPayEvn.online, String? universalLink}) async {
Future<Map> pay(String order,
{AliPayEvn evn = AliPayEvn.online,
String? universalLink,
bool isOhosAutoSub = false}) async {
throw UnimplementedError('pay() has not been implemented.');
}

Expand Down
51 changes: 40 additions & 11 deletions ohos/src/main/ets/components/plugin/TobiasPlugin.ets
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
import { bundleManager, common } from '@kit.AbilityKit';
import * as alipay from "@cashier_alipay/cashiersdk"
import { BusinessError } from '@kit.BasicServicesKit';
import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';

const MESSAGE_CHANNEL_NAME = "com.jarvanmo/tobias";
const TAG: string = 'TobiasPlugin';

/** TobiasPlugin **/
export default class TobiasPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware {
Expand Down Expand Up @@ -71,17 +73,44 @@ export default class TobiasPlugin implements FlutterPlugin, MethodCallHandler, A

pay(call: MethodCall, result: MethodResult): void {
const order: string = call.argument("order");
const isShowLoading: boolean = true;
const payment = new alipay.Pay();
payment.pay(order, isShowLoading)
.then((res: Map<string, string>) => {
result.success(res);
})
.catch((_: BusinessError) => {
const resp: Map<string, string> = new Map();
resp.set("resultStatus", "-1");
result.success(resp);
});
const isOhosAutoSub: boolean = call.argument("isOhosAutoSub");
if(isOhosAutoSub){
console.log(TAG, '当前为自动订阅');
let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
let openLinkOptions: OpenLinkOptions = {
appLinkingOnly: false
};
const resp: Map<string, string> = new Map();
try {
context.openLink(order, openLinkOptions)
.then(() => {
console.log(TAG, 'open link success.');
resp.set("resultStatus", "success");
result.success(resp)
}).catch((err: BusinessError) => {
console.log(TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
resp.set("resultStatus", "erroe");
result.success(resp)
})
} catch (paramError) {
console.log(TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
resp.set("resultStatus", "erroe");
result.success(resp)
}
} else {
console.log(TAG, '当前为非自动订阅');
const isShowLoading: boolean = true;
const payment = new alipay.Pay();
payment.pay(order, isShowLoading)
.then((res: Map<string, string>) => {
result.success(res);
})
.catch((_: BusinessError) => {
const resp: Map<string, string> = new Map();
resp.set("resultStatus", "-1");
result.success(resp);
});
}
}

isAliPayInstalled(result: MethodResult): void {
Expand Down

0 comments on commit 2bb58a9

Please sign in to comment.