drwj4061 2019-05-22 21:38
浏览 117

在WooCommerce中以编程方式为已保存的信用卡充值

I am creating an order programmatically in WooCommerce and need to charge the default saved credit card. I am using the WooCommerce stripe plugin and have figured out how to set the correct payment method but can't figure out how to actually charge the card. Below is the code I have so far.

$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();
  • 写回答

1条回答 默认 最新

  • dongtun2459 2019-05-23 15:23
    关注

    I was able to figure out a solution, although not very elegant, it seems to work. The basic premise is that we use the stripe api to create a charge and then add all the custom fields manually. This will result in a successful charge that is reflected in woocommerce and can be refunded via the admin later on. Below is the code with comments. I would love to know if anybody has found a better solution.

    Note: You must use the 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();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来