如何为Django-Oscar添加更多支付方式?已接入货到付款
Hey there! Let's walk through adding a new payment method to your checkout flow. Based on the code snippet you shared, I'll break this down into clear, actionable steps:
First, create a class for your new payment method (typically in a payment/methods.py file in your app). This class will define the method's unique identifier, display name, and any availability rules:
from oscar.apps.payment.methods import PaymentMethod # Adjust import if using a different framework class NewPaymentGateway(PaymentMethod): code = 'new_payment' # Unique code to identify this method name = 'Your New Payment Gateway' # Name shown to users at checkout def is_available(self, order_total): # Add logic to check if this method is available (e.g., minimum order amount) return True # Return False to disable it under certain conditions
Modify your PaymentDetailsView to include both your existing cash-on-delivery method and the new one in the template context. Update the get_context_data method:
class PaymentDetailsView(PaymentDetailsView): template_name = 'checkout/payment-details.html' template_name_preview = 'checkout/preview.html' def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx['signature'] = gateway.hmac_gen(ctx) ctx['amount'] = '%s' % ctx['order_total'].incl_tax # Add all available payment methods to the context from your_app.payment.methods import CashOnDelivery, NewPaymentGateway ctx['payment_methods'] = [ CashOnDelivery(), NewPaymentGateway() ] return ctx
Update your checkout/payment-details.html template to let users select between payment methods. Add a radio button group for each method:
<div class="payment-selection"> <h3>Choose Payment Method</h3> {% for method in payment_methods %} <div class="payment-option"> <input type="radio" id="payment-{{ method.code }}" name="payment_method" value="{{ method.code }}" required> <label for="payment-{{ method.code }}">{{ method.name }}</label> </div> {% endfor %} </div>
Add logic to your view to handle the selected payment method when the user submits the checkout form. Override the post method (or form handling logic):
def post(self, request, *args, **kwargs): selected_method = request.POST.get('payment_method') ctx = self.get_context_data() if selected_method == 'new_payment': # Handle redirection/processing for your new payment gateway return self.process_new_payment(ctx) elif selected_method == 'cash_on_delivery': # Your existing cash-on-delivery processing logic return self.process_cash_on_delivery(ctx) else: messages.error(request, "Invalid payment method selected") return self.render_to_response(ctx) def process_new_payment(self, context): # Add code to interact with your new payment gateway: # 1. Generate a payment request/order # 2. Redirect the user to the gateway's payment page payment_url = gateway.initiate_payment( order=context['order'], amount=context['amount'] ) return redirect(payment_url)
Don't forget to:
- Add your new payment gateway's API credentials to your app settings (e.g., API keys, secrets)
- Set up a webhook endpoint to handle payment status updates (success, failure, cancellation)
- Test the full flow: selecting the new method, completing payment, and verifying order status updates
内容的提问来源于stack exchange,提问作者Artem Goh

