gpt4 book ai didi

python - 在 django-oscar 中集成包含重定向的支付方式

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:30 26 4
gpt4 key购买 nike

我正在使用 django-oscar 框架开发一个购物网站,实际上我正在使用他们的沙盒网站。我想在结帐流程中添加付款,但问题是,我完全糊涂了!

我已阅读此链接:"Oscar's payment integration docs"

我得到了大局的东西。我也在结帐应用程序中阅读了文件 views.py,但我有几个问题在网上找不到。

我的问题是我应该覆盖或创建哪些方法/类来处理以下过程:

用户请求付款后,我应该向银行发送请求并给他们所需的参数(代码中的pay_request_parameters)

然后,他们会发送一个 ID,确认我的访问权限,然后我应该将该 ID 发布到一个地址,并将用户重定向到银行的网页。

用户成功支付银行后,他们会通过我在第一步提供的回调网址发帖通知我。

有了这些信息,我应该验证付款是否成功,如果成功,我应该要求银行结算,然后把钱寄给我。

现在我的代码完成了前两个步骤,但我不知道如何在沙箱中处理重定向后的过程。这是我的代码:

from oscar.apps.checkout import views
from oscar.apps.payment import models
from oscar.apps.payment.exceptions import *
import requests
import datetime

mellat_services_url = 'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl'
start_pay_url = 'https://bpm.shaparak.ir/pgwchannel/startpay.mellat'
terminal_id = 'xxx'
username = 'xxx'
password = 'xxx'

# Subclass the core Oscar view so we can customise
class PaymentDetailsView(views.PaymentDetailsView):

def handle_payment(self, order_number, total, **kwargs):
# Talk to payment gateway. If unsuccessful/error, raise a
# PaymentError exception which we allow to percolate up to be caught
# and handled by the core PaymentDetailsView.

# mellat cycle start
local_date = str(datetime.date.today())[0:4] + str(datetime.date.today())[5:7] + str(datetime.date.today())[8:10]
local_time = str(datetime.datetime.now().time())[0:2] + str(datetime.datetime.now().time())[3:5] + str(datetime.datetime.now().time())[6:8]
# call bpPayRequest and get refId
pay_request_parameters = {'terminalId': terminal_id, 'userName': username,
'userPassword': password, 'orderId': order_number,
'amount': total.incl_tax, 'localDate': local_date,
'localTime': local_time, 'additionalData': ""
'callBackUrl': 'mysite.com/checkout/preview/'}

pay_request_answer = requests.post(mellat_services_url, pay_request_parameters)

if not pay_request_answer.split(",")[0] == 0:
response_code = pay_request_answer.split(",")[0]
if response_code[0] == '1':
raise UnableToTakePayment()
else:
raise PaymentError()

requests.post(start_pay_url, pay_request_answer.split(",")[1])
raise RedirectRequired(start_pay_url)

# post the refId to bank and then redirect customer to the bank
# apparently wait for the bank ( like for 10 mins ) to get the payment status
# if the bank responded with success, the you verify the payment with a post to the bank
# if everything was verified, tell the bank for a settlement

# mellat cycle end

#The rest should be implemented but I dont know where I should put this
#All I know is that it should be done after the verification with the data
#sent from the bank.

reference = gateway.pre_auth(order_number, total.incl_tax, kwargs['bankcard'])

# Payment successful! Record payment source
source_type, __ = models.SourceType.objects.get_or_create(
name="SomeGateway")
source = models.Source(
source_type=source_type,
amount_allocated=total.incl_tax,
reference=reference)
self.add_payment_source(source)

# Record payment event
self.add_payment_event('pre-auth', total.incl_tax)

提前致谢。

最佳答案

我有一个类似的问题,我所做的是将银行的 callBackUrl 重定向到一个 View ,该 View 的实现方式如下:

class CustomCheckoutDone(OrderPlacementMixin, RedirectView):
"""
here we verify payment was done and place the actual order
then redirect to thank you page
"""
permanent = False

def get_redirect_url(self, pk):
basket = Basket.objects.get(pk=self.checkout_session.get_submitted_basket_id())
basket.strategy = CustomStrategy()
order_number = self.checkout_session.get_order_number()
shipping_address = self.get_shipping_address(basket)
shipping_method = self.get_shipping_method(basket, shipping_address)
shipping_charge = shipping_method.calculate(basket)
billing_address = self.get_billing_address(shipping_address)
order_total = self.get_order_totals(basket, shipping_charge=shipping_charge)
order_kwargs = {}
# make sure payment was actually paid
CustomPayment.objects.get(order_number=order_number, payed_sum=str(float(order_total.incl_tax)))
user = self.request.user
if not user.is_authenticated():
order_kwargs['guest_email'] = self.checkout_session.get_guest_email()
self.handle_order_placement(
order_number, user, basket, shipping_address, shipping_method,
shipping_charge, billing_address, order_total, **order_kwargs
)
return '/checkout/thank-you/'

关于python - 在 django-oscar 中集成包含重定向的支付方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com