Skip to content

Commit

Permalink
Merge pull request #6 from MartinMikita/webhook-callback
Browse files Browse the repository at this point in the history
Change processing webhook to prevent losing successful order.
  • Loading branch information
keosak authored May 4, 2017
2 parents ecc8903 + 1a55a84 commit 246dedf
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions flask_fastspring.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,23 @@ def milliseconds_to_datetime(m):
<script type="text/javascript">
var fscSession = {{ session|tojson }};
{% if webhook %}
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
function fastspringOnPopupClosed(data) {
var fastspringRedirectUrl;
function fastspringOnPopupWebhookReceived(data) {
if (!data) return;
var xhr = new XMLHttpRequest();
xhr.open("POST", "{{ webhook }}", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
window.onbeforeunload = function() {};
window.location.replace("{{ request.url }}");
fastspringRedirectUrl = '{{ request.url }}';
} else if (xhr.status === 201 || (301 <= xhr.status && xhr.status <= 303)) {
window.onbeforeunload = function() {};
window.location.replace(xhr.getResponseHeader("Location"));
fastspringRedirectUrl = xhr.getResponseHeader("Location");
} else {
window.onbeforeunload = function() {};
var message = "ERROR: Could not process order: " + data["reference"];
console.log(message);
alert(message);
fastspringRedirectUrl = null;
}
}
};
Expand All @@ -276,13 +271,22 @@ def milliseconds_to_datetime(m):
"payload": {{ payload|tojson }}
}));
}
function fastspringOnPopupClosed(data) {
if (!data) return;
if (fastspringRedirectUrl) {
window.location.replace(fastspringRedirectUrl);
}
}
{% endif %}
</script>
<script
id="fsc-api"
src="https://d1f8f9xcsvx3ha.cloudfront.net/sbl/0.7.2/fastspring-builder.min.js"
type="text/javascript"
{% if webhook %}data-popup-closed="fastspringOnPopupClosed"{% endif %}
{% if webhook %}
data-popup-webhook-received="fastspringOnPopupWebhookReceived"
data-popup-closed="fastspringOnPopupClosed"
{% endif %}
{% if access_key %}data-access-key="{{ access_key }}"{% endif %}
data-storefront="{{ storefront }}">
</script>
Expand Down

0 comments on commit 246dedf

Please sign in to comment.