Теперь я могу отправить webhook, и он будет получен и вернуть статус 200 в Stripe, но я не понимаю, как получить информацию в json.
{
"object": {
"id": "ch_1D2mVJEiQxrCiRvRgbqdtpbI",
"object": "charge",
"amount": 5000,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"balance_transaction": "txn_1D2mVKEiQxrCiRvRAQ61MjSt",
"captured": true,
"created": 1535144857,
"currency": "usd",
"customer": null,
"description": "A Django charge",
"destination": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {
},
"invoice": null,
"livemode": false,
"metadata": {
},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_1D2mVJEiQxrCiRvRgbqdtpbI/refunds"
},
"review": null,
"shipping": null,
"source": {
"id": "card_1D2mVEEiQxrCiRvRTa2poVVT",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 9,
"exp_year": 2021,
"fingerprint": "p6BWjMSv83IqjoCg",
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "marie.pier.cm@gmail.com",
"tokenization_method": null
},
"source_transfer": null,
"statement_descriptor": null,
"status": "succeeded",
"transfer_group": null
},
"previous_attributes": null
}
Я хочу восстановить созданный (какдата), object.id, object.amount и object.paid, чтобы потом можно было видеть в django admin.
код в views.py -
@require_POST
@csrf_exempt
def webhook(request):
# Retrieve the request's body and parse it as JSON:
jsondata = request.body
data = json.loads(jsondata)
return HttpResponse(status=200)
моделья хотел бы заполнить это так
class StripePayment(models.Model):
paymentId = models.CharField(max_length=200)
paymentAmount = models.CharField(max_length=10)
paid = models.BooleanField()
created = models.DateTimeField()
def __str__(self):
return self.paymentId
я знаю, что это будет что-то вроде
@require_POST
@csrf_exempt
def webhook(request):
jsondata = request.body
data = json.loads(jsondata)
meta = copy.copy(request.META)
for k, v in meta.items():
if not isinstance(v, basestring):
del meta[k]
modelname.objects.create(
.......)
но я не могу понять это, пожалуйста, помогите:)
ИСПРАВЛЕНО ЭТОМ СПОСОБОМ, ЕСЛИ НИКТО НУЖЕН В ОДНОЙ ТОЧКЕ: views.py
@require_POST
@csrf_exempt
def webhook(request):
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
# Invalid payload
return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return HttpResponse(status=400)
# Do something with event
return HttpResponse(status=200)
paymentId = payload['object:id']
amount = payload['object:amount']
paid = payload['paid']
StripePayment.objects.create(
paymentId=paymentId,
amount=amount,
paid=paid,
)
и models.py
class StripePayment(models.Model):
paymentId = models.CharField(max_length=150)
amount = models.CharField(max_length=150)
paid = models.BooleanField()