<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/9/21
 * Time: 下午7:54
 */

namespace Pay;

define(ALIPAY_PATH, BASE_DATA_PATH . '/api/alipay');

require_once (ALIPAY_PATH . '/lib/alipay_rsa.function.php');
require_once (ALIPAY_PATH . '/lib/alipay_submit.class.php');



class alipay implements IPay
{
    const pri_key_path = BASE_DATA_PATH.'/api/alipay/key/lrlz_private_key.pem';
    const ali_notifyurl = BASE_SITE_URL . '/mobile/alipay_notify_url.php';

    public function gen_pay($pay_sn, $fee,$order_sn,$subject)
    {
        $param = $this->getParam($pay_sn, $fee, $order_sn, $subject);
        $para_filter = paraFilter($param);
        $para_sort = argSort($para_filter);
        $prestr = createLinkstring($para_sort);
        $sign = rsaSign($prestr,self::pri_key_path);

        $para_sort['sign'] = $sign;
        $para_sort['sign_type'] = "RSA";
        $req_param = createLinkstringUrlencode($para_sort);

        return array("data" => $req_param);
    }

    /**
     * @param $pay_sn
     * @param $fee
     * @param $order_sn
     * @param $subject
     * @param $ret
     */
    private function getParam($pay_sn, $fee, $order_sn, $subject)
    {
        $ret['partner'] = '2088121219613123';                           // 合作者身份ID
        $ret['seller_id'] = 'napheir.ao@lrlz.com';                      // 签约卖家支付宝账号
        $ret['out_trade_no'] = $pay_sn;   // 商户网站唯一订单号
        $ret['subject'] = $subject;                                     // 商品名称
        $ret['body'] = "order_sn={$order_sn}";                          // 商品详情
        $total_fee = $fee / 100;
        $ret['total_fee'] = "{$total_fee}";// 商品金额
        $ret['notify_url'] = self::ali_notifyurl;                       // 服务器异步通知页面路径
        $ret['service'] = 'mobile.securitypay.pay';                     // 服务器接口名称，固定值
        $ret['payment_type'] = '1';                                     // 支付类型，固定值
        $ret['_input_charset'] = 'utf-8';                               // 参数编码，固定值
        $ret['it_b_pay'] = '30m';                                       // 设置未付款交易的超时时间 30min
        $ret['return_url'] = 'http://';                                 // 支付宝完成请求后，当前页面跳转到商户指定页面的路径，可空

        return $ret;
    }

    public function on_notify($post)
    {

    }
}
