|
@@ -10,13 +10,15 @@ from django.db.models import Q, F
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Ansjer.config import PAYPAL_CRD, SERVER_DOMAIN_SSL, PAYPAL_WEB_HOOK_ID, PAYPAL_WEB_HOOK_ID_TWO
|
|
|
+from Ansjer.config import PAYPAL_CRD, SERVER_DOMAIN_SSL, PAYPAL_WEB_HOOK_ID, PAYPAL_WEB_HOOK_ID_TWO, CONFIG_INFO, \
|
|
|
+ CONFIG_US, CONFIG_EUR
|
|
|
from Controller import CloudStorage
|
|
|
from Model.models import PayCycleConfigModel, Store_Meal, UID_Bucket, PromotionRuleModel, \
|
|
|
- Unused_Uid_Meal, Device_Info, CouponModel, Order_Model, PaypalWebHookEvent
|
|
|
+ Unused_Uid_Meal, Device_Info, CouponModel, Order_Model, PaypalWebHookEvent, CountryModel
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Service.CommonService import CommonService
|
|
|
+PAY_LOGGER = logging.getLogger('pay')
|
|
|
|
|
|
|
|
|
# 周期扣款相关
|
|
@@ -324,17 +326,16 @@ class PaypalCycleNotify(View):
|
|
|
return True
|
|
|
|
|
|
def do_paypal_webhook_notify(self, request_dict, request, response):
|
|
|
- logger = logging.getLogger('pay')
|
|
|
- logger.info('--------进入周期扣款钩子--------')
|
|
|
+ PAY_LOGGER.info('--------进入周期扣款钩子--------')
|
|
|
if not request.body:
|
|
|
- logger.info('PayPal周期扣款失败---缺失请求体')
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---缺失请求体')
|
|
|
return HttpResponse('fail', status=500)
|
|
|
json_agreement_str = request.body.decode("utf-8")
|
|
|
json_obj = json.loads(json_agreement_str)
|
|
|
header = request.META
|
|
|
paypal_body = json_obj.get('resource')
|
|
|
- logger.info('----请求体数据:{}----'.format(json_agreement_str))
|
|
|
- logger.info('----请求头数据:{}----'.format(header))
|
|
|
+ PAY_LOGGER.info('----请求体数据:{}----'.format(json_agreement_str))
|
|
|
+ PAY_LOGGER.info('----请求头数据:{}----'.format(header))
|
|
|
try:
|
|
|
transmission_id = header.get('HTTP_PAYPAL_TRANSMISSION_ID', None)
|
|
|
transmission_time = header.get('HTTP_PAYPAL_TRANSMISSION_TIME', None)
|
|
@@ -358,7 +359,7 @@ class PaypalCycleNotify(View):
|
|
|
}
|
|
|
self.paypal_webhook_log(**PaypalWebHookEventInsert)
|
|
|
if event_type != 'PAYMENT.SALE.COMPLETED':
|
|
|
- logger.info('----event_type异常:{}----'.format(event_type))
|
|
|
+ PAY_LOGGER.info('----event_type异常:{}----'.format(event_type))
|
|
|
self.find_subscription_transactions(billing_agreement_id)
|
|
|
|
|
|
if resource_type == 'sale' and paypal_body.get('state') == 'completed':
|
|
@@ -367,33 +368,42 @@ class PaypalCycleNotify(View):
|
|
|
transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID, json_agreement_str, cert_url,
|
|
|
transmission_sig, auth_algo)
|
|
|
if not response:
|
|
|
- logger.info('PayPal周期扣款失败---签名验证失败')
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---签名验证失败')
|
|
|
return HttpResponse('Fail', status=500)
|
|
|
else:
|
|
|
- logger.info('PayPal周期扣款失败,付款状态有误,resource_type:{},state:{}----'.
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败,付款状态有误,resource_type:{},state:{}----'.
|
|
|
format(resource_type, paypal_body.get('state')))
|
|
|
return HttpResponse('Fail', status=500)
|
|
|
|
|
|
+ nowTime = int(time.time())
|
|
|
if not billing_agreement_id:
|
|
|
# 记录钩子日志
|
|
|
PaypalWebHookEvent.objects.create(**PaypalWebHookEventInsert)
|
|
|
+
|
|
|
# 普通支付,更新paypal交易id
|
|
|
paymentID = paypal_body.get('parent_payment')
|
|
|
if paymentID and paypal_transaction_id:
|
|
|
- Order_Model.objects.filter(paymentID=paymentID).update(
|
|
|
- updTime=int(time.time()),
|
|
|
- trade_no=paypal_transaction_id
|
|
|
- )
|
|
|
- logger.info('PayPal周期扣款成功---更新交易id:{}'.format(paypal_transaction_id))
|
|
|
+ # 查询客户地区信息,地区跟服务器配置不匹配,返回500
|
|
|
+ order_qs = Order_Model.objects.filter(paymentID=paymentID).values('userID__region_country')
|
|
|
+ if not order_qs.exists():
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---根据paymentID查询订单数据不存在')
|
|
|
+ return HttpResponse('Fail', status=500)
|
|
|
+
|
|
|
+ country_id = order_qs[0]['userID__region_country']
|
|
|
+ if not self.config_match_region(country_id):
|
|
|
+ return HttpResponse('Fail', status=500)
|
|
|
+
|
|
|
+ order_qs.update(updTime=nowTime, trade_no=paypal_transaction_id)
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款成功---更新交易id:{}'.format(paypal_transaction_id))
|
|
|
return HttpResponse('success')
|
|
|
else:
|
|
|
- logger.info('PayPal周期扣款失败---paymentID:{}或paypal_transaction_id:{}为空'.
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---paymentID:{}或paypal_transaction_id:{}为空'.
|
|
|
format(paymentID, paypal_transaction_id))
|
|
|
return HttpResponse('fail', status=500)
|
|
|
|
|
|
agreement_id = paypal_body.get('billing_agreement_id')
|
|
|
billing_agreement = paypalrestsdk.BillingAgreement.find(agreement_id)
|
|
|
- logger.info('billing_agreement:{}'.format(billing_agreement))
|
|
|
+ PAY_LOGGER.info('billing_agreement:{}'.format(billing_agreement))
|
|
|
|
|
|
# 记录钩子日志
|
|
|
PaypalWebHookEventInsert['agreement_desc'] = repr(billing_agreement)
|
|
@@ -408,23 +418,26 @@ class PaypalCycleNotify(View):
|
|
|
'payType', 'currency', 'addTime',
|
|
|
'commodity_type', 'updTime',
|
|
|
'userID__userID', 'uid_bucket_id',
|
|
|
- 'userID__username'
|
|
|
+ 'userID__username', 'userID__region_country'
|
|
|
)
|
|
|
if not order_qs.exists():
|
|
|
- logger.info('PayPal周期扣款失败---订单数据不存在')
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---根据order_id查询订单数据不存在')
|
|
|
return HttpResponse('fail', status=500)
|
|
|
|
|
|
+ country_id = order_qs[0]['userID__region_country']
|
|
|
+ if not self.config_match_region(country_id):
|
|
|
+ return HttpResponse('Fail', status=500)
|
|
|
+
|
|
|
UID = order_qs[0]['UID']
|
|
|
# PayPal周期扣款首次扣款
|
|
|
if billing_agreement.agreement_details.cycles_completed == '0':
|
|
|
# 更新order表,paypal的商家交易号
|
|
|
- order_qs.update(updTime=int(time.time()), trade_no=paypal_transaction_id)
|
|
|
- logger.info('{} PayPal周期扣款首次扣款成功'.format(UID))
|
|
|
+ order_qs.update(updTime=nowTime, trade_no=paypal_transaction_id)
|
|
|
+ PAY_LOGGER.info('{} PayPal周期扣款首次扣款成功'.format(UID))
|
|
|
return HttpResponse('success')
|
|
|
|
|
|
- nowTime = int(time.time())
|
|
|
if order_qs[0]['addTime'] + 9200 > nowTime: # 避免续费订单重复支付
|
|
|
- logger.info('{} PayPal周期扣款失败---续费订单已创建'.format(UID))
|
|
|
+ PAY_LOGGER.info('{} PayPal周期扣款失败---续费订单已创建'.format(UID))
|
|
|
return HttpResponse('success')
|
|
|
|
|
|
desc = order_qs[0]['desc']
|
|
@@ -439,7 +452,7 @@ class PaypalCycleNotify(View):
|
|
|
rank = order_qs[0]['rank']
|
|
|
store_meal_qs = Store_Meal.objects.filter(id=rank).values("day", "bucket_id", "bucket__storeDay", "expire")
|
|
|
if not store_meal_qs.exists():
|
|
|
- logger.info('{} PayPal周期扣款失败---套餐数据不存在'.format(UID))
|
|
|
+ PAY_LOGGER.info('{} PayPal周期扣款失败---套餐数据不存在'.format(UID))
|
|
|
return HttpResponse('fail', status=500)
|
|
|
|
|
|
bucketId = store_meal_qs[0]['bucket_id']
|
|
@@ -522,10 +535,10 @@ class PaypalCycleNotify(View):
|
|
|
}
|
|
|
]
|
|
|
billing_agreement.replace(billing_agreement_update_attributes)
|
|
|
- logger.info('{} PayPal周期扣款成功'.format(UID))
|
|
|
+ PAY_LOGGER.info('{} PayPal周期扣款成功'.format(UID))
|
|
|
return HttpResponse('success')
|
|
|
except Exception as e:
|
|
|
- logger.info('PayPal周期扣款异常: errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款异常: errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
return HttpResponse('fail', status=500)
|
|
|
|
|
|
@staticmethod
|
|
@@ -551,6 +564,15 @@ class PaypalCycleNotify(View):
|
|
|
except Exception as e:
|
|
|
logger.info('出错了~查询订阅的事务异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def config_match_region(country_id):
|
|
|
+ country_qs = CountryModel.objects.filter(id=country_id).values('region_id')
|
|
|
+ region_id = country_qs[0]['region_id']
|
|
|
+ if (CONFIG_INFO == CONFIG_US and region_id == 4) or (CONFIG_INFO == CONFIG_EUR and region_id != 4):
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---服务器跟用户地区不匹配')
|
|
|
+ return False
|
|
|
+ return True
|
|
|
+
|
|
|
def do_subscription_break_notify(self, request_dict, request, response):
|
|
|
logger = logging.getLogger('pay')
|
|
|
logger.info('--------进入订阅失败,付款失败,暂停--------')
|