如何将Stripe付款网关与Django Oscar集成? [英] How to integrate Stripe payments gateway with Django Oscar?
问题描述
我正在尝试将Stripe付款网关集成到Django oscar上,以建立一个在线销售杂货等实物商品的电子商务网站,我使用python 3.6.3,Django 2.0,Django-oscar 1.6,stripe 1.82.2。
I am trying to integrate the Stripe payment gateway to Django oscar for an e-commerce site which sells physical goods like groceries online.I use python 3.6.3, Django 2.0, Django-oscar 1.6, stripe 1.82.2.
方法1 :
所以我在django-oscar组中遵循了此链接:
So I followed this link in django-oscar groups:
我已经注册了一个Stripe帐户,并使用我的可发布密钥和测试密钥来配置Stripe。问题是,当我尝试使用随附的按钮进行付款时标签用卡付款,它收集了我的卡信息,然后当我单击按钮时,它显示有些钱将从卡中扣除,如下图所示:
预览页面的图像
I have signed up for a stripe account and used my publishable key and test key to configure stripe.The problem is, when I try to pay using the button provided with label "Pay with Card",it collects my card information and then when I click the button, it shows "Some money will be debited from the card" like in this image: Image of Preview page
然后,在我点击下订单按钮后,它显示给我这个:
确认页面的图像
Then after I click the place order button,it shows me this: Image of confirmation page
尽管我已使用卡付款。
我想奥斯卡似乎还不知道这笔款项已经通过条纹完成了,但是我不确定如何解决。
Though I have paid using my card. I guess oscar doesn't seem to know that the payment has been done through stripe already?But I'm not sure how to solve this.
方法2 :
我尝试使用dj-stripe,位于此处:
Method 2: I tried to use dj-stripe,found here:
https://github.com/dj-stripe/dj-stripe
但是我在 https://dj-stripe.readthedocs.io/en上阅读了整个文档/stable-1.0/ ,似乎我只能将其用于需要订阅的产品,我不需要订阅,并且dj-stripe的文档还不完整。
But I read the whole documentation on https://dj-stripe.readthedocs.io/en/stable-1.0/ , it seems like I can use it only for products that need subscriptions, mine don't require subscribing and the docs for dj-stripe aren't fully complete.
我尝试了django-oscar官方仓库,链接在这里:
https://github.com/django-oscar/django-oscar-stripe
,此存储库已有5年历史了,我认为它不兼容用于我的Django oscar版本。
I tried with the official django-oscar repo, the link is here: https://github.com/django-oscar/django-oscar-stripe , this repository is like five years old and I don't think it would be compatible to use with my version of Django oscar.
方法3 :
我尝试使用stripe.js和元素并创建了我的表单以接受卡:
Method 3: I tried using the stripe.js and elements and created my form to accept cards:
< script src = "https://js.stripe.com/v3/" > < /script> <
script >
var stripe = Stripe('your_stripe_publishable_key');
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
var style = {
base: {
color: '#32325d',
lineHeight: '18px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '20px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
var card = elements.create('card', {
style: style
});
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Create a source or display an error when the form is submitted.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createSource(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the source to your server
stripeSourceHandler(result.source);
}
});
});
function stripeSourceHandler(source) {
// Insert the source ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
var hiddenAmount = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeSource');
hiddenInput.setAttribute('value', source.id);
form.appendChild(hiddenInput);
hiddenAmount.setAttribute('type', 'hidden');
hiddenAmount.setAttribute('name', 'amt');
hiddenAmount.setAttribute('value', '{{ order_total.incl_tax|safe }}');
form.appendChild(hiddenAmount);
// Submit the form
form.submit();
}
<
/script>
<form action="/charge/" method="post" id="payment-form">
{% csrf_token % }
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display Element errors. -->
<div id="card-errors" role="alert"></div>
</div>
<br>
<!--<hr>-->
<button class="btn btn-primary">Pay Now</button>
</form>
在我的python views.py文件中,我创建了条带电荷并也提供了源。
And in my python views.py file I create a stripe charge and also sources.
@csrf_exempt
def stripe_payment(request):
user = request.user
source_id = request.POST.get("stripeSource", None)
amount = request.POST.get("amt", None)
stripe.api_key = "your_test_key"
customer = stripe.Customer.create(
email=email,
source=source_id,
)
# print("Customer ID: ", customer['id'])
amt = float(amount) * 100
# print("Amount:", int(amt))
int_amt = int(amt)
charge = stripe.Charge.create(
amount=int_amt,
currency='cad',
customer=customer['id'],
source=source_id,
)
return HttpResponseRedirect("/checkout/preview/")
然后我c在条纹仪表板中创建一个Webhook并将其链接到我的本地URL,每次通过Web钩子发送的Stripe都有响应时,此URL被命中。
Then I created a webhook in the stripe dashboard and linked it to my local url , every-time there is a response from stripe being sent through the web-hook, this url is hit.
@csrf_exempt
def demo_checkout(request):
# Retrieve the request's body and parse it as JSON:
event_json = json.dumps(json.loads(request.body), indent=4)
# event_json = json.loads(request.body)
# Do something with event_json
print("Json event:", event_json)
return HttpResponse(status=200)
截至目前,我可以从仪表板跟踪各种事件或日志,以及诸如创建客户,进行收费以及通过网络挂钩发送响应之类的事件都可以,但是我不知道该如何完成付款,使得Django-oscar也可以知道付款已经完成,并且不显示不需要付款:
谢谢您的页面
As of now, I can track the various events or logs from my dashboard and the events like creating a customer, making a charge and the web-hook sending a response is working fine,but I can't figure out how can I complete the payment such that Django-oscar can also know that the payment is done and it doesn't show "No payment was required": Thank you page
我已经尝试了所有这些方法,但是仍然无法使用。我可以使用任何其他建议的方法或我对django-oscar并不陌生,提供了一些代码和一些解释的答案将是有帮助的。
I have tried all these methods,but it still doesn't work.I am open to using any other method which is suggested or an improvement to what I have done in any of the methods explained so far.I am new to django-oscar and an answer with some code and some explanation would be helpful.
推荐答案
我找到了一种将Stripe与Django Oscar集成的方法,这是实现它的一种简单方法。
I have found a way to integrate Stripe with Django Oscar, this is one of the easy ways to do it.
-
首先从此处创建一个条纹帐户: https://stripe.com/ ,您将获得一个可发布的密钥和一个秘密密钥,登录开发人员> API密钥下的条纹仪表板后,即可查看它们。
Create a stripe account first from here:https://stripe.com/, you will get a publishable key and a secret key, you can view them after logging in into the stripe dashboard under Developers > API keys.
在您的Django中奥斯卡代码方面。从oscar分支出结帐应用程序,将其添加到INSTALLED_APPS + = get_core_apps(['checkout'])。要了解如何派生应用程序,可以通过以下文档中的链接进行操作: https://django-oscar.readthedocs.io/zh-CN/latest/topics/customisation.html# fork-oscar-app
In your django oscar code side. Fork the checkout app from oscar, add it to the INSTALLED_APPS+=get_core_apps(['checkout']).To know how to fork an app you can follow this link from the docs:https://django-oscar.readthedocs.io/en/latest/topics/customisation.html#fork-oscar-app
在结帐时创建一个名为facade.py的文件,将密钥从仪表板复制到settings.py文件中,然后进行此链接中建议的其他更改:在django oscar小组上进行条带化支付网关集成,恰好标题错误。只需按照整个页面操作就可以完成。
Create a file called facade.py under checkout, copy the keys from your dashboard into the settings.py file and do the other changes as suggested in this link: Stripe payment gateway integration on the django oscar groups, it just happens to be titled wrong.Just follow this whole page and it's done.
这篇关于如何将Stripe付款网关与Django Oscar集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!