第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 WooCommerce 中以編程方式對保存的信用卡收費(fèi)

在 WooCommerce 中以編程方式對保存的信用卡收費(fèi)

PHP
慕慕森 2021-06-27 09:36:27
我正在 WooCommerce 中以編程方式創(chuàng)建訂單,需要從默認(rèn)保存的信用卡中收費(fèi)。我正在使用 WooCommerce 條紋插件,并且已經(jīng)弄清楚如何設(shè)置正確的付款方式,但無法弄清楚如何實(shí)際向卡收費(fèi)。下面是我到目前為止的代碼。$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 回答

?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

我找到了一個(gè)解決方案,雖然不是很優(yōu)雅,但它似乎有效。基本前提是我們使用stripe api創(chuàng)建一個(gè)charge,然后手動(dòng)添加所有的自定義字段。這將導(dǎo)致成功收費(fèi),該費(fèi)用反映在 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();



查看完整回答
反對 回復(fù) 2021-07-02
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號