Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 538db001
authored
2026-07-14 10:22:45 +0800
by
ethanlamzs
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
testing
1 parent
4412ac66
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
24 deletions
lib/services/subscription_service_ios.dart
lib/services/subscription_service_ios.dart
View file @
538db00
...
...
@@ -77,11 +77,8 @@ class SubscriptionService {
// ---------- 购买订阅 ----------
Future
<
bool
>
buySubscription
(
ProductDetails
product
,
String
userId
,
String
tbxStuId
,
String
yWOrderId
)
async
{
//final orderId = await api.createPendingOrder(...);
final
orderId
=
yWOrderId
;
_pendingContexts
[
product
.
id
]
=
{
'userId'
:
userId
,
'tbxStuId'
:
tbxStuId
,
"orderId"
:
orderId
};
_orderIdMap
[
product
.
id
]
=
orderId
;
// 按产品 ID 存储
_pendingContexts
[
product
.
id
]
=
{
'userId'
:
userId
,
'tbxStuId'
:
tbxStuId
,
"orderId"
:
yWOrderId
};
_orderIdMap
[
product
.
id
]
=
yWOrderId
;
// 按产品 ID 存储
// 订阅必须使用 PurchaseParam 并设置购买类型为订阅
final
PurchaseParam
purchaseParam
=
PurchaseParam
(
...
...
@@ -105,9 +102,20 @@ class SubscriptionService {
continue
;
}
final
orderId
=
_orderIdMap
.
remove
(
purchaseDetails
.
productID
);
// 取出并删除
final
ctx
=
_pendingContexts
[
purchaseDetails
.
productID
];
//var
orderId = _orderIdMap.remove(purchaseDetails.productID); // 取出并删除
final
ctx
=
_pendingContexts
[
purchaseDetails
.
productID
]
;
print
(
ctx
);
if
(
ctx
==
null
)
{
// 打印错误日志,防止崩溃
print
(
'错误:未找到 productID 对应的上下文缓存'
);
return
;
}
final
orderId
=
ctx
[
'orderId'
]
as
String
?;
final
userId
=
ctx
[
'userId'
]
as
String
?;
final
tbxStuId
=
ctx
[
'tbxStuId'
]
as
String
?;
print
(
"orderId ==> "
+
orderId
.
toString
()+
" "
+
userId
.
toString
()
+
". "
+
tbxStuId
.
toString
());
// 根据交易状态处理
switch
(
purchaseDetails
.
status
)
{
...
...
@@ -115,7 +123,7 @@ class SubscriptionService {
case
PurchaseStatus
.
purchased
:
// 购买成功或续期成功
// 1. 验证收据(必须!)
_verifyReceipt
(
purchaseDetails
,
orderId
??
''
,
"purchased"
);
_verifyReceipt
(
purchaseDetails
,
orderId
??
''
,
userId
??
''
,
"purchased"
);
// 2. 通知 App Store 交易完成
_iap
.
completePurchase
(
purchaseDetails
);
...
...
@@ -126,7 +134,7 @@ class SubscriptionService {
case
PurchaseStatus
.
restored
:
// 恢复购买成功
_verifyReceipt
(
purchaseDetails
,
""
,
"restored"
);
_verifyReceipt
(
purchaseDetails
,
orderId
??
''
,
userId
??
''
,
"restored"
);
_iap
.
completePurchase
(
purchaseDetails
);
case
PurchaseStatus
.
canceled
:
...
...
@@ -145,26 +153,27 @@ class SubscriptionService {
}
// ---------- 服务端验证收据 ----------
Future
<
void
>
_verifyReceipt
(
PurchaseDetails
purchaseDetails
,
String
orderid
,
String
scence
)
async
{
debugPrint
(
"[info] _verifyReceipt is start scence ["
+
scence
+
"] "
+
purchaseDetails
.
productID
+
" orderid:"
+
orderid
);
// 将收据发送到你的后端进行验证
final
String
receipt
=
purchaseDetails
.
verificationData
.
serverVerificationData
;
// 把 base64Receipt 作为 receipt-data 发给你的服务端验证
final
bool
isValid
=
await
_sendReceiptToBackend
(
receipt
,
orderid
);
if
(
isValid
)
{
// 更新本地订阅状态
_hasActiveSubscription
=
true
;
// 保存到期时间等信息
Future
<
void
>
_verifyReceipt
(
PurchaseDetails
purchaseDetails
,
String
orderid
,
String
userid
,
String
scence
)
async
{
debugPrint
(
"[info] _verifyReceipt is checking 3 ["
+
scence
+
"] "
+
purchaseDetails
.
productID
+
" orderid:"
+
orderid
+
" userid:"
+
userid
);
if
(
orderid
!=
null
&&
orderid
.
length
>
6
)
{
debugPrint
(
"[info] _verifyReceipt is start scence ["
+
scence
+
"] "
+
purchaseDetails
.
productID
+
" orderid:"
+
orderid
);
// 将收据发送到你的后端进行验证
final
String
receipt
=
purchaseDetails
.
verificationData
.
serverVerificationData
;
// 把 base64Receipt 作为 receipt-data 发给你的服务端验证
final
bool
isValid
=
await
_sendReceiptToBackend
(
userid
,
receipt
,
orderid
);
if
(
isValid
)
{
// 更新本地订阅状态
_hasActiveSubscription
=
true
;
// 保存到期时间等信息
}
}
}
// 示例:发送收据到自己的服务器进行二次验证
Future
<
bool
>
_sendReceiptToBackend
(
String
receipt
,
String
orderid
)
async
{
Future
<
bool
>
_sendReceiptToBackend
(
String
userid
,
String
receipt
,
String
orderid
)
async
{
var
result
=
_userAuthRepository
.
orderCheck
(
""
,
receipt
,
orderid
,
"ios"
,
1
);
var
result
=
_userAuthRepository
.
orderCheck
(
userid
,
receipt
,
orderid
,
"ios"
,
1
);
//var data = result['data'] as Map<String, dynamic>?;
//debugPrint(result);
debugPrint
(
"[info] _sendReceiptToBackend is done"
);
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment