PHP Modify Zen-cart order and payment process to prevent missing orders _php tutorial

Source: Internet
Author: User
Tags zen cart
People who have used Zen-cart know that the next step in Zen-cart is the following (where the representation in [] is not required):

1. Shopping cart (shopping cart)

2. [Shipping Method (delivery method)]

3. Payment method (Payment method)

4. Order Confirmation (Confirmation)

5. [Payment by third party website]

6. Order Processing (checkout process)-This step is important because the information in the shopping cart will be written to the order here

7. Order success (Checkout success)

Such a process does not have any problems under normal circumstances. However, from the 5th to the 6th part of the process, the user may think that the payment is successful directly off the page, or due to network causes can not jump to the checkout_process page, the result is very serious, because the order can not be created normally.

Based on the above analysis, we would like to change the process a little bit, that is, the order has been created before payment, so even if the payment can not be from the third-party payment site to return, we will not have the user payment success in the background without orders. The revised blueprint is basically the following:

1. After confirming the order on the Checkour_confirmation page, it will be proccess directly and go to the Checkour_success page where you can enter the payment page. As shown in the following:


2. If the customer is not able to pay at that time, can also enter their own backstage to pay for the historical order. As shown in the following:


Let's take a look at how to achieve the above functionality step-by-step.

1. First we need to make a transformation of the existing payment module. You need to add a field paynow_action_url to the class of the payment method that represents the URL of the page to be paid, plus a function, Paynow_button ($order _id), to get the parameters of the payment form to hide the field code.
To increase the Paynow_action_url field, add the following code to the constructor of the class payment:
Copy CodeThe code is as follows:
if (Zen_not_null ($module)) && (In_array ($module. php ', $this->modules)) && (Isset ($GLOBALS [$module]->paynow_action_url)) {
$this->paynow_action_url = $GLOBALS [$module]->paynow_action_url;
}

To add the Paynow_button ($order _id) function, add the following code after the last function of the payment class:
Copy CodeThe code is as follows:
function Paynow_button ($order _id) {
if (Is_array ($this->modules)) {
if (Is_object ($GLOBALS [$this->selected_module])) {
return $GLOBALS [$this->selected_module]->paynow_button ($order _id);
}
}
}

2. Take the PayPal payment method as an example to illustrate how to implement it. In order not to break the original PayPal code, we will copy the paypal.php file, and name it paypalsimple.php, and make appropriate changes to the code inside. The code looks like this, and you can see that the designation for Form_action_url is removed and given the Paynow_action_url, because we want the user to go directly to Checkout_process after clicking "Confirm Order", so if you don't specify FORM_ Action_url, then the confirmation order form will be submitted directly to the Checkout_process page, and Paynow_action_url is the value of the previous form_action_url. The implementation of the Paynow_button function is also very simple, just cut the contents of the original Process_button () function, except that we do not use the global $order variable, but instead use $order = New Order ($order _id). , to reconstruct an object that is prepared for displaying the pay Now button in a history order.
paypalsimple.php
Copy CodeThe code is as follows:
/**
* @package paypalsimple Payment Module
* @copyright Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 OsCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: paypalsimple.php 4960 2009-12-29 11:46:46z Gary $
*/
Ensure dependencies is loaded
Include_once (Is_admin_flag = = = true? Dir_fs_catalog_modules:dir_ws_modules). ' payment/paypal/paypal_functions.php ');
Class Paypalsimple {
var $code, $title, $description, $enabled;
Class constructor
function Paypalsimple () {
Global $order;
$this->code = ' paypalsimple ';
$this->title = Module_payment_paypal_simple_text_title;
if (Is_admin_flag = = = True) {
$this->title = Module_payment_paypal_simple_text_admin_title;
}
$this->description = module_payment_paypal_simple_text_description;
$this->sort_order = Module_payment_paypal_simple_sort_order;
$this->enabled = ((Module_payment_paypal_simple_status = = ' True ')? true:false);
if ((int) module_payment_paypal_simple_order_status_id > 0) {
$this->order_status = module_payment_paypal_simple_order_status_id;
}
$this->paynow_action_url = ' https://'. Module_payment_paypal_simple_handler;
if (Is_object ($order)) $this->update_status ();
}
Class methods
function Update_status () {
Global $order, $db;
if (($this->enabled = = True) && ((int) module_payment_paypal_simple_zone > 0)) {
$check _flag = false;
$check = $db->execute ("Select zone_id from". Table_zones_to_geo_zones. "Where geo_zone_id = '". Module_payment_paypal_simple_zone. "' and zone_country_id = '". $order->billing[' country ' [' ID ']. "' ORDER by zone_id");
while (! $check->eof) {
if ($check->fields[' zone_id ') < 1) {
$check _flag = true;
Break
} elseif ($check->fields[' zone_id '] = = $order->billing[' zone_id ']) {
$check _flag = true;
Break
}
$check->movenext ();
}
if ($check _flag = = False) {
$this->enabled = false;
}
}
}
function Javascript_validation () {
return false;
}
function selection () {
$text = Module_payment_simple_paypal_text_catalog_logo. ' '. Module_payment_paypal_simple_text_title. '

' . Module_payment_paypal_simple_acceptance_mark_text. '

';
Return array (' id ' = = $this->code,
' Module ' = $text
);
}
function Pre_confirmation_check () {
return false;
}
Function Confirmation () {
return false;
}
function Process_button () {
return false;
}
function before_process () {
return false;
}
function after_process () {
return false;
}
function Get_error () {
return false;
}
function Check () {
Global $db;
if (!isset ($this->_check)) {
$check _query = $db->execute ("Select Configuration_value from". Table_configuration. "Where Configuration_key = ' module_payment_paypal_simple_status '");
$this->_check = $check _query->recordcount ();
}
return $this->_check;
}
function Install () {
Global $db;
$db->execute ("insert into".) Table_configuration. "(Configuration_title, Configuration_key, Configuration_value, Configuration_description, configuration_group_id, Sort_order, Set_function, date_added) VALUES (' Enable paypal-simple Module ', ' module_payment_paypal_simple_status ', ' True ', ' Do you want to accept paypal-simple payments? ', ' 6 ', ' 0 ', ' zen_cfg_select_option (array (\ ' true\ ', \ ' false\ '), ', no W ()) ");
$db->execute ("insert into".) Table_configuration. "(Configuration_title, Configuration_key, Configuration_value, Configuration_description, configuration_group_id, Sort_order, date_added) VALUES (' sort order of display ', ' module_payment_paypal_simple_sort_order ', ' 0 ', ' sort order of d ' Isplay. Lowest is displayed first, ' 6 ', ' 8 ', now ());
$db->execute ("insert into".) Table_configuration. "(Configuration_title, Configuration_key, Configuration_value, Configuration_description, configuration_group_id, Sort_order, Use_function, set_function, date_added) VALUES (' Payment Zone ', ' module_payment_paypal_simple_zone ', ' 0 ', ' If a zone is selected, only enable the this payment method for that zone. ', ' 6 ', ' 2 ', ' zen_get_zone_class_title ', ' Zen_cfg_pul L_down_zone_classes (', Now ()) ");
$db->execute ("insert into".) Table_configuration. "(Configuration_title, Configuration_key, Configuration_value, Configuration_description, configuration_group_id, Sort_order, Set_function, use_function, date_added) VALUES (' Set order Status ', ' Module_payment_paypal_simple_order_ status_id ', ' 0 ', ' Set the status of orders made with this payment module to this value ', ' 6 ', ' 0 ', ' Zen_cfg_pull_down_orde R_statuses (', ' Zen_get_order_status_name ', Now ()) ");
$db->execute ("insert into".) Table_configuration. "(Configuration_title, Configuration_key, Configuration_value, Configuration_description, configuration_group_id, Sort_order, Set_function, date_added) VALUES (' Mode for PayPal Web Services

