1

I have been trying to hide checkout process of my saleor site, because I think the process choosing payment gateway before payment could make people confused.

I have searched a lot of documents and tried to understand and modified codes related to payment process for almost a week. It was not successful because of my poor understanding of django framework.

Please, help me and let me know some hints about where and what should I look into. If it is possible, teach me how to set default payment gateway and hide options for choosing payment gateway.

I think it is related to three files.

  1. saleor/templates/order/pavement.html By modifying pavement template, I succeed in making default payment gateway and hiding radio buttons but whenever it was hidden, payment processing was also not proceed( In successful payment, the url should be changed from 'http://127.0.0.1:8000/ko/order/[ order token ]/payment/' to 'http://127.0.0.1:8000/ko/order/[ order token ]/payment/[ payment gateway name ]/' but when I hided radio button, the url was not changed. )

  2. saleor/saleor/order/views.py

  3. saleor/saleor/order/forms.py

    I guessed the payment gateway information is transferring from payment.html to start_payment function of views.py and somewhat related to PaymentsForms class in forms.py. I have been trying to analysis and understand these codes but I got stuck.

some part of code in 'saleor/templates/order/pavement.html'

            <h3>
              <b>{% trans "Choose payment method" %}</b>
            </h3>
            <div class="checkout__payment-form">
              <form method="post" action="{% url "order:payment" token=order.token %}" class="payment-form radio">
                {% csrf_token %}
                {% for choice in payment_form.gateway %}
                  <div class="radio checkout__payment-form__choice">
                    <label>
                      <div class="card card-default">
                        <div class="card-body">
                          <strong>
                            {{choice}}
                          </strong>
                        </div>
                      </div>
                    </label>
                  </div>
                {% endfor %}
                {% block form_submit %}
                  <button type="submit" class="btn btn-primary">
                    {% trans "Proceed to payment" context "Payment details primary action" %}
                  </button>
                {% endblock %}
              </form>
            </div>

'start_payment' function in 'saleor/saleor/order/views.py'

@check_order_status
def start_payment(request, order, gateway):
    payment_gateway, connection_params = get_payment_gateway(gateway)
    #payment_gateway, connection_params = get_payment_gateway('Dummy gateway')
    extra_data = {'customer_user_agent': request.META.get('HTTP_USER_AGENT')}
    with transaction.atomic():
        payment = create_payment(
            gateway=gateway,
            currency=order.total.gross.currency,
            email=order.user_email,
            billing_address=order.billing_address,
            customer_ip_address=get_client_ip(request),
            total=order.total.gross.amount,
            order=order,
            extra_data=extra_data)

        if (order.is_fully_paid()
                or payment.charge_status == ChargeStatus.FULLY_REFUNDED):
            return redirect(order.get_absolute_url())

        payment_info = create_payment_information(payment)
        form = payment_gateway.create_form(
            data=request.POST or None,
            payment_information=payment_info,
            connection_params=connection_params)
        if form.is_valid():
            try:
                gateway_process_payment(
                    payment=payment, payment_token=form.get_payment_token())
            except Exception as exc:
                form.add_error(None, str(exc))
            else:
                if order.is_fully_paid():
                    return redirect('order:payment-success', token=order.token)
                return redirect(order.get_absolute_url())

    client_token = payment_gateway.get_client_token(
        connection_params=connection_params)
    ctx = {
        'form': form,
        'payment': payment,
        'client_token': client_token,
        'order': order}
    return TemplateResponse(request, payment_gateway.TEMPLATE_PATH, ctx)

'PaymentsForms' class in 'saleor/saleor/order/forms.py'

PAYMENT_CHOICES = [
    (k, v) for k, v in settings.CHECKOUT_PAYMENT_GATEWAYS.items()]


class PaymentsForm(forms.Form):
    gateway = forms.ChoiceField(
        label=pgettext_lazy('Payments form label', 'Payment Method'),
        choices=PAYMENT_CHOICES,
        initial=PAYMENT_CHOICES[0][0],
        widget=forms.RadioSelect)

I tried to set default payment gateway and hide radio box for choosing payment gateways, but failed.

4

1 回答 1

0

我只修改了几行代码就解决了你的问题。

我开始只选择一个支付网关(对我来说:BrainTree),为此您只需插入您希望在电子商务中允许的支付网关。修改CHECKOUT_PAYMENT_GATEWAYS文件中的dict saleor/settings.py

CHECKOUT_PAYMENT_GATEWAYS = {
  BRAINTREE: pgettext_lazy('Payment method name', 'Brain tree')
}

然后,您必须将用户从存在单选按钮的页面重定向到唯一的一个付款选项页面。修改payment文件中的函数saleor/order/views.py并添加我调用的第二个函数exist_one_gateway

def payment(request, token):
    [...]
    if not order.is_pre_authorized():
        payment_form = PaymentsForm(form_data)
        if payment_form.is_valid():
            payment = payment_form.cleaned_data['gateway']
            return redirect(
                'order:payment', token=order.token, gateway=payment)
        elif exist_one_gateway():
            payment = list(settings.CHECKOUT_PAYMENT_GATEWAYS.items())[0][0]
            return redirect(
                'order:payment', token=order.token, gateway=payment)
    [...]
    return TemplateResponse(request, 'order/payment.html', ctx)


    def exist_one_gateway():
        gateway = list(settings.CHECKOUT_PAYMENT_GATEWAYS.items())
    return len(gateway) == 1

payment功能适用​​于两个不同的页面,第一个页面用于您必须单击要选择的支付网关的单选按钮的页面(我们要跳过的页面)和特定的支付选项页面。

出于这个原因,我添加了一个条件,如果 payment_form 无效(这意味着没有人提交付款),则验证是否仅存在一个付款选项。如果True,用户将被重定向到在前一个文件中选择的特定支付页面。

我希望我是有帮助的。

于 2019-06-13T09:26:04.747 回答