在 Django 应用程序中设置 Shopify RecurringApplicationCharge 时出现问题

问题描述 投票:0回答:1

我正在参考 https://github.com/Shopify/shopify_django_app 存储库遵循应用程序结构。我正在尝试使用 shopify.RecurringApplicationCharge 为我的应用程序设置计费。

@shop_login_required
def buy_plan(request, id):
    plan = PlanTable.objects.get(id=id)
    context = {'plans': plan}
    charge = shopify.RecurringApplicationCharge()
    charge.name = plan.name
    charge.price = plan.price
    charge.test = True
    charge.return_url = reverse('accept')
    if charge.response_code == 201:
        confirmation_url = charge['recurring_application_charge']['confirmation_url']
        return redirect(confirmation_url)
    else:
        return render(request, 'home/billing.html', context)

我还尝试了以下内容,如https://github.com/Shopify/shopify_python_api#billing

中提到的
@shop_login_required
def buy_plan(request, id):
    plan = PlanTable.objects.get(id=id)
    context = {'plans': plan}
    charge = shopify.RecurringApplicationCharge.create({'name' : plan.name,'price' : plan.price, 'test' : True,'return_url' : reverse('accept')})
    if charge.response_code == 201:
        confirmation_url = charge['recurring_application_charge']['confirmation_url']
        return redirect(confirmation_url)
    else:
        return render(request, 'home/billing.html', context)

但是这两种方法都返回以下错误:-

ForbiddenAccess at /buy_plan/1
Response(code=403, body="b''", headers={'Date': 'Fri, 18 Aug 2023 12:52:49 GMT', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'close', 'X-Sorting-Hat-PodId': '319', 'X-Sorting-Hat-ShopId': '72702755136', 'Vary': 'Accept-Encoding', 'Referrer-Policy': 'origin-when-cross-origin', 'X-Frame-Options': 'DENY', 'X-ShopId': '72702755136', 'X-ShardId': '319', 'X-Stats-UserId': '', 'X-Stats-ApiClientId': '54612525057', 'X-Stats-ApiPermissionId': '604913992000', 'X-Shopify-API-Version': 'unstable', 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT': '1/40', 'X-Shopify-Shop-Api-Call-Limit': '1/40', 'Strict-Transport-Security': 'max-age=7889238', 'Server-Timing': 'processing;dur=92', 'X-Shopify-Stage': 'production', 'Content-Security-Policy': "default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopifycdn.net https://checkout.shopifycs.com https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Frecurring_application_charges&source%5Bsection%5D=admin_api&source%5Buuid%5D=99c55ce8-b483-4005-a18e-add193a2114c", 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Permitted-Cross-Domain-Policies': 'none', 'X-XSS-Protection': '1; mode=block; report=/xss-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Frecurring_application_charges&source%5Bsection%5D=admin_api&source%5Buuid%5D=99c55ce8-b483-4005-a18e-add193a2114c', 'X-Dc': 'gcp-asia-south1,gcp-us-central1,gcp-us-central1', 'X-Request-ID': '99c55ce8-b483-4005-a18e-add193a2114c', 'CF-Cache-Status': 'DYNAMIC', 'Report-To': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=JctaWU1iSmnNUr84N9i4Y8IDKVFPQBgV32a%2B5OseIheGbwENjrnosLQL0iyMKySYWpaVtGa91T7HNNKETgY4ap0msO4km8R%2FJF44TWKAA9UJeuBK0Q7%2FGqp7P%2BamF02SuTR85Wyry3pvQw%3D%3D"}],"group":"cf-nel","max_age":604800}', 'NEL': '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}', 'Server': 'cloudflare', 'CF-RAY': '7f8a4bceabcc17ab-MAA', 'alt-svc': 'h3=":443"; ma=86400'}, msg="Forbidden")

我的 urls.py 文件:

path('buy_plan/<str:id>', views.buy_plan, name='buy_plan'),
path('accept', views.accept, name='accept'),

我的 billing.html 文件:

{% extends "base.html" %}
{% block content %}
  
<h2>Plans</h2>
{% if plans %}
<div class="product-list">
  {% for plan in plans %}
  <div class="product-card">
    
    <div class="product-info">
      <h1><b>Name: </b>{{plan.name}}</h1>
      <h2><b>Price: </b>{{plan.price}}</h2>
      <a href="{% url 'buy_plan' plan.id %}">
        <button class="custom-btn btn-2">Activate</button>
      </a>
    </div>
  </div>
  {% endfor %}
</div>
{% else %}
  <em class="note">There are no plans.</em>
{% endif %}

{% endblock %}

任何发生此错误的原因以及如何消除它的详细信息将不胜感激。

python django shopify shopify-app shopify-api
1个回答
0
投票

这个问题就解决了。问题是shopify 要求redirect_uri 为HTTPS,否则RecurringApplicationCharge API 请求将无法通过。

© www.soinside.com 2019 - 2024. All rights reserved.