Browse Source

commit common and pay model

superbee 8 years ago
parent
commit
8ec7d4de36

+ 71 - 0
application/common.php

@@ -125,4 +125,75 @@
 		}
 		
 		return $short_url_list[rand(0, count($short_url_list)-1)];
+	}
+	
+	/**
+	 * 得到支付宝签名字符串
+	 * @param array $params
+	 * @param string $postCharset
+	 * @return string
+	 */
+	function getAliSignContent($params, $postCharset = 'UTF-8') {
+		ksort($params);
+		
+		$stringToBeSigned = "";
+		$i = 0;
+		foreach ($params as $k => $v) {
+			if ($v && trim($v) && "@" != substr($v, 0, 1)) {
+				
+				// 转换成目标字符集
+				$v = characet($v, $postCharset);
+				
+				if ($i == 0) {
+					$stringToBeSigned .= "$k" . "=" . "$v";
+				} else {
+					$stringToBeSigned .= "&" . "$k" . "=" . "$v";
+				}
+				$i++;
+			}
+		}
+		
+		unset ($k, $v);
+		return $stringToBeSigned;
+	}
+	
+	/**
+	 * 转换字符集编码
+	 * @param $data
+	 * @param $targetCharset
+	 * @return string
+	 */
+	function characet($data, $targetCharset) {
+		
+		if (!empty($data)) {
+			$data = mb_convert_encoding($data, $targetCharset, 'auto');
+			//	$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
+		}
+		
+		return $data;
+	}
+	
+	/**
+	 * 支付宝签名
+	 * @param string $privateKey 商家私钥
+	 * @param array $data 支付数据信息
+	 * @param string $signType 签名类型
+	 * @return string
+	 */
+	function aliSign($privateKey, $data, $signType = "RSA2") {
+		$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
+				wordwrap($privateKey, 64, "\n", true) .
+				"\n-----END RSA PRIVATE KEY-----";
+
+		
+		($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
+		
+		if ("RSA2" == $signType) {
+			openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
+		} else {
+			openssl_sign($data, $sign, $res);
+		}
+		
+		$sign = base64_encode($sign);
+		return $sign;
 	}

+ 86 - 0
application/home/controller/Pay.php

@@ -0,0 +1,86 @@
+<?php
+namespace app\home\controller;
+
+use think\Controller;
+use think\Db;
+use app\user\model\PayPlugin;
+use app\user\model\User;
+use app\user\model\UserPay;
+
+class Pay extends Controller{
+	private $payPluginModel, $userModel;
+	
+	public function _initialize(){
+		
+		$this->payPluginModel= new PayPlugin();
+		$this->userModel = new User();
+	}
+	
+	public function pay(){
+		$data = decode($this->request->post());
+		
+		$data['t'] = 1;
+		$data['i'] = 1;
+		$data['u'] = 324234;
+		
+		$ipaInfo = Db::name('iap_config')->where(['id'=>$data['i']])->find();
+		if(empty($ipaInfo)) dir(json(['error'=>1111]));
+		
+		$plugin = $this->payPluginModel->getPayPluginByType($data['t']);
+		if(empty($plugin)) dir(json(['error'=>1111]));
+		
+		$biz_content = $this->getAliTradeInfo($ipaInfo, $data['u']);
+		$val = $this->getAliPayInfo($plugin, $biz_content);
+		print_r($val);
+		exit;
+		
+		return http_build_query($val);
+	}
+	
+	public function notify(){
+		
+	}
+	
+	private function getAliTradeInfo($ipa, $user){
+		$content = [];
+		
+		$content['subject'] = "玩家充值";
+		$content['out_trade_no'] = $tradeNo = 'D'.getId();
+		$content['total_amount'] = $ipa['price'];
+		$content['product_code'] = 'QUICK_MSECURITY_PAY';
+		$content['timeout_express'] = '90m';
+		$content['body'] = $ipa['desc'];
+		
+// 		$userPay = new UserPay();
+// 		$userPay->add(['user_id'=>$user, 'cost'=>$ipa['price'], 'pay_id'=>$ipa['id'], 'out_trade_no'=>$tradeNo]);
+		
+		return json_encode($content);
+	}
+	
+	private function getAliPayInfo($plugin, $biz_content){
+		$params = [];
+		
+		$params['gatewayUrl'] = "https://openapi.alipaydev.com/gateway.do";
+		
+		$params['app_id'] = $plugin['appid'];
+		$params['method'] = 'alipay.trade.app.pay';
+		$params['format'] = "json";
+		$params['charset'] = "UTF-8";
+		$params['version'] = "1.0";
+		$params['timestamp'] = getCurrentTime();
+		$params['sign_type'] = "RSA2";
+		$params['notify_url'] = 'http://'.$_SERVER['SERVER_NAME']._PHP_FILE_.'/'.$this->request->module().'/'.$this->request->controller().'/notify';
+		$params['biz_content'] = $biz_content;
+		
+		ksort($params);
+		
+		$params['sign'] = aliSign($plugin['private_key'], getAliSignContent($params));
+		
+		foreach ($params as &$value) {
+			$value = characet($value, $params['charset']);
+		}
+		
+		return $params;
+	}
+	
+}

+ 1 - 0
application/user/controller/Pay.php

@@ -0,0 +1 @@
+<?php

+ 58 - 0
application/user/model/PayPlugin.php

@@ -0,0 +1,58 @@
+<?php
+namespace app\user\model;
+
+use think\Model;
+
+class PayPlugin extends Model{
+	
+	protected $updateTime = 'update_time';			// 每次更新时间
+	protected $autoWriteTimestamp = 'datetime';		// 开启自动写入时间戳字段
+	
+	public function initialize(){
+		parent::initialize();
+	}
+	
+	/**
+	 * 获取数据信息
+	 * @param int $page 页码
+	 * @param array $where 查询条件
+	 */
+	public function getInfo($page, $where = array()){
+		return $this->db()
+		->where($where)
+		->page($page, config('paginate.list_rows'))
+		->select();
+	}
+	
+	/**
+	 * 获取分页信息
+	 * @param array $where 查询条件
+	 */
+	public function getPage($where = array()){
+		$query = getPaginatiorQuery();
+		$total = $this->db()->where($where)->count();
+		// 查询条件
+		$config = ['query'=>$query];
+		$paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total, $config);
+		
+		return $paginate->render();
+	}
+	
+	/**
+	 * 根据id找到支付信息
+	 * @param int $id
+	 * @return array|\think\db\false|PDOStatement|string|\think\Model
+	 */
+	public function getPayPluginById($id){
+		return $this->db()->where(['id'=>$id])->find();
+	}
+	
+	/**
+	 * 根据状态找到支付信息
+	 * @param int $type
+	 * @return mixed|array|array[]|object[]
+	 */
+	public function getPayPluginByType($type){
+		return $this->db()->where(['type'=>$type])->find()->getData();
+	}
+}

+ 19 - 0
application/user/model/UserPay.php

@@ -0,0 +1,19 @@
+<?php
+namespace app\user\model;
+
+use think\Model;
+
+class UserPay extends Model{
+	
+	protected $createTime = 'create_time';			// 每次更新时间
+	protected $autoWriteTimestamp = 'datetime';		// 开启自动写入时间戳字段
+	
+	public function initialize(){
+		parent::initialize();
+	}
+	
+	public function add($data){
+		return $this->db()->insert($data);
+	}
+	
+}