我正在尝试做同样的事情,但还没有运气,但我发现了 payment_redirect_key 的创建位置。您可以在 commerce/modules/commerce_payment/includes/commerce_payment.checkout_pane.inc 函数 commerce_payment_redirect_pane_checkout_form 的 commerce_payment_redirect_pane_checkout_form 函数中找到它,当前版本的第 360 行(http://cgit.drupalcode.org/commerce/tree/modules/payment /includes/commerce_payment.checkout_pane.inc#n360 )
基本上,是这样的:
$order->data['payment_redirect_key'] = drupal_hash_base64(time());
commerce_order_save($order);
编辑
经过几天的研究,我在几行代码中找到了解决方案。我将此函数用作菜单项的页面回调(类似于 product/%sku)。这是代码:
<?php
function custom_module_create_order($sku) {
global $user;
$product = commerce_product_load_by_sku($sku);
$order = ($user->uid) ? commerce_order_new($user->uid, 'checkout_checkout') : commerce_cart_order_new();
// Save to get the Order ID.
commerce_order_save($order);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
// Select here the payment method. Usually something like:
// [module_name]|commerce_payment_[module_name]
$order->data['payment_method'] = 'commerce_sermepa|commerce_payment_commerce_sermepa';
commerce_order_save($order);
// Set status to order checkout to go to the payment platform redirect.
commerce_order_status_update($order, 'checkout_payment');
drupal_goto('checkout/' . $order->order_id);
}