<?php
/*
*   Name:   payforaffiliate.php
*   Author: easelify@gmail.com
*   Create: 2009/02/28 15:59
*   Modify: 2009/04/01 14:49
*/
header('Content-Type:text/html;charset=utf-8');
define('EPLUG_ABSPATH', dirname(__FILE__) . '/');
require_once(EPLUG_ABSPATH.'../settings/settings.php');
require_once(EPLUG_ABSPATH.'../include/Adodb/adodb.inc.php');
require_once(EPLUG_ABSPATH.'../alipay/alipay_config.php');
require_once(EPLUG_ABSPATH.'../alipay/alipay_service.php');

//数据库连接
$GLOBALS['DB'] = NewADOConnection(DB_TYPE);
$GLOBALS['DB']->Connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$GLOBALS['DB']->Execute("SET NAMES 'utf8'");

//数据过滤
$accountid = preg_replace('/[\'\"]/', '',$_POST['accountid']);
$userid = preg_replace('/[\'\"]/', '',$_POST['userid']);
$amount = preg_replace('/[\'\"]/', '',$_POST['amount']);

if(empty($accountid) || empty($userid) || empty($amount))
    exit('参数错误！');

//检测商家支付宝设置
$query = 'select pf.payfieldid from wd_pa_payoutfields pf 
    inner join wd_pa_payoutoptions po 
    on pf.payoptid = po.payoptid 
    where po.payoptid = "alipay" 
    and po.accountid="'.$accountid.'"';
$rs = $GLOBALS['DB']->Query($query);
if(!$rs || $rs->EOF) {
    exit('您的支付设置中没有设置支付宝方式，请设置。如有任何疑问，请联系推广易客户人员！');
}

//检测推广员制服宝帐号
$payfieldid = $rs->fields['payfieldid'];
$query = 'select `value` from wd_g_settings 
    where `code` = "Aff_payoptionfield_'.$payfieldid.'" 
    and userid="'.$userid.'"';
$rs = $GLOBALS['DB']->Query($query);
if(!$rs || $rs->EOF) {
    exit('该推广员的支付宝账号未设置，请联系推广员设置，否则无法使用支付宝方式付款！');
}
//向支付宝接口提交订单
$alipayid = $rs->fields['value'];
//$return_url = 'http://www.marketools.com.cn/merchants/alipay/return_url.php';
//$notify_url = 'http://www.marketools.com.cn/merchants/alipay/notify_url.php';
$parameter = array(
    "service"=> "create_direct_pay_by_user",
    "partner"=>$partner,                              //合作商户号
    "return_url"=>$return_url,                        //同步返回
    "notify_url"=>$notify_url,                        //异步返回
    "_input_charset"=>$_input_charset,                //字符集，默认为GBK
    "subject"=>"Marketools Merchant Pay",             //商品名称，必填
    "body"=>'Marketools Merchant Pay For Affiliate.', //商品描述，必填
    "out_trade_no"=>'MTPAP_ORDER'.time(),             //商品外部交易号，必填,每次提交都须修改
    "total_fee"=>0.01,//$amount,             //商品单价，必填
    "payment_type"=>"1",                              //默认为1,不需要修改
    "show_url"=>$show_url,                            //商品相关网站
    "seller_email"=>$alipayid                         //卖家邮箱，必填
);
$alipay = new alipay_service($parameter,$security_code,$sign_type);
$alipay_gateway = $alipay->create_url();
js_goto($alipay_gateway);
?>
