Woocommerce自定义付款插件,基于表格,付款后的处理响应

时间:2019-04-30 23:38:24

标签: php wordpress woocommerce payment-gateway

我正在尝试实现基于woocomerce FORM的自定义插件(与mango pay集成),因此我将用户重定向到支付网关。用户在mangopay托管页面(信用卡表格)上付款并返回网站。 我不确定如何处理响应并将用户重定向到“谢谢”页面。 我已经能够按照woocommerce标准重定向到支付网关,但是无法继续进行以确保woo电子商务流程到位。

请任何人实施了类似的方法,然后让我知道下一步该怎么做。 现在,我只是尝试通过返回URL处理它,稍后将实现webhooks等

我已经添加了整个插件代码,并且保留了芒果薪水的返回网址,类似于paypal standard的做法。 $ 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 个答案:

没有答案