我使用Wordpress,但我使用的是“开箱即用”的解决方案,因为我的客户具体要求它。我有这个Javascript,它创建了一个表单,当表单提交时,它创建了一个包含令牌的cookie,所以我可以从PHP检索它,它看起来如下:
<form action="/wp-content/themes/betheme/includes/add_payment_method.php" method="post" id="add-card-form">
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element"></div>
<!-- Used to display Element errors. -->
<div id="card-errors" role="alert"></div>
</div>
<button>Add payment method</button>
</form>
<script src="https://js.stripe.com/v3/"></script>
<script>
var style = {
base: {
color: '#ffffff',
fontSize: '14px',
fontSmoothing: 'antialiased',
'::placeholder': {
color: '#ccc',
},
iconColor: "#fff"
},
invalid: {
color: '#e5424d',
':focus': {
color: '#303238',
},
},
};
var stripe = Stripe('xxxxxxxxxxxxxxxxxxxxxxx');
var elements = stripe.elements();
var cardElement = elements.create('card', {style: style, hidePostalCode: true});
window.mobilecheck = function() {
var check = false;
(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};
if(mobilecheck()) {
cardElement.mount('#Content > div > div > div > div.section.mcb-section.hide-desktop > div > div > div > div > div > div > div > form > div.form-row >div#card-element');
}
else {
cardElement.mount('#Content > div > div > div > div.section.mcb-section.hide-tablet.hide-mobile > div > div > div > div > div > div > div > form > div.form-row > div#card-element');
}
cardElement.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Create a token or display an error when the form is submitted.
var form = document.getElementById('add-card-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(cardElement).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}
function stripeTokenHandler(token) {
createCookie("stripe_token", token, 1);
var form = document.getElementById('add-card-form');
form.submit();
}
</script>当表单提交时,它在add_payment_method.php中执行此PHP
<?php
require_once("../../../../wp-load.php");
submit_stripe_payment_method($_COOKIE['stripe_token']);
?>在我的主题functions.php文件中,我有:
require_once('stripe-php/init.php');
....
function submit_stripe_payment_method($token) {
print_r($token);
$current_user_id = get_current_user_id();
echo 'Your User ID is: ' .$current_user_id;
$customer_id = get_user_meta( $current_user_id, '_stripe_customer_id', true );
echo "CUSTOMER_ID: ".(string)$customer_id;
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxxxxxxxxxxxx");
echo "MADE IT HERE";
$customer = \Stripe\Customer::retrieve((string)$customer_id);
echo "CUSTOMER: ".$customer;
$customer_card = $customer->sources->create(["source" => $token]);
echo "<pre>";
var_dump($customer_card);
echo "</pre>";
}上述各项的产出如下:
[object Object]Your User ID is: 1CUSTOMER_ID: cus_DsoF18uw1dR0ujMADE IT HERE
正如您所看到的,它仅限于“在这里制造”echo。
我已经在Stripe控制台中验证了我使用的$customer_id是正确的,并且我没有任何错误,无论是PHP还是其他。我唯一能想到的就是我使用了错误的标记,但它看起来非常直接。正如您所看到的,$token打印为object对象,不管我如何记录它,它都会这样做,所以我无法确切地看到它是什么,但这是有意义的,因为它包含用于验证信用card...also的敏感信息--也许它永远不会达到这个目的,因为我从未看到echo "CUSTOMER: ".$customer;语句,所以可能会挂起:
$customer = \Stripe\Customer::retrieve((string)$customer_id);
另外,我在没有(string)的情况下尝试了上面的方法,但是得到了相同的结果。
所以,在所有这些之后,我检查了Stripe控制台,并且没有添加任何卡片。
更新
显然,我使用的API版本不是最新版本,我可以升级,我只想知道在我这么做之前,是否有人认为这是一个可能的解决方案。因为它可能会改变整个站点的条纹工作方式(使用插件)。我正在使用2018-02-28,我可以升级到2018-09-24。我也在localhost环境上进行测试,所以我不是https,但我认为这对测试并不重要。
更新
我意识到我没有将localhost设置的端点添加到stripe控制台管理区域。要做到这一点,我必须使用ngrok (就像Stripe建议的那样,如果您正在使用localhost)。我在端口8888上使用了8888,所以我做了./ngrok http 8888,然后再试一次,但没有成功。它确实在Wordpress中说端点将enable you to receive notifications on the charge statuses.,所以它可能与添加支付方法无关。我将http url用于ngrok,而不是https url。
更新
我手动安装了stripe-php,所以我尝试使用composer,但是它没有帮助,同样的结果。
更新
我更改了secret key的密钥,这允许我检索$customer,但是我看不到$customer_card的任何输出,而且当我查看仪表板时,没有创建任何卡片。
更新
我检查了Stripe仪表板,当我试图创建源代码时,我收到了一个错误:
{
"error": {
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such token: [object Object]",
"param": "source",
"type": "invalid_request_error"
}
}我的记号怎么了?
更新
令牌问题的解决方案:在测试模式中,令牌只是一个与您正在使用的测试信用卡号相对应的字符串,我需要"tok_visa"用于4242424242424242。https://stripe.com/docs/testing
发布于 2018-10-31 04:55:10
问题可能是排好了队
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxxxxxxxxxxxx");
您使用的是以Publishable key开头的pk_xxx,而应该在您的帐户中使用Secret Key sk_xxxx。
您可以看到Stripe仪表板中的错误。
在你登录后。
如果这有帮助,请告诉我。
编辑
我还需要使用测试令牌,它只是一个字符串,它对应于您正在使用的测试信用卡号。https://stripe.com/docs/testing。请参阅Tokens选项卡。
发布于 2022-02-09 12:48:51
您不能静态地调用Stripe\PaymentMethod:attach()。您必须先获取PaymentMethod,然后将其附加到客户。守则应是:
$pm =\Stripe\PaymentMethod:检索(‘pm_123’);$pm->附加(‘customer’=> 'cus_123');
我将致力于更新文档以避免这种混乱!
https://stackoverflow.com/questions/53074364
复制相似问题