Ruby on rails 自定义比特币支付
我收到一位客户的电子邮件,说他们在比特币/条纹支付后从未收到过他们的产品。问题是,我找不到在沙箱中彻底测试比特币的方法,所以我不确定我的实现是否正常工作。沙箱会自动填充接收者,所以我会收到付款通知,一切都正常。但是,当运行live时,我不确定我的轮询是否正常工作。我在下面发布了我所有的相关代码,有人能看到代码中的任何缺陷吗 *我的商店在Rails(4.2.6)上运行Ruby(2.3.1p112) 我的付款\u bitcoin.html.erbRuby on rails 自定义比特币支付,ruby-on-rails,ruby-on-rails-4,stripe-payments,bitcoin,Ruby On Rails,Ruby On Rails 4,Stripe Payments,Bitcoin,我收到一位客户的电子邮件,说他们在比特币/条纹支付后从未收到过他们的产品。问题是,我找不到在沙箱中彻底测试比特币的方法,所以我不确定我的实现是否正常工作。沙箱会自动填充接收者,所以我会收到付款通知,一切都正常。但是,当运行live时,我不确定我的轮询是否正常工作。我在下面发布了我所有的相关代码,有人能看到代码中的任何缺陷吗 *我的商店在Rails(4.2.6)上运行Ruby(2.3.1p112) 我的付款\u bitcoin.html.erb <%= render :partial =&g
<%= render :partial => "offsite_checkout_summary" %>
<p>Your license key, along with your purchase receipt, will be sent to <strong><%= @order.licensee_name %></strong> at <strong><%= @order.email %></strong> once payment has been confirmed.</p>
<div id="extra_purchase_notes">
<em><strong>Note:</strong> The bitcoin link and price provided is only valid for <span id="countdown_time">10:00</span> minutes.</em>
</div>
<div class="d cl"></div>
<%= render :partial => "items_checkout_summary", :locals => { order: @order } %>
<div id="bitcoin_payment_address">
<script src="https://js.stripe.com/v2/stripe.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<%= $STRIPE_PREFS['stripe_publishable_key'] %>');
function populateBitcoinCheckout(status, response) {
if (status === 200) {
document.getElementById("bitcoin_total").innerHTML = " (" + response.bitcoin_amount/100000000 + " BTC)";
document.getElementById("bitcoin_payment_string").innerHTML = 'Please send: <strong>' + response.bitcoin_amount/100000000 + ' BTC</strong> to <strong>' + '<a href="' + response.bitcoin_uri + '&label=Software+Purchase">' + response.inbound_address + '</a></strong>';
document.getElementById("btc-button").href = response.bitcoin_uri + '&label=Software+Purchase';
//poll reciever
Stripe.bitcoinReceiver.pollReceiver(response.id, filledReceiverHandler(response));
//configure timer
function startTimer(duration, countdown) {
var timer = duration,minutes,seconds;
var t = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
countdown.textContent = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(t);
document.getElementById("bitcoin_total").innerHTML = "";
document.getElementById("bitcoin_payment_string").innerHTML = "";
document.getElementById("bitcoin_button_text").innerHTML = "Refresh Order"
document.getElementById("btc-button").href = "javascript:history.back()"
document.getElementById("extra_purchase_notes").innerHTML = "<em><strong>Oops...</strong> This order has expired, use the Refresh Order button to retry.</em>"
Stripe.bitcoinReceiver.cancelReceiverPoll(response.id);
}
}, 1000);
}
//start timer
var countdown = document.getElementById('countdown_time');
startTimer(600, countdown);
} else {
document.getElementById("bitcoin_uri_string").innerHTML = JSON.stringify(response);
Stripe.bitcoinReceiver.cancelReceiverPoll(response.id);
}
}
Stripe.bitcoinReceiver.createReceiver({
amount: "<%= (@order.total * 100).round %>",
currency: 'usd',
description: 'Software purchase',
email: "<%= @order.email %>"
}, populateBitcoinCheckout);
function filledReceiverHandler(response)
{
if (response.filled === true) {
function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
if ( typeof value == 'object' || typeof value == 'array' ){
$.each(value, function(subkey, subvalue) {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key+'[]');
field.attr("value", subvalue);
form.append(field);
});
} else {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
}
});
$(document.body).append(form);
form.submit();
}
post('purchase_bitcoin', response);
}
}
</script>
</div>
</div>
<p id="bitcoin_payment_string"></p>
<div class="d"></div>
<p style="text-align: right;">
<a id="btc-button"><button class="bitcoin-button" style="visibility: visible;"><span id="bitcoin_button_text">Pay with Bitcoin</span></button></a>
</p>
最后,我将轮询逻辑添加到计时器中,这似乎起到了作用。。。。尽管文档中暗示我不需要手动投票?至少我是这么看的 这是我对bitcoin.html.erb的完整修订版,供其他任何遇到问题的人使用
<%= render :partial => "offsite_checkout_summary" %>
<p>Your license key, along with your purchase receipt, will be sent to <strong><%= @order.licensee_name %></strong> at <strong><%= @order.email %></strong> once payment has been confirmed.</p>
<div id="extra_purchase_notes">
<em><strong>Note:</strong> The bitcoin link and price provided is only valid for <span id="countdown_time">10:00</span> minutes.</em>
</div>
<div class="d cl"></div>
<%= render :partial => "items_checkout_summary", :locals => { order: @order } %>
<div id="bitcoin_payment_address">
<script src="https://js.stripe.com/v2/stripe.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<%= $STRIPE_PREFS['stripe_publishable_key'] %>');
function populateBitcoinCheckout(status, response) {
if (status === 200) {
document.getElementById("bitcoin_total").innerHTML = " (" + response.bitcoin_amount/100000000 + " BTC)";
document.getElementById("bitcoin_payment_string").innerHTML = 'Please send: <strong>' + response.bitcoin_amount/100000000 + ' BTC</strong> to <strong>' + '<a href="' + response.bitcoin_uri + '&label=Software+Purchase">' + response.inbound_address + '</a></strong>';
document.getElementById("btc-button").href = response.bitcoin_uri + '&label=Software+Purchase';
//configure timer
function startTimer(duration, countdown) {
var timer = duration,minutes,seconds;
var t = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//poll reciever
Stripe.bitcoinReceiver.pollReceiver(response.id, filledReceiverHandler(response));
countdown.textContent = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(t);
document.getElementById("bitcoin_total").innerHTML = "";
document.getElementById("bitcoin_payment_string").innerHTML = "";
document.getElementById("bitcoin_button_text").innerHTML = "Refresh Order"
document.getElementById("btc-button").href = "javascript:history.back()"
document.getElementById("extra_purchase_notes").innerHTML = "<em><strong>Oops...</strong> This order has expired, use the Refresh Order button to retry.</em>"
Stripe.bitcoinReceiver.cancelReceiverPoll(response.id);
}
}, 1000);
}
//start timer
var countdown = document.getElementById('countdown_time');
startTimer(600, countdown);
} else {
document.getElementById("bitcoin_uri_string").innerHTML = JSON.stringify(response);
}
}
Stripe.bitcoinReceiver.createReceiver({
amount: "<%= (@order.total * 100).round %>",
currency: 'usd',
description: 'Software purchase',
email: "<%= @order.email %>"
}, populateBitcoinCheckout);
function filledReceiverHandler(response)
{
if (response.filled === true) {
function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
if ( typeof value == 'object' || typeof value == 'array' ){
$.each(value, function(subkey, subvalue) {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key+'[]');
field.attr("value", subvalue);
form.append(field);
});
} else {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
}
});
$(document.body).append(form);
form.submit();
}
post('purchase_bitcoin', response);
}
}
</script>
</div>
</div>
<p id="bitcoin_payment_string"></p>
<div class="d"></div>
<p style="text-align: right;">
<a id="btc-button"><button class="bitcoin-button" style="visibility: visible;"><span id="bitcoin_button_text">Pay with Bitcoin</span></button></a>
</p>
“场外结账汇总”%>
确认付款后,您的许可证密钥以及购买收据将发送至的
注意:提供的比特币链接和价格仅在10:00分钟内有效。
“项目\结帐\汇总”,:局部变量=>{order:@order}%>
Stripe.setPublishableKey(“”);
函数populateBitcoinCheckout(状态、响应){
如果(状态===200){
document.getElementById(“比特币总额”).innerHTML=“(“+response.bitcoin\u amount/100000000+”BTC)”;
document.getElementById(“比特币支付字符串”).innerHTML='请发送:'+响应。比特币金额/100000000+'BTC至'+'';
document.getElementById(“btc按钮”).href=response.bitcoin_uri+'&label=Software+Purchase';
//配置定时器
功能启动计时器(持续时间、倒计时){
var定时器=持续时间,分钟,秒;
var t=设置间隔(函数(){
分钟=parseInt(计时器/60,10)
秒=parseInt(计时器%60,10);
分钟=分钟<10?“0”+分钟:分钟;
秒=秒<10?“0”+秒:秒;
//轮询接收器
Stripe.bitconReceiver.pollReceiver(response.id,filledReceiverHandler(response));
countdown.textContent=分钟+“:”+秒;
如果(--定时器<0){
净间隔(t);
document.getElementById(“比特币总量”).innerHTML=“”;
document.getElementById(“比特币支付字符串”).innerHTML=“”;
document.getElementById(“比特币按钮文本”).innerHTML=“刷新订单”
document.getElementById(“btc按钮”).href=“javascript:history.back()”
document.getElementById(“额外购买说明”).innerHTML=“Oops…此订单已过期,请使用“刷新订单”按钮重试。”
Stripe.bitcoinReceiver.cancelReceiverPoll(response.id);
}
}, 1000);
}
//启动计时器
var countdown=document.getElementById('countdown\u time');
startTimer(600,倒计时);
}否则{
document.getElementById(“比特币uri字符串”).innerHTML=JSON.stringify(响应);
}
}
Stripe.bitcoinReceiver.createReceiver({
金额:“,
货币:美元,
说明:'软件购买',
电子邮件:“
},populateBitcoinCheckout);
函数filledReceiverHandler(响应)
{
如果(response.filled==true){
功能柱(路径、参数){
变量形式=$('');
表格attr(“方法”、“职位”);
form.attr(“动作”,路径);
$.each(参数、函数(键、值){
if(typeof value==“object”| typeof value==“array”){
$.each(值,函数(子键,子值){
变量字段=$('');
字段属性(“类型”、“隐藏”);
field.attr(“name”,key+“[]”);
字段属性(“值”,子值);
表单追加(字段);
});
}否则{
变量字段=$('');
字段属性(“类型”、“隐藏”);
字段属性(“名称”,键);
字段属性(“值”,值);
表单追加(字段);
}
});
$(document.body).append(表单);
表单提交();
}
post(“购买比特币”,回复);
}
}
用比特币支付
我最终将轮询逻辑添加到计时器中,这似乎起到了作用。。。。尽管文档中暗示我不需要手动投票?至少我是这么看的
这是我对bitcoin.html.erb的完整修订版,供其他任何遇到问题的人使用
<%= render :partial => "offsite_checkout_summary" %>
<p>Your license key, along with your purchase receipt, will be sent to <strong><%= @order.licensee_name %></strong> at <strong><%= @order.email %></strong> once payment has been confirmed.</p>
<div id="extra_purchase_notes">
<em><strong>Note:</strong> The bitcoin link and price provided is only valid for <span id="countdown_time">10:00</span> minutes.</em>
</div>
<div class="d cl"></div>
<%= render :partial => "items_checkout_summary", :locals => { order: @order } %>
<div id="bitcoin_payment_address">
<script src="https://js.stripe.com/v2/stripe.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<%= $STRIPE_PREFS['stripe_publishable_key'] %>');
function populateBitcoinCheckout(status, response) {
if (status === 200) {
document.getElementById("bitcoin_total").innerHTML = " (" + response.bitcoin_amount/100000000 + " BTC)";
document.getElementById("bitcoin_payment_string").innerHTML = 'Please send: <strong>' + response.bitcoin_amount/100000000 + ' BTC</strong> to <strong>' + '<a href="' + response.bitcoin_uri + '&label=Software+Purchase">' + response.inbound_address + '</a></strong>';
document.getElementById("btc-button").href = response.bitcoin_uri + '&label=Software+Purchase';
//configure timer
function startTimer(duration, countdown) {
var timer = duration,minutes,seconds;
var t = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//poll reciever
Stripe.bitcoinReceiver.pollReceiver(response.id, filledReceiverHandler(response));
countdown.textContent = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(t);
document.getElementById("bitcoin_total").innerHTML = "";
document.getElementById("bitcoin_payment_string").innerHTML = "";
document.getElementById("bitcoin_button_text").innerHTML = "Refresh Order"
document.getElementById("btc-button").href = "javascript:history.back()"
document.getElementById("extra_purchase_notes").innerHTML = "<em><strong>Oops...</strong> This order has expired, use the Refresh Order button to retry.</em>"
Stripe.bitcoinReceiver.cancelReceiverPoll(response.id);
}
}, 1000);
}
//start timer
var countdown = document.getElementById('countdown_time');
startTimer(600, countdown);
} else {
document.getElementById("bitcoin_uri_string").innerHTML = JSON.stringify(response);
}
}
Stripe.bitcoinReceiver.createReceiver({
amount: "<%= (@order.total * 100).round %>",
currency: 'usd',
description: 'Software purchase',
email: "<%= @order.email %>"
}, populateBitcoinCheckout);
function filledReceiverHandler(response)
{
if (response.filled === true) {
function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
if ( typeof value == 'object' || typeof value == 'array' ){
$.each(value, function(subkey, subvalue) {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key+'[]');
field.attr("value", subvalue);
form.append(field);
});
} else {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
}
});
$(document.body).append(form);
form.submit();
}
post('purchase_bitcoin', response);
}
}
</script>
</div>
</div>
<p id="bitcoin_payment_string"></p>
<div class="d"></div>
<p style="text-align: right;">
<a id="btc-button"><button class="bitcoin-button" style="visibility: visible;"><span id="bitcoin_button_text">Pay with Bitcoin</span></button></a>
</p>
“场外结账汇总”%>
确认付款后,您的许可证密钥以及购买收据将发送至的
注意:提供的比特币链接和价格仅在10:00分钟内有效。
“项目\结帐\汇总”,:局部变量=>{order:@order}%>
Stripe.setPublishableKey(“”);
函数populateBitcoinCheckout(状态、响应){
如果(状态===200){
document.getElementById(“比特币总额”).innerHTML=“(“+response.bitcoin\u amount/100000000+”BTC)”;
document.getElementById(“比特币支付字符串”).innerHTML='请发送:'+响应。比特币金额/100000000+'BTC至'+'';
document.getElement
require 'json'
require 'stripe'
class Store::BitcoinController < ApplicationController
def payment_recieved
type = params[:type]
data = params[:data]
if (type.blank? || type != "bitcoin.receiver.filled" || data.blank?)
logger.warn("Got request to Bitcoin IPN with invalid receiver email from #{request.remote_addr || request.remote_ip}")
render :nothing => true, :status => 200
return
end
..process order here...