<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>支付宝手机网站支付接口接口</title>
</head>
<?php
/* *
 * 功能：手机网站支付接口接入页
 * 版本：3.3
 * 修改日期：2012-07-23
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。

 *************************注意*************************
 * 如果您在接口集成过程中遇到问题，可以按照下面的途径来解决
 * 1、商户服务中心（https://b.alipay.com/support/helperApply.htm?action=consultationApply），提交申请集成协助，我们会有专业的技术工程师主动联系您协助解决
 * 2、商户帮助中心（http://help.alipay.com/support/232511-16307/0-16307.htm?sh=Y&info_type=9）
 * 3、支付宝论坛（http://club.alipay.com/read-htm-tid-8681712.html）
 * 如果不想使用扩展功能请把扩展功能参数赋空值。
 */

require_once("ourphpapi.php");
require_once("alipay.config.php");
require_once("lib/alipay_submit.class.php");

$buynumber = $ourphp_adminfont['buynumber'];
$alipay = $_POST['alipay'];

if($alipay == 'fk'){
	
	//验证开始
	$rs = $db -> select("`id`,`OP_Add`,`OP_Addtel`,`OP_Addname`","`ourphp_usershopadd`","where OP_Addindex = 1 && `OP_Adduser` = '".$_SESSION['username']."'");
	if(!$rs){
		exit("<script language=javascript> alert('".$buynumber."');location.replace('".$ourphp['webpath']."client/wap/?cn-usershopadd.html');</script>");
	}
	if(isset($_POST['id'])){
		$shopid = implode(',',$_POST["id"]);
	}else{
		$shopid = 0;
	}
	if ($shopid == 0){
		exit("请先选购商品!");
	}
	if(preg_match('/[a-zA-Z]/',$shopid)){
		exit("no!"); 
	}else{
		$shopid = $shopid;
	}
	
	$idsafe = str_replace(",","",$shopid);
	$idsafe = md5($idsafe.$ourphp['safecode']);
	if($_POST['idmd5'] != $idsafe){
		exit("no!");
	}
	
	$stocknumno = $ourphp_adminfont['stocknumno'];
	$query = $db -> listgo("`OP_Ordersnum`,`OP_Ordersusermarket`,`OP_Ordersfreight`,`OP_Ordersid`,`OP_Ordersproductatt`,`OP_Orderswebmarket`","`ourphp_orders`","where `id` in (".$shopid.") && OP_Ordersemail = '".$_SESSION['username']."' && OP_Orderspay = 1");
	$zj = '';
	while($ourphp_rs = $db -> whilego($query)){
		$zj += ($ourphp_rs[0] * $ourphp_rs[1]) + $ourphp_rs[2];
		
		$prinfo = $db -> select("OP_Stock,`OP_Specifications`","ourphp_product","where id = ".intval($ourphp_rs[3]));
		if($ourphp_rs[4] == '')
		{
			if($prinfo[0] < $ourphp_rs[0])
			{
				exit("<script language=javascript> alert('".$stocknumno."');history.go(-1);</script>");
			}			
		}else{
			
			$newkucun = str_replace("、", ",", $ourphp_rs[4]).','.$ourphp_rs[5].','.$ourphp_rs[1];
			$kucun = explode("|", $prinfo[1]);
			foreach($kucun as $op)
			{
				if(strpos($op,$newkucun) !== false){ 
					$kucun2 = explode(",",$op);
					if(end($kucun2) < $ourphp_rs[0])
					{
						exit("<script language=javascript> alert('".$stocknumno."');history.go(-1);</script>");
					}
				}
			}
			
		}
		
	}
	
	if($zj < 0.01){
		exit("<script language=javascript> alert('金额不能低于0.01元');history.go(-1);</script>");
	}
	
	//优惠券
	if($_POST['coupon'] == 'ourphp'){ 
		
		$zj = $zj;
		$coupon = explode('|','0|0|0');
		
	}else{
		
		$coupon = explode('|',$_POST['coupon']);
		if($zj < $coupon[2]){
			
			exit("<script language=javascript> alert('优惠券已过期,无法使用!~');history.go(-1);</script>");
			
		}else{
			
			$c = $db -> select("`id`,`OP_Timewin`","`ourphp_couponlist`","where `id` = ".intval($coupon[0])." && `OP_Type` = 0 && `OP_Md` = '".dowith_sql($coupon[1])."' && `OP_Username` = '".$_SESSION['username']."'");
			if($c){
				
				$date = date("Y-m-d H:i:s");
				if($c[1] != '0000-00-00 00:00:00'){
					if(strtotime($date) > strtotime($c[1])){
						exit("<script language=javascript> alert('优惠券已过期,无法使用!~');history.go(-1);</script>");
					}
				}
				
				$zj = $zj - $coupon[2];
				
			}else{
				
				exit("no!");
				
			}
			
		}
		
	}
	
	//卖家账户
	$alipay_username = $api[3];
	//商户订单号
	$alipay_tradeno = 'FK_'.date("Yhmdms");
	//订单名称
	$alipay_tradename = '商品付款(订单ID：'.$shopid.')';
	//付款金额
	$alipay_money = $zj;
	//订单描述
	$alipay_body = "fk|".$shopid."|".$safecode['id']."|".$safecode['md5code']."|".$coupon[0]."|".$coupon[2]."|".$rs[0];
	//商品展示地址
	$alipay_url = $web['webhttp'].$web['weburl'].$ourphp['webpath'].'client/wap/?cn-usercenter.html';
	
}elseif($alipay == 'cz'){
	
	if($_POST['WIDtotal_fee'] < 0.01){
		exit("<script language=javascript> alert('金额不能低于0.01元');history.go(-1);</script>");
	}
	//卖家账户
	$alipay_username = $api[3];
	//商户订单号
	$alipay_tradeno = 'CZ_'.date("Yhmdms");
	//订单名称
	$alipay_tradename = '用户账户充值';
	//付款金额
	$alipay_money = $_POST['WIDtotal_fee'];
	//订单描述
	$alipay_body = $_POST['WIDbody']."!|ourphp|".$safecode['id']."|".$safecode['md5code'];
	//商品展示地址
	$alipay_url = $web['webhttp'].$web['weburl'].$ourphp['webpath'].'client/wap/?cn-userpay.html';
	
}else{
	
	exit("no!");
	
}

