doumei2023 2019-04-30 23:38
浏览 101

Woocommerce定制支付插件,基于表格,付款后处理响应

I am trying to implement woocomerce FORM based custom plugin( integrated with mango pay) , so I redirect user to payment gateway. User pays on mangopay hosted page (credit card form) and returns to website. I am not sure how to handle response and redirect user to Thank you page. I have been able to redirect to payment gateway as per woocommerce standard but not able to proceed further keeping woo commerce flow in place.

Please anyone out there had implemented similar then please let me know what to do next. For now I am just try to handle it via return URL and will later implement webhooks etc

I have added my whole plugin code and there I have kept return url from mango pay similar to how paypal standard does. $PayIn->ExecutionDetails->ReturnURL

add_action('plugins_loaded', 'init_custom_gateway_class');

function init_custom_gateway_class(){

    class WC_Gateway_Custom extends WC_Payment_Gateway {

        public $domain;

        /**
         * Constructor for the gateway.
         */
        public function __construct() {

            $this->domain = 'custom_payment';

            $this->id                 = 'custom';
            $this->icon               = apply_filters('woocommerce_custom_gateway_icon', '');
            $this->has_fields         = true;
            $this->method_title       = __( 'Pay via card', $this->domain );
            $this->method_description = __( 'Allows payments with credit and debit cards', $this->domain );

            $this->init_form_fields();
            $this->init_settings();

            // Define user set variables
            $this->title        = $this->get_option( 'title' );
            $this->description  = $this->get_option( 'description' );
            $this->instructions = $this->get_option( 'instructions', $this->description );
            $this->order_status = $this->get_option( 'order_status');

            // Actions
            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );



        }

        /**
         * Initialise Gateway Settings Form Fields.
         */
        public function init_form_fields() {

            $this->form_fields = array(
                'enabled' => array(
                    'title'   => __( 'Enable/Disable', $this->domain ),
                    'type'    => 'checkbox',
                    'label'   => __( 'Enable Mango Pay', $this->domain ),
                    'default' => 'yes'
                ),
                'title' => array(
                    'title'       => __( 'Pay via Cards', $this->domain ),
                    'type'        => 'text',
                    'description' => __( 'This controls the title which the user sees during checkout.', $this->domain ),
                    'default'     => __( 'Mango Pay', $this->domain ),
                    'desc_tip'    => true,
                ),
                'order_status' => array(
                    'title'       => __( 'Order Status', $this->domain ),
                    'type'        => 'select',
                    'class'       => 'wc-enhanced-select',
                    'description' => __( 'Choose whether status you wish after checkout.', $this->domain ),
                    'default'     => 'wc-completed',
                    'desc_tip'    => true,
                    'options'     => wc_get_order_statuses()
                ),
                'description' => array(
                    'title'       => __( 'Description', $this->domain ),
                    'type'        => 'textarea',
                    'description' => __( 'Payment method description that the customer will see on your checkout.', $this->domain ),
                    'default'     => __('Payment Information', $this->domain),
                    'desc_tip'    => true,
                ),
                'api_login' => array(
                    'title'     => __( 'Mango Pay API Login', $this->domain ),
                    'type'      => 'text',
                    'desc_tip'  => __( 'This is the API Login provided by Mango when you signed up for an account.', 'spyr-authorizenet-aim' ),
                ),
                'trans_key' => array(
                    'title'     => __( 'Authorize.net Transaction Key', $this->domain),
                    'type'      => 'password',
                    'desc_tip'  => __( 'This is the Transaction Key provided by Mango when you signed up for an account.', 'spyr-authorizenet-aim' ),
                ),
                'environment' => array(
                    'title'     => __( 'Mango Test Mode', $this->domain),
                    'label'     => __( 'Enable Test Mode', $this->domain),
                    'type'      => 'checkbox',
                    'description' => __( 'Place the payment gateway in test mode.', $this->domain),
                    'default'   => 'no',
                )

            );
        }

       public function process_payment( $order_id ) {

            $order = wc_get_order( $order_id );
            $total = $order->get_total();
            $fees = round(($total*10)/100,2);

            require_once("./mangopay/vendor/autoload.php");
            $MangopayApi = new \MangoPay\MangoPayApi();
            $MangopayApi->Config->ClientId = "XXX"; //#TODO Update with your own info
            $MangopayApi->Config->ClientPassword = "XXXXXXX"; //#TODO Update with your own info
            $MangopayApi->Config->TemporaryFolder = $_SERVER['DOCUMENT_ROOT'];

            global $current_user;
            get_currentuserinfo();

            $current_user = wp_get_current_user();
            $user_id = $current_user->ID;

            $mangoid = 0;
            $walletid = 0;
            $mangoid = get_user_meta( $user_id, 'mangoid',true ); 

            if(empty($mangoid)){
                // CREATE NATURAL USER

                $naturalUser = new \MangoPay\UserNatural();
                $naturalUser->Email = $current_user->user_email;
                $naturalUser->FirstName = $current_user->user_firstname;
                $naturalUser->LastName = $current_user->user_lastname;
                $naturalUser->Birthday = 121271;
                $naturalUser->Nationality = "FR";
                $naturalUser->CountryOfResidence = "FR";
                // print_r($naturalUser);
                $naturalUserResult = $MangopayApi->Users->Create($naturalUser);
                // echo "<pre>";
                // print_r($naturalUserResult);
                if(!empty($naturalUserResult->Id)){
                    $updated = update_user_meta($user_id, 'mangoid', $naturalUserResult->Id );
                    $mangoid = get_user_meta( $user_id, 'mangoid',true ); 
                }else{
                    // throw error

                }


               // die;
            }
            if($mangoid){
                //get wallet id
                $walletid = get_user_meta( $user_id, 'walletid',true ); 
                if(empty($walletid)){
                    $Wallet = new \MangoPay\Wallet();
                    $Wallet->Owners = array($mangoid);
                    $Wallet->Description = "wallet for User ".$mangoid;
                    $Wallet->Currency = "EUR";
                    $result = $MangopayApi->Wallets->Create($Wallet);
                    $walletid  = $result->Id;
                    if(!empty($walletid)){
                        $updated_wallet_id = update_user_meta($user_id, 'walletid', $walletid);
                        $walletid = get_user_meta( $user_id, 'walletid',true ); 
                    }else{
                        // throw error

                    }
                }
            }

            $PayIn = new \MangoPay\PayIn();
            $PayIn->CreditedWalletId = $walletid;
            $PayIn->AuthorId = $mangoid;
            $PayIn->PaymentType = \MangoPay\PayInPaymentType::Card;
            $PayIn->PaymentDetails = new \MangoPay\PayInPaymentDetailsCard();
            $PayIn->PaymentDetails->CardType = "CB_VISA_MASTERCARD";
            $PayIn->DebitedFunds = new \MangoPay\Money();
            $PayIn->DebitedFunds->Currency = "EUR";
            $PayIn->DebitedFunds->Amount = str_replace('.', '',$total);
            $PayIn->Fees = new \MangoPay\Money();
            $PayIn->Fees->Currency = "EUR";
            $PayIn->Fees->Amount = str_replace('.', '',$fees);
            $PayIn->ExecutionType = \MangoPay\PayInExecutionType::Web;

            $PayIn->ExecutionDetails = new \MangoPay\PayInExecutionDetailsWeb();

            // return url , tried keeping it similar to how paypal standard does
            $PayIn->ExecutionDetails->ReturnURL = "http".(isset($_SERVER['HTTPS']) ? "s" : null)."://".$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"]."checkout/order-received/".($order_id)."?key=".$order->order_key;

            $PayIn->ExecutionDetails->Culture = "EN";
            $result = $MangopayApi->PayIns->Create($PayIn);
            $_SESSION["MangoPayDemo"]["PayInCardWeb"] = $result->Id;

           // Here mango pay redirect url generated and hence user is redirected to mangopay credit card form

            $mango_url = ($result->ExecutionDetails->RedirectURL);

            return array(
                'result' => 'success',
                'redirect' => "$mango_url"
            );


        }


     }
 }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大