|
@@ -10,7 +10,7 @@ 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, CONFIG_INFO, \
|
|
|
+from Ansjer.config import PAYPAL_CRD, SERVER_DOMAIN_SSL, PAYPAL_WEB_HOOK_ID, CONFIG_INFO, \
|
|
|
CONFIG_US, CONFIG_EUR
|
|
|
from Controller import CloudStorage
|
|
|
from Model.models import PayCycleConfigModel, Store_Meal, UID_Bucket, PromotionRuleModel, \
|
|
@@ -25,18 +25,21 @@ PAY_LOGGER = logging.getLogger('pay')
|
|
|
# 周期扣款相关
|
|
|
class Paypal:
|
|
|
# 检查是否有重复订阅
|
|
|
- def checkSubscriptions(userID, uid, rank):
|
|
|
- hasOrder = Order_Model.objects.filter(UID=uid)
|
|
|
+ def checkSubscriptions(userID, uid, rank, app_type):
|
|
|
+ hasOrder = Order_Model.objects.filter(UID=uid, app_type=app_type)
|
|
|
hasOrder = hasOrder.filter(~Q(agreement_id='')).values('agreement_id', 'orderID').order_by('-addTime')[0:1]
|
|
|
if not hasOrder.exists():
|
|
|
return True
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ if app_type == 1:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
+ elif app_type == 2:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
billing_agreement = paypalrestsdk.BillingAgreement.find(hasOrder[0]['agreement_id'])
|
|
|
if billing_agreement.state == 'Active':
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
- def subscriptions(store_info, lang, orderID, price):
|
|
|
+ def subscriptions(store_info, lang, orderID, price, app_type):
|
|
|
logger = logging.getLogger('pay')
|
|
|
cycle_config = PayCycleConfigModel.objects.filter(id=store_info['cycle_config_id']).values()
|
|
|
if not cycle_config:
|
|
@@ -46,8 +49,8 @@ class Paypal:
|
|
|
cal_url = "{SERVER_DOMAIN_SSL}web/paid2/fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
|
|
|
if lang != 'cn':
|
|
|
cal_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
|
|
|
- return_url = "{SERVER_DOMAIN_SSL}payCycle/paypalCycleReturn?lang={lang}". \
|
|
|
- format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, lang=lang)
|
|
|
+ return_url = "{SERVER_DOMAIN_SSL}payCycle/paypalCycleReturn?lang={lang}&app_type={app_type}". \
|
|
|
+ format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, lang=lang, app_type=app_type)
|
|
|
# call_sub_url = "http://binbin.uicp.vip/cloudstorage/dopaypalcallback?orderID={orderID}".format(
|
|
|
# SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, orderID=orderID)
|
|
|
# exit(price)
|
|
@@ -90,7 +93,10 @@ class Paypal:
|
|
|
],
|
|
|
"type": "INFINITE",
|
|
|
}
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ if app_type == 1:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
+ elif app_type == 2:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
billing_plan = paypalrestsdk.BillingPlan(BillingPlan)
|
|
|
if billing_plan.create():
|
|
|
billing_plan.activate() # 激活
|
|
@@ -199,11 +205,15 @@ class PaypalCycleNotify(View):
|
|
|
def do_paypal_cycle_return(self, request_dict, response):
|
|
|
lang = request_dict.get('lang', 'en')
|
|
|
token = request_dict.get('token', None)
|
|
|
+ app_type = int(request_dict.get('app_type', 1))
|
|
|
|
|
|
logger = logging.getLogger('pay')
|
|
|
logger.info('--------进入paypay首次订阅付款回调--------')
|
|
|
logger.info(request_dict)
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ if app_type == 1:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
+ elif app_type == 2:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
billing_agreement = paypalrestsdk.BillingAgreement()
|
|
|
billing_agreement_response = billing_agreement.execute(token)
|
|
|
if billing_agreement_response.error:
|
|
@@ -380,7 +390,7 @@ class PaypalCycleNotify(View):
|
|
|
agreement_id = paypal_body.get('billing_agreement_id')
|
|
|
paypal_transaction_id = paypal_body.get('id')
|
|
|
amount = paypal_body.get('amount')
|
|
|
- fee = paypal_body['transaction_fee']['value']
|
|
|
+ fee = paypal_body['transaction_fee']['value'] if 'transaction_fee' in paypal_body else 0
|
|
|
PaypalWebHookEventInsert = {
|
|
|
'webhook_event_id': json_obj.get('id'),
|
|
|
'resource_type': json_obj.get('resource_type'),
|
|
@@ -395,13 +405,20 @@ class PaypalCycleNotify(View):
|
|
|
PAY_LOGGER.info('----event_type异常:{}----'.format(event_type))
|
|
|
|
|
|
if resource_type == 'sale' and paypal_body.get('state') == 'completed':
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
response = paypalrestsdk.WebhookEvent.verify(
|
|
|
- transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID, json_agreement_str, cert_url,
|
|
|
- transmission_sig, auth_algo)
|
|
|
+ transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID['Zosi']['paypalCycleNotify'],
|
|
|
+ json_agreement_str, cert_url, transmission_sig, auth_algo)
|
|
|
+ app_type = 1
|
|
|
if not response:
|
|
|
- PAY_LOGGER.info('PayPal周期扣款失败---签名验证失败')
|
|
|
- return HttpResponse('Fail', status=500)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
+ response = paypalrestsdk.WebhookEvent.verify(
|
|
|
+ transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID['Vsees']['paypalCycleNotify'],
|
|
|
+ json_agreement_str, cert_url, transmission_sig, auth_algo)
|
|
|
+ if not response:
|
|
|
+ PAY_LOGGER.info('PayPal周期扣款失败---签名验证失败')
|
|
|
+ return HttpResponse('Fail', status=500)
|
|
|
+ app_type = 2
|
|
|
else:
|
|
|
PAY_LOGGER.info('PayPal周期扣款失败,付款状态有误,resource_type:{},state:{}----'.
|
|
|
format(resource_type, paypal_body.get('state')))
|
|
@@ -597,7 +614,7 @@ class PaypalCycleNotify(View):
|
|
|
store_meal_name = '未知套餐'
|
|
|
Order_Model.objects.create(orderID=orderID, UID=UID, channel=channel, userID_id=userid,
|
|
|
desc=desc, payType=pay_type, payTime=nowTime,
|
|
|
- price=amount.get('total'),
|
|
|
+ price=amount.get('total'), app_type=app_type,
|
|
|
currency=order_qs[0]['currency'], addTime=nowTime,
|
|
|
updTime=nowTime, order_type=order_type,
|
|
|
pay_url='', isSelectDiscounts=0, fee=fee,
|
|
@@ -663,7 +680,8 @@ class PaypalCycleNotify(View):
|
|
|
# 不确定用户地区信息,默认美洲
|
|
|
if not country_qs.exists():
|
|
|
if CONFIG_INFO == CONFIG_EUR:
|
|
|
- PAY_LOGGER.info('PayPal周期扣款失败---不确定地区的用户请求欧洲服,uid:{},country_id:{}'.format(uid, country_id))
|
|
|
+ PAY_LOGGER.info(
|
|
|
+ 'PayPal周期扣款失败---不确定地区的用户请求欧洲服,uid:{},country_id:{}'.format(uid, country_id))
|
|
|
return False
|
|
|
else:
|
|
|
return True
|
|
@@ -699,14 +717,21 @@ class PaypalCycleNotify(View):
|
|
|
amount = paypal_body.get('amount')
|
|
|
|
|
|
# self.get_plan_desc('P-4CG284532S612303METMEINY')
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
response = paypalrestsdk.WebhookEvent.verify(
|
|
|
- transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID_TWO, json_agreement_str, cert_url,
|
|
|
- transmission_sig, auth_algo)
|
|
|
+ transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID['Zosi']['subscriptionBreakNotify'],
|
|
|
+ json_agreement_str, cert_url, transmission_sig, auth_algo)
|
|
|
logger.info('----验证签名----')
|
|
|
logger.info(response)
|
|
|
if not response:
|
|
|
- return HttpResponse('Fail', status=500)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
+ response = paypalrestsdk.WebhookEvent.verify(
|
|
|
+ transmission_id, transmission_time, PAYPAL_WEB_HOOK_ID['Vsees']['subscriptionBreakNotify'],
|
|
|
+ json_agreement_str, cert_url, transmission_sig, auth_algo)
|
|
|
+ logger.info('----验证签名----')
|
|
|
+ logger.info(response)
|
|
|
+ if not response:
|
|
|
+ return HttpResponse('Fail', status=500)
|
|
|
event_type_code = 0
|
|
|
billing_agreement_id = ''
|
|
|
if event_type == 'PAYMENT.SALE.COMPLETED':
|
|
@@ -820,11 +845,14 @@ class payCycle(View):
|
|
|
def do_cancel_pay_cycle(self, request_dict, userID, response):
|
|
|
orderID = request_dict.get('orderID', 'None')
|
|
|
orderObject = Order_Model.objects.filter(orderID=orderID)
|
|
|
- orderObject = orderObject.filter(~Q(agreement_id='')).values("agreement_id")
|
|
|
+ orderObject = orderObject.filter(~Q(agreement_id='')).values("agreement_id", "app_type")
|
|
|
if not orderObject.exists():
|
|
|
return response.json(800)
|
|
|
-
|
|
|
- paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ app_type = orderObject[0]['app_type']
|
|
|
+ if app_type == 1:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Zosi'])
|
|
|
+ elif app_type == 2:
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD['Vsees'])
|
|
|
BILLING_AGREEMENT_ID = orderObject[0]['agreement_id']
|
|
|
try:
|
|
|
billing_agreement = paypalrestsdk.BillingAgreement.find(BILLING_AGREEMENT_ID)
|