Default:
www.paypal.com/cgi-bin/webscr
Or
www.paypal.com/us/cgi-bin/webscr
Or for the UK,
www.paypal.com/uk/cgi-bin/webscr', ' Module_payment_paypal_simple_handler ', ' www.paypal.com/cgi-bin/webscr ', ' Choose the URL for PAYPAL live processing ' , ' 6 ', ' A ', ', ', Now () ');
}
function Remove () {
Global $db;
$db->execute ("Delete from". Table_configuration. "Where Configuration_key in ('". Implode ("', '", $this->keys ()));
}
function keys () {
Return Array (' Module_payment_paypal_simple_status ', ' module_payment_paypal_simple_sort_order ', ' Module_payment_ Paypal_simple_zone ', ' module_payment_paypal_simple_order_status_id ', ' Module_payment_paypal_simple_handler ');
}
function Paynow_button ($order _id) {
Global $db, $order, $currencies, $currency;
Require_once (dir_ws_classes. ' order.php ');
$order = New Order ($order _id);
$options = Array ();
$optionsCore = Array ();
$optionsPhone = Array ();
$optionsShip = Array ();
$optionsLineItems = Array ();
$optionsAggregate = Array ();
$optionsTrans = Array ();
$buttonArray = Array ();
$this->totalsum = $order->info[' total ');
Save the session stuff permanently in case PayPal loses the session
$_session[' ppipn_key_to_remove '] = session_id ();
$db->execute ("Delete from". Table_paypal_session. "Where session_id = '". Zen_db_input ($_session[' Ppipn_key_to_remove '). "'");
$sql = "INSERT INTO". Table_paypal_session. (session_id, saved_session, expiry) VALUES (
'" . Zen_db_input ($_session[' Ppipn_key_to_remove '). "',
'" . Base64_encode (Serialize ($_session)). "',
'" . (Time () + (1*60*60*24*2)). "')";
$db->execute ($sql);
$my _currency = Select_pp_currency ();
$this->transaction_currency = $my _currency;
$this->transaction_amount = ($this->totalsum * $currencies->get_value ($my _currency));
$telephone = preg_replace ('/\d/', ' ', $order->customer[' telephone ');
if ($telephone! = ") {
$optionsPhone [' h_phonenumber '] = $telephone;
if (In_array ($order->customer[' country '] [' iso_code_2 '], array (' US ', ' CA '))) {
$optionsPhone [' night_phone_a '] = substr ($telephone, 0, 3);
$optionsPhone [' night_phone_b '] = substr ($telephone, 3, 3);
$optionsPhone [' night_phone_c '] = substr ($telephone, 6,4);
$optionsPhone [' day_phone_a '] = substr ($telephone, 0, 3);
$optionsPhone [' day_phone_b '] = substr ($telephone, 3, 3);
$optionsPhone [' day_phone_c '] = substr ($telephone, 6,4);
} else {
$optionsPhone [' night_phone_b '] = $telephone;
$optionsPhone [' day_phone_b '] = $telephone;
}
}
$optionsCore = Array (
' CharSet ' = CharSet,
' LC ' = $order->customer[' country ' [' iso_code_2 '],
' Page_style ' = Module_payment_paypal_page_style,
' Custom ' = Zen_session_name (). '=' . ZEN_SESSION_ID (),
' Business ' = module_payment_paypal_business_id,
' Return ' = Zen_href_link (filename_pay_success, ' referer=paypal ', ' SSL '),
' Cancel_return ' = Zen_href_link (filename_pay_failed, ', ' SSL '),
' Shopping_url ' = Zen_href_link (Filename_shopping_cart, ', ' SSL '),
' Notify_url ' = Zen_href_link (' ipn_main_handler.php ', ' ', ' SSL ', false,false,true),
' Redirect_cmd ' = ' _xclick ',
' RM ' = 2,
' bn ' = ' Zencart ',
' MRB ' = ' r-6c7952342h795591r ',
' Pal ' = ' 9E82WJBKKGPLQ ',
);
$optionsCust = Array (
' First_Name ' = replace_accents ($order->customer[' FirstName '),
' Last_Name ' = replace_accents ($order->customer[' LastName '),
' Address1 ' = replace_accents ($order->customer[' street_address '),
' City ' = replace_accents ($order->customer[' city '),
' state ' = Zen_get_zone_code ($order->customer[' country ' [' ID '], $order->customer[' zone_id '], $order- customer[' zone_id '),
' Zip ' = $order->customer[' postcode '],
' Country ' = $order->customer[' country ' [' iso_code_2 '],
' Email ' + $order->customer[' email_address '],
);
if ($order->customer[' suburb '! = ') $optionsCust [' address2 '] = $order->customer[' suburb '];
if (module_payment_paypal_address_required = = 2) $optionsCust = Array (
' Address_name ' = replace_accents ($order->customer[' FirstName '). ' ' . $order->customer[' LastName '),
' Address_street ' = replace_accents ($order->customer[' street_address '),
' Address_city ' = replace_accents ($order->customer[' city '),
' Address_state ' = Zen_get_zone_code ($order->customer[' country ' [' ID '], $order->customer[' zone_id '], $ order->customer[' zone_id '),
' Address_zip ' = $order->customer[' postcode '],
' Address_country ' = $order->customer[' country ' [' title '],
' Address_country_code ' = $order->customer[' country ' [' iso_code_2 '],
' Payer_email ' = $order->customer[' email_address '],
);
$optionsShip = Array (
' Address_override ' = Module_payment_paypal_address_override,
' No_shipping ' = module_payment_paypal_address_required,
);
if (Module_payment_paypal_detailed_cart = = ' Yes ') $optionsLineItems = Ipn_getlineitemdetails ();
if (sizeof ($optionsLineItems) > 0) {
$optionsLineItems [' cmd '] = ' _cart ';
$optionsLineItems [' num_cart_items '] = sizeof ($order->products);
if (Isset ($optionsLineItems [' shipping '])) {
$optionsLineItems [' shipping_1 '] = $optionsLineItems [' Shipping '];
unset ($optionsLineItems [' shipping ']);
}
if (Isset ($optionsLineItems [' Handling '])) {
$optionsLineItems [' handling_1 '] = $optionsLineItems [' Handling '];
unset ($optionsLineItems [' Handling ']);
}
unset ($optionsLineItems [' Subtotal ']);
If Line-item details couldn ' t is kept due to calculation mismatches or discounts etc, default to aggregate mode
if (!isset ($optionsLineItems [' item_name_1 ')) $optionsLineItems = Array ();
if ($optionsLineItems [' amount ']! = $this->transaction_amount) $optionsLineItems = Array ();
Ipn_debug_email (' line Item Details (if blank, this means there is a data mismatch, and thus bypassed): '. "\ n". Print_r ($optionsLineItems, true));
}
$products _name_display = "";
/*
For ($i =0, $n =sizeof ($order->products); $i < $n; $i + +) {
if (i > 0) {
$products _name_display.= ', ';
}
$products _name_display.= $order->products[$i [' name ']. ' ('. $order->products[$i] [' qty ']. ', '. $order->products[$i [' Dhisys_web_order_number ']. ') ';
}*/
$optionsAggregate = Array (
' cmd ' = ' _ext-enter ',
' Item_name ' = $products _name_display,
' Item_Number ' = $order _id,
' Num_cart_items ' = sizeof ($order->products),
' Amount ' = Number_format ($this->transaction_amount, $currencies->get_decimal_places ($my _currency)),
' Shipping ' = ' 0.00 ',
);
if (Module_payment_paypal_tax_override = = ' true ') $optionsAggregate [' tax '] = ' 0.00 ';
if (Module_payment_paypal_tax_override = = ' true ') $optionsAggregate [' tax_cart '] = ' 0.00 ';
$optionsTrans = Array (
' Upload ' (sizeof ($order->products) > 0),
' Currency_code ' = $my _currency,
' paypal_order_id ' = $paypal _order_id,
' No_note ' = ' 1 ',
' Invoice ' = ',
);
If Line-item info is invalid, use aggregate:
if (sizeof ($optionsLineItems) > 0) $optionsAggregate = $optionsLineItems;
Prepare submission
$options = Array_merge ($optionsCore, $optionsCust, $optionsPhone, $optionsShip, $optionsTrans, $optionsAggregate);
Ipn_debug_email (' Keys for submission: '. Print_r ($options, true));
if (sizeof ($order->products) > 0) {
$options [' cmd '] = ' _cart ';
For ($i =0, $n =sizeof ($order->products); $i < $n; $i + +) {
$options [' item_name_ '. (string) ($i + 1)] = $order->products[$i] [' name '];
$options [' Item_number_ '. (string) ($i + 1)] = $order->products[$i] [' Dhisys_web_order_number '];
$options [' Amount_ '. (string) ($i + 1)] = Number_format (float) $order->products[$i [' Final_price '],2];
$options [' Quantity_ '. (string) ($i + 1)] = $order->products[$i] [' qty '];
}
}
Build the button fields
foreach ($options as $name = = $value) {
Remove quotation marks
$value = Str_replace (' "', ' ', $value);
Check for invalid chars
if (Preg_match ('/[^a-za-z_0-9]/', $name)) {
Ipn_debug_email (' Datacheck-aborting-preg_match found invalid submission key: '. $name. ' ('. $value. ')');
Break
}
Do we need special handling for & and = symbols?
if (Strpos ($value, ' & ')!== false | | Strpos ($value, ' = ')!== false) $value = UrlEncode ($value);
$buttonArray [] = Zen_draw_hidden_field ($name, $value);
}
$_session[' paypal_transaction_info ' = Array ($this->transaction_amount, $this->transaction_currency);
$process _button_string = implode ("\ n", $buttonArray). "\ n";
return $process _button_string;
}
}
?>

3. On the Checkout_success page, show the Pay Now button. Open the file "includes/modules/pages/checkout_success/header.php" and add the following code at the end of the file (if you have mastered the notifier/viewer pattern in Zen-cart, And do not want to destroy the Zen-cart core code, you can also create an observation class to listen to Notify_header_end_checkout_success to implement).
Copy CodeThe code is as follows:
Require_once (dir_ws_classes. ' order.php ');
Require_once (dir_ws_classes. ' payment.php ');
$payment _modules = new Payment ($orders->fields[' Payment_module_code ');

Open the file "includes/modules/templates/template_default/templates/tpl_checkout_success_default.php" and add the following code to the appropriate location, Here is a judgment of the status of the order, when only the status of the order is in the unpaid status, the button is displayed,
Copy CodeThe code is as follows:

&& $orders->fields[' orders_status '] = = ' 1 '
if (Isset ($payment _modules->paynow_action_url) && $payment _modules->paynow_action_url! = "&& $orders->fields[' orders_status '] = = ' 1 ') {
Echo ('

');
Echo ("" . Text_paynow. ");
echo zen_draw_form (' Checkout_paynow ', $payment _modules->paynow_action_url, ' post ', ' id= ' checkout_confirmation ' Onsubmit= "Submitonce ();" ');
$selection = $payment _modules->selection ();
Echo ('. $selection [0][' module '].
Echo (');
if (Is_array ($payment _modules->modules)) {
echo $payment _modules->paynow_button ($orders _id);
}
Echo (Zen_image_submit (Button_image_paynow, Button_image_paynow_alt, ' name= ' Btn_paynow "id=" Btn_paynow "));
Echo (');
Echo (');
Echo (' ');
}
?>


4. Show the Pay Now button in the history order. There are three pages that need to show the Pay Now button: Account, Account_history,account_history_info, the implementation here and Checkout_success page implementation is similar, just passed to $payment_ Modules function Paynow_button The parameters are not the same, here will not repeat.
Summarize:
After the above modification, our process is as follows:
1. Shopping cart (shopping cart)
2. [Shipping Method (delivery method)]
3. Payment method (Payment method)
4. Order Confirmation (Confirmation)
5. Order Processing (Checkout process)
6. Order success (Checkout success)
7. [Payment by third party website]
Since the order confirmation to the order processing, is completed on our own website, and before entering the payment site, the order already exists, so there will be no case of a single.

http://www.bkjia.com/PHPjc/321347.html www.bkjia.com true http://www.bkjia.com/PHPjc/321347.html techarticle people who have used Zen-cart know that zen-cart the next step is one of the following (where the representation in [] is not required): 1. Cart (shopping cart) 2. [Shipping Method (delivery method)] 3. Support ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.