|
@@ -123,33 +123,38 @@ class InAppPurchaseView(View):
|
|
|
root_certificates.append(file.read())
|
|
|
|
|
|
enable_online_checks = True
|
|
|
- app_apple_id = None # 生产环境必需
|
|
|
+ app_apple_id = None # 生产环境必需
|
|
|
signed_data_verifier = SignedDataVerifier(
|
|
|
root_certificates, enable_online_checks, environment, bundle_id, app_apple_id)
|
|
|
|
|
|
payload = signed_data_verifier.verify_and_decode_signed_transaction(signed_transaction_info)
|
|
|
|
|
|
product_id = None
|
|
|
- if payload and payload.productId:
|
|
|
+ originalTransaction_id = None
|
|
|
+ if payload and payload.productId and payload.originalTransactionId:
|
|
|
product_id = payload.productId
|
|
|
+ originalTransaction_id = payload.originalTransactionId
|
|
|
|
|
|
- if not product_id:
|
|
|
+ if not product_id or not originalTransaction_id:
|
|
|
pay_result_url = CommonService.get_payment_status_url(lang, 'fail')
|
|
|
return response.json(0, {'url': pay_result_url})
|
|
|
|
|
|
- if product_id == "30_days_1_month_subscription" or product_id == "30_days_1_month_ai_subscription":
|
|
|
- # 之前订阅过任意套餐返回不可购买
|
|
|
+ # 之前订阅过任意套餐返回不可再次订阅
|
|
|
+ if Store_Meal.objects.filter(product_id=product_id, cycle_config_id__isnull=False).exists():
|
|
|
transaction_subscription = client.get_all_subscription_statuses(transaction_id)
|
|
|
if transaction_subscription.data:
|
|
|
for subscription in transaction_subscription.data[0].lastTransactions:
|
|
|
if str(subscription.status) == "Status.ACTIVE":
|
|
|
- return response.json(10048, f"订阅状态为 {subscription.status} ")
|
|
|
+ key = str(originalTransaction_id) + "_SUBSCRIBED"
|
|
|
+ redis_originalTransaction_id = redis_obj.get_data(key)
|
|
|
+ if redis_originalTransaction_id != originalTransaction_id:
|
|
|
+ return response.json(10048, f"订阅状态为 {subscription.status} ")
|
|
|
|
|
|
pay_type = PAY_TYPE_IN_APP_PURCHASE
|
|
|
now_time = int(time.time())
|
|
|
|
|
|
store_qs = Store_Meal.objects.filter(
|
|
|
- product_id=product_id, lang__lang=lang, is_show=0).\
|
|
|
+ product_id=product_id, lang__lang=lang, is_show=0). \
|
|
|
values(
|
|
|
'id', 'currency', 'price', 'lang__content', 'day', 'commodity_type', 'lang__title', 'expire',
|
|
|
'commodity_code', 'discount_price', 'bucket_id', 'bucket__mold', 'cycle_config_id', 'is_ai')
|
|
@@ -168,7 +173,7 @@ class InAppPurchaseView(View):
|
|
|
commodity_code = store_qs[0]['commodity_code']
|
|
|
commodity_type = store_qs[0]['commodity_type']
|
|
|
order_type = 1 if is_ai else 0
|
|
|
- store_meal_qs = Store_Meal.objects.filter(id=rank_id, lang__lang='cn', is_show=0).\
|
|
|
+ store_meal_qs = Store_Meal.objects.filter(id=rank_id, lang__lang='cn', is_show=0). \
|
|
|
values('lang__title', 'lang__content')
|
|
|
if store_meal_qs.exists():
|
|
|
store_meal_name = store_meal_qs[0]['lang__title'] + '-' + store_meal_qs[0]['lang__content']
|
|
@@ -177,7 +182,7 @@ class InAppPurchaseView(View):
|
|
|
|
|
|
# 查询设备是否已开过云存
|
|
|
use_flag = True
|
|
|
- uid_bucket_qs = UID_Bucket.objects.filter(uid=uid).\
|
|
|
+ uid_bucket_qs = UID_Bucket.objects.filter(uid=uid). \
|
|
|
values('id', 'bucket_id', 'bucket__region', 'endTime', 'use_status')
|
|
|
if uid_bucket_qs.exists():
|
|
|
uid_bucket = uid_bucket_qs.first()
|
|
@@ -341,35 +346,156 @@ class InAppPurchaseView(View):
|
|
|
decoded_payload = verifier.verify_and_decode_notification(signed_payload)
|
|
|
|
|
|
logger.info(f"App Store服务器通知解码后decoded_payload:{decoded_payload}")
|
|
|
- logger.info(f"App Store服务器通知decoded_payload.rawNotificationType{str(decoded_payload.rawNotificationType)}")
|
|
|
- if decoded_payload.rawNotificationType == "DID_CHANGE_RENEWAL_STATUS":
|
|
|
- # 已更改订阅状态
|
|
|
- # 一种通知类型,与其子类型一起表示客户更改了订阅续订状态。如果subtype为
|
|
|
- # AUTO_RENEW_ENABLED,则表示客户重新启用了订阅自动续订功能。如果subtype为
|
|
|
- # AUTO_RENEW_DISABLED,则表示客户禁用了订阅自动续订功能,或者客户请求退款后
|
|
|
- # App
|
|
|
- # Store
|
|
|
- # 禁用了订阅自动续订功能。
|
|
|
- pass
|
|
|
- elif str(decoded_payload.rawNotificationType) == "DID_RENEW":
|
|
|
- # 一种通知类型,与其subtype一起表示订阅已成功续订。如果subtype为
|
|
|
- # BILLING_RECOVERY,则表示之前未能续订的过期订阅已成功续订。如果子类型为空,则活动订购已成功自动续订到新的交易期。向客户提供订阅内容或服务的访问权限。
|
|
|
- logger.info(f"返回400拒绝续订")
|
|
|
- return HttpResponse(status=400)
|
|
|
-
|
|
|
- elif decoded_payload.rawNotificationType == "SUBSCRIBED":
|
|
|
+ logger.info(
|
|
|
+ f"App Store服务器通知decoded_payload.rawNotificationType{str(decoded_payload.rawNotificationType)}")
|
|
|
+ if str(decoded_payload.rawNotificationType) == "DID_RENEW":
|
|
|
+ decoded_transaction_information = verifier.verify_and_decode_signed_transaction(
|
|
|
+ decoded_payload.data.signedTransactionInfo)
|
|
|
+
|
|
|
+ # originalTransactionId 原始购买的交易标识符
|
|
|
+ originalTransaction_id = decoded_transaction_information.originalTransactionId
|
|
|
+
|
|
|
+ logger.info(f"App Store服务器通知originalTransactionId原始购买的交易标识符{originalTransaction_id}")
|
|
|
+ if not originalTransaction_id:
|
|
|
+ logger.info(f"App Store服务器通知originalTransactionId原始购买的交易标识符为空, 返回状态 400")
|
|
|
+ return HttpResponse(status=400)
|
|
|
+ else:
|
|
|
+ ord_order = Order_Model.objects.filter(agreement_id=originalTransaction_id).order_by(
|
|
|
+ 'addTime').first()
|
|
|
+ channel = ord_order.channel
|
|
|
+ uid = ord_order.uid
|
|
|
+ pay_type = ord_order.payType
|
|
|
+ user_id = ord_order.userID_id
|
|
|
+
|
|
|
+ store_qs = Store_Meal.objects.filter(id=ord_order.rank). \
|
|
|
+ values(
|
|
|
+ 'id', 'currency', 'price', 'lang__content', 'day', 'commodity_type', 'lang__title',
|
|
|
+ 'expire', 'lang__lang',
|
|
|
+ 'commodity_code', 'discount_price', 'bucket_id', 'bucket__mold', 'cycle_config_id', 'is_ai')
|
|
|
+ if not store_qs.exists():
|
|
|
+ logger.info(f"App Store服务器通知云存套餐不存在, 返回状态 400")
|
|
|
+ return HttpResponse(status=400)
|
|
|
+
|
|
|
+ order_id = CommonService.createOrderID()
|
|
|
+ rank_id = store_qs[0]['id']
|
|
|
+ bucket_id = store_qs[0]['bucket_id']
|
|
|
+ currency = store_qs[0]['currency']
|
|
|
+ price = store_qs[0]['price']
|
|
|
+ is_ai = store_qs[0]['is_ai']
|
|
|
+ expire = store_qs[0]['expire']
|
|
|
+ end_time = CommonService.calcMonthLater(expire)
|
|
|
+ content = store_qs[0]['lang__content']
|
|
|
+ commodity_code = store_qs[0]['commodity_code']
|
|
|
+ commodity_type = store_qs[0]['commodity_type']
|
|
|
+ lang = store_qs[0]['lang__lang']
|
|
|
+ order_type = 1 if is_ai else 0
|
|
|
+
|
|
|
+ store_meal_qs = Store_Meal.objects.filter(id=rank_id, lang__lang='cn', is_show=0). \
|
|
|
+ values('lang__title', 'lang__content')
|
|
|
+ if store_meal_qs.exists():
|
|
|
+ store_meal_name = store_meal_qs[0]['lang__title'] + '-' + store_meal_qs[0]['lang__content']
|
|
|
+ else:
|
|
|
+ store_meal_name = '未知套餐'
|
|
|
+
|
|
|
+ # 查询设备是否已开过云存
|
|
|
+ use_flag = True
|
|
|
+ uid_bucket_qs = UID_Bucket.objects.filter(uid=uid). \
|
|
|
+ values('id', 'bucket_id', 'bucket__region', 'endTime', 'use_status')
|
|
|
+ now_time = int(time.time())
|
|
|
+ if uid_bucket_qs.exists():
|
|
|
+ uid_bucket = uid_bucket_qs.first()
|
|
|
+ uid_bucket_id = uid_bucket['id']
|
|
|
+ # 叠加相同套餐的过期时间
|
|
|
+ if uid_bucket['use_status'] == 1 and uid_bucket['endTime'] > now_time:
|
|
|
+ Unused_Uid_Meal.objects.create(
|
|
|
+ uid=uid, channel=channel, addTime=now_time, order_id=order_id, expire=expire,
|
|
|
+ is_ai=is_ai,
|
|
|
+ bucket_id=bucket_id)
|
|
|
+ UID_Bucket.objects.filter(id=uid_bucket_id).update(has_unused=1)
|
|
|
+ use_flag = False
|
|
|
+ # 更新套餐的过期时间
|
|
|
+ else:
|
|
|
+ UID_Bucket.objects.filter(id=uid_bucket_id).update(
|
|
|
+ channel=channel, bucket_id=bucket_id, endTime=end_time, updateTime=now_time,
|
|
|
+ use_status=1,
|
|
|
+ orderId=order_id)
|
|
|
+ else:
|
|
|
+ uid_bucket = UID_Bucket.objects.create(
|
|
|
+ uid=uid, channel=channel, bucket_id=bucket_id, endTime=end_time, use_status=1,
|
|
|
+ orderId=order_id,
|
|
|
+ addTime=now_time, updateTime=now_time)
|
|
|
+ uid_bucket_id = uid_bucket.id
|
|
|
+
|
|
|
+ # 开通AI服务
|
|
|
+ if is_ai and use_flag:
|
|
|
+ ai_service = AiService.objects.filter(uid=uid, channel=channel)
|
|
|
+ # 有正在使用的套餐,叠加套餐时间,否则创建
|
|
|
+ if ai_service.exists():
|
|
|
+ ai_service.update(updTime=now_time, use_status=1, orders_id=order_id,
|
|
|
+ endTime=end_time)
|
|
|
+ else:
|
|
|
+ AiService.objects.create(
|
|
|
+ uid=uid, channel=channel, detect_status=1, use_status=1, orders_id=order_id,
|
|
|
+ addTime=now_time, updTime=now_time, endTime=end_time)
|
|
|
+
|
|
|
+ Order_Model.objects.create(
|
|
|
+ orderID=order_id, UID=uid, channel=channel, userID_id=user_id, desc=content,
|
|
|
+ payType=pay_type,
|
|
|
+ payTime=now_time, price=price, currency=currency, addTime=now_time, updTime=now_time,
|
|
|
+ order_type=order_type, commodity_code=commodity_code, commodity_type=commodity_type,
|
|
|
+ rank_id=rank_id,
|
|
|
+ ai_rank_id=1, status=1, create_vod=1, store_meal_name=store_meal_name,
|
|
|
+ uid_bucket_id=uid_bucket_id, agreement_id=originalTransaction_id
|
|
|
+ )
|
|
|
+
|
|
|
+ # 发送云存开通信息
|
|
|
+ date_time = time.strftime("%Y-%m-%d", time.localtime())
|
|
|
+ # 如果存在序列号,消息提示用序列号
|
|
|
+ device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number', 'Type')
|
|
|
+ serial_number = device_info_qs[0]['serial_number']
|
|
|
+ device_type = device_info_qs[0]['Type']
|
|
|
+ if serial_number:
|
|
|
+ device_name = CommonService.get_full_serial_number(uid, serial_number, device_type)
|
|
|
+ else:
|
|
|
+ device_name = uid
|
|
|
+ sys_msg_text_list = [
|
|
|
+ '温馨提示:尊敬的客户,您的{}设备在{}已成功续订云存套餐'.format(device_name, date_time),
|
|
|
+ 'Dear customer,you already subscribed the cloud storage package successfully for device {} on '.
|
|
|
+ format(device_name, time.strftime('%b %dth,%Y', time.localtime()))]
|
|
|
+ cls.do_vod_msg_notice(uid, user_id, lang, sys_msg_text_list)
|
|
|
+
|
|
|
+ elif str(decoded_payload.rawNotificationType) == "SUBSCRIBED":
|
|
|
# 处理订阅
|
|
|
# 一种通知类型,与其子类型一起表示客户订阅了自动续订的套餐。如果子类型为
|
|
|
# INITIAL_BUY(首次购买),则表示用户首次购买或通过家庭共享访问该订阅。如果子类型为
|
|
|
- # RESUBSCRIBE,则表示用户通过家庭共享重新订阅了同一订阅或同一订阅组中的另一个订阅或获得了访问权。
|
|
|
- pass
|
|
|
+ decoded_transaction_information = verifier.verify_and_decode_signed_transaction(
|
|
|
+ decoded_payload.data.signedTransactionInfo)
|
|
|
+ originalTransaction_id = decoded_transaction_information.originalTransactionId
|
|
|
+
|
|
|
+ # redis上锁,保证验证时是同一个订单
|
|
|
+ redis_obj = RedisObject()
|
|
|
+ redis_key = str(originalTransaction_id) + "_SUBSCRIBED"
|
|
|
+ redis_obj.set_data(redis_key, originalTransaction_id, 3600)
|
|
|
+
|
|
|
+ # 之前订阅过不可重复订阅
|
|
|
+ key_id = 'N42WMFCV6A'
|
|
|
+ issuer_id = '69a6de8c-789b-47e3-e053-5b8c7c11a4d1'
|
|
|
+ bundle_id = 'com.ansjer.zccloud'
|
|
|
+ environment = ENV
|
|
|
+ key_path = '{}/Ansjer/file/in_app_purchase/SubscriptionKey_N42WMFCV6A.p8'.format(BASE_DIR)
|
|
|
+ with open(key_path, 'rb') as file:
|
|
|
+ # 读取文件内容
|
|
|
+ private_key = file.read()
|
|
|
|
|
|
- elif decoded_payload.rawNotificationType == "ONE_TIME_CHARGE":
|
|
|
- # 处理单次购买 只有在沙盒环境会发通知
|
|
|
- pass
|
|
|
+ client = AppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
|
|
|
+ transaction_id = decoded_transaction_information.transactionId
|
|
|
+ transaction_subscription = client.get_all_subscription_statuses(transaction_id)
|
|
|
+ if transaction_subscription.data:
|
|
|
+ for subscription in transaction_subscription.data[0].lastTransactions:
|
|
|
+ if str(subscription.status) == "Status.ACTIVE":
|
|
|
+ return HttpResponse(status=400)
|
|
|
|
|
|
- elif decoded_payload.rawNotificationType == "EXPIRED":
|
|
|
- # 处理订阅过期
|
|
|
+ elif str(decoded_payload.rawNotificationType) == "EXPIRED":
|
|
|
# 一种通知类型,与其子类型一起表示订阅已过期。如果subtype为
|
|
|
# VOLUNTARY(自愿),则表示订阅在用户禁用订阅续订后过期。如果subtype是
|
|
|
# BILLING_RETRY(计费重试),则表示订阅过期,因为计费重试期结束时没有成功的计费交易。如果subtype为
|
|
@@ -378,26 +504,13 @@ class InAppPurchaseView(View):
|
|
|
# 没有子类型的通知表示订阅因其他原因过期。
|
|
|
pass
|
|
|
|
|
|
- elif decoded_payload.rawNotificationType == "GRACE_PERIOD_EXPIRED":
|
|
|
- # 处理订阅过期
|
|
|
- # 一种通知类型,表示账单宽限期已结束,但未续订,因此可以关闭对服务或内容的访问。通知客户其账单信息可能有问题。App Store 会继续重试计费 60 天,或直到客户解决了计费问题或取消了订阅(以先到者为准)。如需了解更多信息,请参阅减少用户非自愿流失。
|
|
|
- pass
|
|
|
-
|
|
|
- elif decoded_payload.rawNotificationType == "DID_FAIL_TO_RENEW":
|
|
|
- # 处理订阅续费失败
|
|
|
- # 该通知类型及其子类型表示订阅因计费问题而未能续订。订阅进入计费重试期。如果子类型为
|
|
|
- # GRACE_PERIOD,则在宽限期内继续提供服务。如果子类型为空,则订购不在宽限期内,可以停止提供订购服务。
|
|
|
- # 通知客户其账单信息可能有问题。App Store
|
|
|
- # 会继续重试计费60天,或直到客户解决了计费问题或取消了订阅(以先到者为准)。有关更多信息,请参阅
|
|
|
- # "减少用户非自愿流失"。
|
|
|
- pass
|
|
|
-
|
|
|
+ else:
|
|
|
+ logger.info(f"App Store服务器通知decoded_payload.rawNotificationType 未处理")
|
|
|
+ return HttpResponse(status=500)
|
|
|
return HttpResponse(status=200)
|
|
|
-
|
|
|
else:
|
|
|
logger.info('App Store服务器通知不是post请求')
|
|
|
return HttpResponse(status=400)
|
|
|
-
|
|
|
except Exception as e:
|
|
|
logger.info('App Store服务器通知异常:{}'.
|
|
|
format('error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))))
|