/**************************请求参数**************************/

        $payment_type = "1";
        $notify_url = $web['webhttp'].$web['weburl'].$ourphp['webpath'].'function/api/alipay_wappay/notify_url.php';
        $return_url = $web['webhttp'].$web['weburl'].$ourphp['webpath'].'function/api/alipay_wappay/return_url.php';
        $out_trade_no = $alipay_tradeno;
        $subject = $alipay_tradename;
        $total_fee = $alipay_money;
        $show_url = $alipay_url;
        $body = $alipay_body;
        $it_b_pay = "";
        $extern_token = "";


/************************************************************/

//构造要请求的参数数组，无需改动
$parameter = array(
		"service" => "alipay.wap.create.direct.pay.by.user",
		"partner" => trim($alipay_config['partner']),
		"seller_id" => trim($alipay_config['seller_id']),
		"payment_type"	=> $payment_type,
		"notify_url"	=> $notify_url,
		"return_url"	=> $return_url,
		"out_trade_no"	=> $out_trade_no,
		"subject"	=> $subject,
		"total_fee"	=> $total_fee,
		"show_url"	=> $show_url,
		"body"	=> $body,
		"it_b_pay"	=> $it_b_pay,
		"extern_token"	=> $extern_token,
		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
);

//建立请求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "下一步或等待自动跳转");
echo $html_text;

?>
</body>
</html>