我正在尝试将 Razorpay 支付网关集成到默认的 Django-Oscar 项目中。我不知道如何将 checkout.js 生成的付款 id 传递到 Django 服务器以捕获付款。
根据我有限的理解,Razorpay的checkout.js代码在每次用户填写支付信息并发起授权后都会生成一个唯一的支付id。
payment_details.html
<form action="{% url 'checkout:preview' %}" method="POST">
<!-- Note that the amount is in its subunit value = 599 -->
<script
src="https://checkout.razorpay.com/v1/checkout.js"
data-key="rzp_test_XXUUnffGta42gL"
data-amount="{{ order_total_incl_tax_cents }}"
data-currency="INR"
data-buttontext="Pay with Razorpay"
data-name="{{ shop_name }}"
data-description="{{ basket.num_items }} items ({{ order_total.incl_tax|currency }})"
data-image="https://your-awesome-site.com/your_logo.jpg"
data-prefill.name="Gaurav Kumar"
data-prefill.email="[email protected]"
data-theme.color="#F37254"
></script>
<input type="hidden" value="Hidden Element" name="hidden">
</form>
我已经分叉了结帐应用程序。
结帐/__init__.py
PAYMENT_EVENT_PURCHASE = 'Purchase'
PAYMENT_METHOD_STRIPE = 'Razorpay'
RAZORPAY_EMAIL = 'razorpayEmail'
RAZORPAY_TOKEN = 'razorpayToken'
default_app_config = 'checkout.apps.CheckoutConfig'
结帐/forms.py
class StripeTokenForm(forms.Form):
razorpayEmail = forms.EmailField(widget=forms.HiddenInput())
razorpayToken = forms.CharField(widget=forms.HiddenInput())
结帐/views.py
class PaymentDetailsView(CorePaymentDetailsView):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(PaymentDetailsView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
if self.preview:
ctx['razorpay_token_form'] = forms.RazorpayTokenForm(self.request.POST)
ctx['order_total_incl_tax_cents'] = (
ctx['order_total'].incl_tax * 100
).to_integral_value()
else:
ctx['order_total_incl_tax_cents'] = (
ctx['order_total'].incl_tax * 100
).to_integral_value()
ctx['razorpay_publishable_key'] = settings.RAZORPAY_PUBLISHABLE_KEY
return ctx
def handle_payment(self, order_number, total, **kwargs):
razorpay_ref = Facade().charge(
order_number,
total,
card=self.request.POST[RAZORPAY_TOKEN],
description=self.payment_description(order_number, total, **kwargs),
metadata=self.payment_metadata(order_number, total, **kwargs))
source_type, __ = SourceType.objects.get_or_create(name=PAYMENT_METHOD_RAZORPAY)
source = Source(
source_type=source_type,
currency=settings.RAZORPAY_CURRENCY,
amount_allocated=total.incl_tax,
amount_debited=total.incl_tax,
reference=razorpay_ref)
self.add_payment_source(source)
self.add_payment_event(PAYMENT_EVENT_PURCHASE, total.incl_tax)
def payment_description(self, order_number, total, **kwargs):
return self.request.POST[RAZORPAY_EMAIL]
def payment_metadata(self, order_number, total, **kwargs):
return {'order_number': order_number}
结帐/facade.py
from django.conf import settings
from oscar.apps.payment.exceptions import UnableToTakePayment, InvalidGatewayRequestError
import razorpay
class Facade(object):
def __init__(self):
client = razorpay.Client(auth=("rzp_test_XXUUnrrBmW42gL", "32Cqrj2UvxOT24nUbtO7Zacr"))
@staticmethod
def get_friendly_decline_message(error):
return 'The transaction was declined by your bank - please check your bankcard details and try again'
@staticmethod
def get_friendly_error_message(error):
return 'An error occurred when communicating with the payment gateway.'
def charge(self,
order_number,
total,
card,
currency=settings.STRIPE_CURRENCY,
description=None,
metadata=None,
**kwargs):
try:
return client.payment.capture(card, total)
except razorpay.error.CardError as e:
raise UnableToTakePayment(self.get_friendly_decline_message(e))
except razorpay.error.RazorpayError as e:
raise InvalidGatewayRequestError(self.get_friendly_error_message(e))
由于 Razorpay 结账流程与 Stripe 类似,因此我使用了与 Stripe 集成相同的结账流程。如果我能得到前端生成的付款id,付款捕获就可以成功。
我尝试将 Razorpay 集成到 django oscar 中,但是这段代码无法正常工作 你能帮我如何与更新的代码集成吗