I hope you all are well.
Currently, I'm working on a website where I'm selling game items like gold and characters. I'm receiving payment through Coinbase. I want to send a message in Zendesk chatbox from the user who paid, after successful payment on Coinbase.
Tech I'm using
- Django for backend
- HTML, CSS and JS for front-end
Things I've done so far
- I've managed to receive money in my Coinbase account and Coinbase redirects to my website after successful payment. I'm doing all this by the following code.
def buy_osrs(request):
obj = Setting.objects.first()
osrs_price = getattr(obj, "osrs_price")
osrs_fee = getattr(obj, "osrs_fee")
form = OSRSForm()
site = get_current_site(request).domain
payment_success = f"https://{site}/payment/"
if request.method == "POST":
form = OSRSForm(request.POST)
if form.is_valid():
form.save()
total = float(form.cleaned_data.get("osrs_numbers")) * osrs_price + osrs_fee
charge_info = {
"name": "My Website",
"description": "My Website Description",
"local_price": {"amount": total, "currency": "USD"},
"pricing_type": "fixed_price",
"metadata": {"rsn": form.cleaned_data.get("rsn"), "email": form.cleaned_data.get("email")},
"redirect_url": payment_success,
}
charge = client.charge.create(**charge_info)
return redirect(charge.get("hosted_url"))
context = {
"form": form,
"osrs_price": osrs_price,
"osrs_fee": osrs_fee,
}
return render(request, "main/buy_osrs.html", context)
- I've managed to set up and receive webhooks, which change my database according to the response it gets from Coinbase.
@csrf_exempt
def coinbase_webhook(request):
if request.method == "POST":
# event payload
request_data = request.body.decode("utf-8")
# webhook signature
request_sig = request.headers.get("X-CC-Webhook-Signature")
try:
# signature verification and event object construction
event = Webhook.construct_event(request_data, request_sig, settings.SECRET_KEY)
obj, created = CoinbaseOrder.objects.get_or_create(order_id=event.id, code=event.data.code)
if event.type == "charge:pending":
obj.status = "Pending Confirmation"
obj.save()
elif event.type == "charge:confirmed":
obj.status = "Payment Confirmed"
obj.save()
elif event.type == "charge:delayed":
obj.status = "Payment Delayed"
obj.save()
elif event.type == "charge:resolved":
obj.status = "Payment Resolved"
obj.save()
elif event.type == "charge:failed":
obj.status = "Payment Failed"
obj.save()
except (WebhookInvalidPayload, SignatureVerificationError) as e:
return str(e), 400
return HttpResponse()
Things that I want
Now my requirement is whenever event.type == "charge:confirmed" happen, an automatic message should be sent in Zendesk live chatbox from end-user saying, Hi, I'm ABC. I've paid you XYZ amount.
I think this is not possible through webhook, I have to use jquery or some other technology for this. So kindly guide me what's the best strategy