ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在WooCommerce中以编程方式保存已保存的信用卡

2019-10-12 21:34:32  阅读:238  来源: 互联网

标签:php wordpress woocommerce


我正在WooCommerce中以编程方式创建订单,需要从默认的已保存信用卡中收取费用.我正在使用WooCommerce条形插件,并且想出了如何设置正确的付款方式,但无法弄清楚如何对卡进行实际收费.下面是我到目前为止的代码.

$order = wc_create_order();

$order->add_product( wc_get_product( 52 ), 1 );
$order->set_address( $shipping_address, 'shipping' );
$order->set_address($user_info, 'billing');

$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['stripe']);

$order->calculate_totals(); 
$order->update_status("Completed", 'First Partner Order', TRUE);
$order->save();

解决方法:

我能够找到一个解决方案,尽管它不是很优雅,但似乎可行.
基本前提是,我们使用Stripe API创建费用,然后手动添加所有自定义字段.这将导致成功的收费,反映在woocommerce中,以后可以通过管理员退款.下面是带有注释的代码.我想知道是否有人找到了更好的解决方案.

注意:您必须使用sripe php api

$order = wc_create_order();

$order->add_product( wc_get_product( 52 ), 1 ); //Add product to order
$order->set_address( $shipping_address, 'shipping' ); //Add shipping address
$order->set_address($user_info, 'billing'); //Add billing address

//Set payment gateways
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['stripe']);

$order->calculate_totals(true); //setting true included tax 
//Try to charge stripe card
try {

  // Get stripe  test or secret key from woocommerce settings
  $options = get_option( 'woocommerce_stripe_settings' );
  $stripeKey = 'yes' === $options['testmode'] ? $options['test_secret_key'] : 
  $options['secret_key'] ;

  //Set the Stripe API Key
  \Stripe\Stripe::setApiKey($stripeKey);

  //Get Stripe customer token that was created when the card was saved in woo
  $tokenString = get_user_meta($user_id, '_stripe_customer_id', true);

  //Get total for the order as a number
  $total = intval($order->get_total());
  $totalNoDec = $total * 100;

  //Charge user via Stripe API
  $charge = \Stripe\Charge::create([
    'amount' => $totalNoDec,
    'currency' => 'usd',
    'customer' => $tokenString,
  ]);

  //Set all the meta data that will be needed
  $order->update_meta_data( '_transaction_id', $charge->id );
  $order->update_meta_data( '_stripe_source_id', $charge->payment_method );
  $order->update_meta_data( '_stripe_charge_captured', 'yes'  );
  $order->update_meta_data( '_stripe_currency', $charge->currancy);
  $order->update_meta_data( '_stripe_customer_id', $charge->customer);

} catch (\Stripe\Error\Base $e) {
  // Code to do something with the $e exception object when an error occurs
  echo($e->getMessage());
} catch (Exception $e) {
  echo($e->getMessage());
  // Catch any other non-Stripe exceptions
}

//Set order status to processing
$order->set_status("processing");
$order->save();

标签:php,wordpress,woocommerce
来源: https://codeday.me/bug/20191012/1903100.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有