随着实体店与网店的结合 很多实体店开始开发属于自己的网上商城,网络商城经常有一个做法就是不人名币转换成商城自己的表示方式,在此就叫做积分。ecshop有一个功能是在线充值人名币,现在我用此功能来实现人名币充值积分。
1,充值页面
复制ecshop用户中心user.php下‘account_deposit’下的程序与user_transaction.dwt下account_deposit的模版信息作为虫子积分的充值程序与模版文件。
2,数据库处理。
在数据库user_account中添加字段pay_points用于存取充值换算下来的积分。
3,充值提交程序处理
ecshop默认充值程序包括了退款与冲值的两个过程,在此我把简化好的程序贴出 供大家研究
include_once(ROOT_PATH . 'includes/lib_clips.php');
include_once(ROOT_PATH . 'includes/lib_order.php');
$amount = isset($_POST['amount']) ? floatval($_POST['amount']) : 0;
if ($amount <= 0)
{
show_message($_LANG['amount_gt_zero']);
}
/* 变量初始化 */
$surplus = array(
'user_id' => $user_id,
'rec_id' => !empty($_POST['rec_id']) ? intval($_POST['rec_id']) : 0,
'process_type' => isset($_POST['surplus_type']) ? intval($_POST['surplus_type']) : 0,
'payment_id' => isset($_POST['payment_id']) ? intval($_POST['payment_id']) : 0,
'user_note' => isset($_POST['user_note']) ? trim($_POST['user_note']) : '',
'amount' => $amount,
'pay_points' =>$amount*100/$_CFG['integral_scale']//通过后台积分有换算比例算出充值的积分
);
if ($surplus['payment_id'] <= 0)
{
show_message($_LANG['select_payment_pls']);
}
include_once(ROOT_PATH .'includes/lib_payment.php');
//获取支付方式名称
$payment_info = array();
$payment_info = payment_info($surplus['payment_id']);
$surplus['payment'] = $payment_info['pay_name'];
if ($surplus['rec_id'] > 0)
{
//更新会员账目明细
$surplus['rec_id'] = update_user_account($surplus);
}
else
{
//插入会员账目明细
$surplus['rec_id'] = insert_user_account($surplus, $amount);
}
//取得支付信息,生成支付代码
$payment = unserialize_config($payment_info['pay_config']);
//生成伪订单号, 不足的时候补0
$order = array();
$order['order_sn'] = $surplus['rec_id'];
$order['user_name'] = $_SESSION['user_name'];
$order['surplus_amount'] = $amount;
//计算支付手续费用
$payment_info['pay_fee'] = pay_fee($surplus['payment_id'], $order['surplus_amount'], 0);
//计算此次预付款需要支付的总金额
$order['order_amount'] = $amount + $payment_info['pay_fee'];
//记录支付log
$order['log_id'] = insert_pay_log($surplus['rec_id'], $order['order_amount'], $type=2, 0);//需要主意这的支付类型,ecshop默认的有两个0和1,这里把这个设置成2去边是积分充值。
/* 调用相应的支付方式文件 */
include_once(ROOT_PATH . 'includes/modules/payment/' . $payment_info['pay_code'] . '.php');
/* 取得在线支付方式的支付按钮 */
$pay_obj = new $payment_info['pay_code'];
$payment_info['pay_button'] = $pay_obj->get_code($order, $payment);
/* 模板赋值 */
$smarty->assign('payment', $payment_info);
$smarty->assign('pay_fee', price_format($payment_info['pay_fee'], false));
$smarty->assign('amount', price_format($amount, false));
$smarty->assign('order', $order);
$smarty->display('');//掉需要的模版,也可以在user_transaction.dwt中操作,模版信息就复制 act_account的模版直接使用,在其中可以添加一行说明充值了多少积分,在这就不说明了,如果有需要的朋友可以给我留言。
4,在第三部中用到函数insert_user_account这个函数需要修改为如下
function insert_user_account($surplus, $amount)
{
$sql = 'INSERT INTO ' .$GLOBALS['ecs']->table('user_account').
' (user_id, admin_user, amount, add_time, paid_time, admin_note, user_note, process_type, payment, is_paid,pay_points)'.
" VALUES ('$surplus[user_id]', '', '$amount', '".gmtime()."', 0, '', '$surplus[user_note]', '$surplus[process_type]', '$surplus[payment]', 0,'$surplus[pay_points]')";
$GLOBALS['db']->query($sql);
return $GLOBALS['db']->insert_id();
}
5,修改支付回调函数
支付回调函数在lib_payment.php中的order_paid,现在添加回调的处理。
{
/* 取得支付编号 */
$log_id = intval($log_id);
if ($log_id > 0)
{
/* 取得要修改的支付记录信息 */
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('pay_log') .
" WHERE log_id = '$log_id'";
$pay_log = $GLOBALS['db']->getRow($sql);
if ($pay_log && $pay_log['is_paid'] == 0)
{
/* 修改此次支付操作的状态为已付款 */
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('pay_log') .
" SET is_paid = '1' WHERE log_id = '$log_id'";
$GLOBALS['db']->query($sql);
/* 根据记录类型做相应处理 */
if ($pay_log['order_type'] == PAY_ORDER)
{
/* 取得订单信息 */
$sql = 'SELECT order_id, user_id, order_sn, consignee, address, tel, shipping_id, extension_code, extension_id, goods_amount ' .
'FROM ' . $GLOBALS['ecs']->table('order_info') .
" WHERE order_id = '$pay_log[order_id]'";
$order = $GLOBALS['db']->getRow($sql);
$order_id = $order['order_id'];
$order_sn = $order['order_sn'];
/* 修改订单状态为已付款 */
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('order_info') .
" SET order_status = '" . OS_CONFIRMED . "', " .
" confirm_time = '" . gmtime() . "', " .
" pay_status = '$pay_status', " .
" pay_time = '".gmtime()."', " .
" money_paid = order_amount," .
" order_amount = 0 ".
"WHERE order_id = '$order_id'";
$GLOBALS['db']->query($sql);
/* 记录订单操作记录 */
order_action($order_sn, OS_CONFIRMED, SS_UNSHIPPED, $pay_status, $note, $GLOBALS['_LANG']['buyer']);
/* 如果需要,发短信 */
if ($GLOBALS['_CFG']['sms_order_payed'] == '1' && $GLOBALS['_CFG']['sms_shop_mobile'] != '')
{
include_once(ROOT_PATH.'includes/cls_sms.php');
$sms = new sms();
$sms->send($GLOBALS['_CFG']['sms_shop_mobile'],
sprintf($GLOBALS['_LANG']['order_payed_sms'], $order_sn, $order['consignee'], $order['tel']),'', 13,1);
}
/* 对虚拟商品的支持 */
$virtual_goods = get_virtual_goods($order_id);
if (!empty($virtual_goods))
{
$msg = '';
if (!virtual_goods_ship($virtual_goods, $msg, $order_sn, true))
{
$GLOBALS['_LANG']['pay_success'] .= '
}
/* 如果订单没有配送方式,自动完成发货操作 */
if ($order['shipping_id'] == -1)
{
/* 将订单标识为已发货状态,并记录发货记录 */
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('order_info') .
" SET shipping_status = '" . SS_SHIPPED . "', shipping_time = '" . gmtime() . "'" .
" WHERE order_id = '$order_id'";
$GLOBALS['db']->query($sql);
/* 记录订单操作记录 */
order_action($order_sn, OS_CONFIRMED, SS_SHIPPED, $pay_status, $note, $GLOBALS['_LANG']['buyer']);
$integral = integral_to_give($order);
log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf($GLOBALS['_LANG']['order_gift_integral'], $order['order_sn']));
}
}
}
elseif ($pay_log['order_type'] == PAY_SURPLUS)
{
$sql = 'SELECT `id` FROM ' . $GLOBALS['ecs']->table('user_account') . " WHERE `id` = '$pay_log[order_id]' AND `is_paid` = 1 LIMIT 1";
$res_id=$GLOBALS['db']->getOne($sql);
if(empty($res_id))
{
/* 更新会员预付款的到款状态 */
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('user_account') .
" SET paid_time = '" .gmtime(). "', is_paid = 1" .
" WHERE id = '$pay_log[order_id]' LIMIT 1";
$GLOBALS['db']->query($sql);
/* 取得添加预付款的用户以及金额 */
$sql = "SELECT user_id, amount FROM " . $GLOBALS['ecs']->table('user_account') .
" WHERE id = '$pay_log[order_id]'";
$arr = $GLOBALS['db']->getRow($sql);
/* 修改会员帐户金额 */
$_LANG = array();
include_once(ROOT_PATH . 'languages/' . $GLOBALS['_CFG']['lang'] . '/user.php');
log_account_change($arr['user_id'], $arr['amount'], 0, 0, 0, $_LANG['surplus_type_0'], ACT_SAVING);
}
}
/*处理积分充值的过程*/
elseif ($pay_log['order_type'] == 2)
{
$sql = 'SELECT `id` FROM ' . $GLOBALS['ecs']->table('user_account') . " WHERE `id` = '$pay_log[order_id]' AND `is_paid` = 1 LIMIT 1";
$res_id=$GLOBALS['db']->getOne($sql);
if(empty($res_id))
{
/* 更新会员预付款的到款状态 */
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('user_account') .
" SET paid_time = '" .gmtime(). "', is_paid = 1" .
" WHERE id = '$pay_log[order_id]' LIMIT 1";
$GLOBALS['db']->query($sql);
/* 取得添加预付款的用户以及金额 */
$sql = "SELECT user_id, amount,pay_points FROM " . $GLOBALS['ecs']->table('user_account') .
" WHERE id = '$pay_log[order_id]'";
$arr = $GLOBALS['db']->getRow($sql);
/* 修改会员帐户金额 */
log_account_change($arr['user_id'], 0, 0, 0, $arr['pay_points'], '积分充值', ACT_SAVING);//积分充值
}
}
}
else
{
/* 取得已发货的虚拟商品信息 */
$post_virtual_goods = get_virtual_goods($pay_log['order_id'], true);
/* 有已发货的虚拟商品 */
if (!empty($post_virtual_goods))
{
$msg = '';
/* 检查两次刷新时间有无超过12小时 */
$sql = 'SELECT pay_time, order_sn FROM ' . $GLOBALS['ecs']->table('order_info') . " WHERE order_id = '$pay_log[order_id]'";
$row = $GLOBALS['db']->getRow($sql);
$intval_time = gmtime() - $row['pay_time'];
if ($intval_time >= 0 && $intval_time < 3600 * 12)
{
$virtual_card = array();
foreach ($post_virtual_goods as $code => $goods_list)
{
/* 只处理虚拟卡 */
if ($code == 'virtual_card')
{
foreach ($goods_list as $goods)
{
if ($info = virtual_card_result($row['order_sn'], $goods))
{
$virtual_card[] = array('goods_id'=>$goods['goods_id'], 'goods_name'=>$goods['goods_name'], 'info'=>$info);
}
}
$GLOBALS['smarty']->assign('virtual_card', $virtual_card);
}
}
}
else
{
$msg = '
}
$GLOBALS['_LANG']['pay_success'] .= $msg;
}
/* 取得未发货虚拟商品 */
$virtual_goods = get_virtual_goods($pay_log['order_id'], false);
if (!empty($virtual_goods))
{
$GLOBALS['_LANG']['pay_success'] .= '
' . $GLOBALS['_LANG']['virtual_goods_ship_fail'];
}
}
}
}
6,后台管理员确认设置
后台管理员可以直接确认充值成功,在此处理程序就不做详细解释。
总结:此次二次开发主要是处理在线接口完成支付回调信息的处理