我有一个使用Stripe.js Elements API
来向Stripe API提交付款方式(信用卡)的表格。提交表单后,一切工作正常,当我刷新页面时(我只是使用示例payment_methods.php
中的表格来显示我添加的卡片),该表格未显示我刚刚添加的卡片,但是它显示了所有先前添加的卡。此外,当我结帐并观察是否有新卡显示时。在结帐时看到卡片后,我返回my account/payment methods
,并显示新卡片。
提交表格后,如何立即将卡片显示在列表中?我假设由于我的代码没有经过正确的woocommerce / wordpress渠道,因此wordpress / woocommerce不会知道新添加的卡,直到结帐时的代码运行才能更新卡列表。结帐时发生什么事才能显示卡?我可以在提交表单后手动(以编程方式)模仿结帐时发生的情况,以便新卡出现在列表中吗?结帐时的清单和my account/payment methods
部分中的清单有什么不同?为什么结帐页面列表会看到新添加的卡?
在检查插件代码时,我发现在添加卡时使用nonce
可能有些相关,但是如果这是问题,我不确定如何实现nonce
。我实际上已经成功地向请求中添加了nonce
,但是add_payment_method()
中的woocommerce abstract-wc-stripe-payment-gateway.php
代码需要进行其他设置才能成功添加付款方式(通过wordpress / woocommerce渠道),例如$_POST['stripe_source']
,$_POST['stripe_token']
和$_POST['payment_method']
。在尝试破解时,我更改了代码以查看使用其中的cookie
创建的stripe_token
,并将代码更改为使用$_COOKIE
而不是$_POST
,但是我一直遇到问题,所以我决定看看是否有一个比较简单的解决方案,例如使用do_action
钩子之类的东西,这将为我刷新列表。提交表单后,我尝试使用do_action('woocommerce_init')
钩子,但没有帮助。
这是我使用payment_methods.php
的{{1}}模板代码:
Stripe.js
add_payment_method.php
<?php
/**
* Payment methods
*
* Shows customer payment methods on the account page.
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/payment-methods.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$show_table = true;
if(isset($_POST['create_new_method'])) {
$show_table = false;
}
$saved_methods = wc_get_customer_saved_methods_list( get_current_user_id() );
$has_methods = (bool) $saved_methods;
$types = wc_get_account_payment_methods_types();
do_action( 'woocommerce_before_account_payment_methods', $has_methods ); ?>
<?php if ( $has_methods && $show_table ) : ?>
<table class="woocommerce-MyAccount-paymentMethods shop_table shop_table_responsive account-payment-methods-table">
<thead>
<tr>
<?php foreach ( wc_get_account_payment_methods_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-PaymentMethod woocommerce-PaymentMethod--<?php echo esc_attr( $column_id ); ?> payment-method-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $saved_methods as $type => $methods ) : ?>
<?php foreach ( $methods as $method ) : ?>
<tr class="payment-method<?php echo ! empty( $method['is_default'] ) ? ' default-payment-method' : '' ?>">
<?php foreach ( wc_get_account_payment_methods_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-PaymentMethod woocommerce-PaymentMethod--<?php echo esc_attr( $column_id ); ?> payment-method-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php
if ( has_action( 'woocommerce_account_payment_methods_column_' . $column_id ) ) {
do_action( 'woocommerce_account_payment_methods_column_' . $column_id, $method );
} elseif ( 'method' === $column_id ) {
if ( ! empty( $method['method']['last4'] ) ) {
/* translators: 1: credit card type 2: last 4 digits */
echo sprintf( __( '%1$s ending in %2$s', 'woocommerce' ), esc_html( wc_get_credit_card_type_label( $method['method']['brand'] ) ), esc_html( $method['method']['last4'] ) );
} else {
echo esc_html( wc_get_credit_card_type_label( $method['method']['brand'] ) );
}
} elseif ( 'expires' === $column_id ) {
echo esc_html( $method['expires'] );
} elseif ( 'actions' === $column_id ) {
foreach ( $method['actions'] as $key => $action ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a> ';
}
}
?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
<form action="" method="post">
<input style="background-color: #88d651!important;" name="create_new_method" type="submit" value="Create new method" />
</form>
<?php else :
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$customer_id = get_user_meta( $current_user_id, '_stripe_customer_id', true );
\Stripe\Stripe::setApiKey("xxx");
if($customer_id == NULL) {
echo 'Make your first purchase to activate this feature - <a href="https://www.grahmlux.com/jewelry" style="font-weight: 700; text-decoration: underline; color: #ffffff">Shop Now</a>';
//die();
}
else {
$customer = \Stripe\Customer::retrieve((string)$customer_id);
} ?>
<p class="woocommerce-Message woocommerce-Message--info woocommerce-info"><?php esc_html_e( 'No saved methods found.', 'woocommerce' ); ?></p>
<!--<form action="https://www.grahmlux.com/wp-content/themes/betheme/includes/add_payment_method.php" method="post" id="add_payment_method">-->
<form action="http://localhost:8888/wp-content/themes/betheme/includes/add_payment_method.php" method="post" id="add_payment_method">
<div class="form-row">
<label for="card-element">
Credit or Debit Card (Secured By Stripe)
</label>
<div id="card-number" style="display: inline-block; width: 180px"></div>
<div id="card-expiry" style="display: inline-block; width: 60px"></div>
<div id="card-cvc" style="display: inline-block; width: 50px"></div>
<!-- Used to display Element errors. -->
<div id="card-errors" role="alert"></div>
<?php wp_nonce_field( 'woocommerce-add-payment-method', 'woocommerce-add-payment-method-nonce' ); ?>
<button type="submit" class="woocommerce-Button woocommerce-Button--alt button alt" id="place_order" style="color: white!important" value="<?php esc_attr_e( 'Add payment method', 'woocommerce' ); ?>"><?php esc_html_e( 'Add payment method', 'woocommerce' ); ?></button>
<input type="hidden" name="woocommerce_add_payment_method" id="woocommerce_add_payment_method" value="1" />
</div>
</form>
<script>
var style = {
base: {
color: '#ffffff',
fontSize: '14px',
fontSmoothing: 'antialiased',
'::placeholder': {
color: '#ccc',
},
iconColor: "#fff"
},
invalid: {
color: '#e5424d',
':focus': {
color: '#303238',
},
},
};
var stripe = Stripe('pk_test_TpmyYk1TnhrxPqNpImYwjyap');
var elements = stripe.elements();
var cardNumber = elements.create('cardNumber', {
placeholder: 'Add Card Number Here',
style: style
});
var cardExpiry = elements.create('cardExpiry', {
style: style
});
var cardCvc = elements.create('cardCvc', {
style: style
});
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()) {
cardNumber.mount('#Content > div > div > div > div.section.mcb-section.hide-desktop > div > div > div > div > div > div > div > form > div.form-row >div#card-number');
cardExpiry.mount('#Content > div > div > div > div.section.mcb-section.hide-desktop > div > div > div > div > div > div > div > form > div.form-row >div#card-expiry');
cardCvc.mount('#Content > div > div > div > div.section.mcb-section.hide-desktop > div > div > div > div > div > div > div > form > div.form-row >div#card-cvc');
}
else {
cardNumber.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-number');
cardExpiry.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-expiry');
cardCvc.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-cvc');
}
cardNumber.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
var stop = 0;
// Create a token or display an error when the form is submitted.
var form = document.getElementById('add_payment_method');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(cardNumber).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 {
if(stop == 0) {
console.log("inside stop token from being stored");
// Send the token to your server.
stripeTokenHandler(result.token);
stop = 1;
}
else {
stop = 0
}
}
});
});
function delete_cookie( name ) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
function createCookie(name,value) {
delete_cookie("stripe_token");
var date = new Date();
date.setTime(date.getTime()+1000);
var expires = "; expires="+date.toGMTString();
//document.cookie = name+"="+value+expires+"; path=/; domain=.grahmlux.com";
document.cookie = name+"="+value+expires+"; path=/";
}
function createUserIDCookie(name,value) {
//document.cookie = name+"="+value+""+"; path=/; domain=.grahmlux.com";
document.cookie = name+"="+value+""+"; path=/";
}
function stripeTokenHandler(token) {
var current_user_id = <?php echo $current_user_id; ?>
console.log("current_user_id:", current_user_id);
//MUST BE THIS ORDER BECAUSE OF DELETE_COOKIE
createUserIDCookie("user_id", current_user_id);
createCookie("stripe_token", token);
var form = document.getElementById('add_payment_method');
form.submit();
}
</script>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_payment_methods', $has_methods ); ?>
functions.php
<?php
require_once("../../../../wp-load.php");
$cookie = $_COOKIE['stripe_token'];
$user_id = $_COOKIE['user_id'];
submit_stripe_payment_method($cookie, $user_id);
?>
请避免使用之类的答案,因为“我不知道这是不推荐的使用woocommerce / wordpress的方法”。我这样做的目的是为一位客户特别要求解决问题的“即开即用”解决方案,而不是仅仅尝试更新主题以查看其是否可以使原始...
function submit_stripe_payment_method($token, $user_id) {
ob_start();
$current_user_id = $user_id;
$customer_id = get_user_meta( $current_user_id, '_stripe_customer_id', true );
\Stripe\Stripe::setApiKey("xxx");
$customer = \Stripe\Customer::retrieve((string)$customer_id);
$customer_card = $customer->sources->create(["source" => "tok_visa"/*$token*/]);
do_action('woocommerce_init');
setcookie("stripe_token", "", time()-3600);
//header("Location: https://www.grahmlux.com/my-account/payment-methods");
header("Location: http://localhost:8888/my-account/payment-methods");
//die();
ob_end_flush();
}
...
表单正常工作。< / p>
希望,解决方案是一个钩子,它将运行用于更新列表的代码,但我无法弄清楚它可能是哪个。
谢谢。