BasePayment.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace Biz\Pay;
  3. /**
  4. * 基础支付类
  5. */
  6. class BasePayment {
  7. /**
  8. * 网关地址
  9. * @var [type]
  10. */
  11. protected $getWay;
  12. /**
  13. * 支付名称或代三
  14. * @var [type]
  15. */
  16. protected $paymentName;
  17. /**
  18. * 支付配置文件
  19. * @var [type]
  20. */
  21. protected $paymentConfig;
  22. /**
  23. * 通知地址
  24. * @var [type]
  25. */
  26. protected $notifyUrl;
  27. /**
  28. * 跳转地址
  29. * @var [type]
  30. */
  31. protected $redirectUrl;
  32. /**
  33. * 编码
  34. * @var string
  35. */
  36. protected $charset = 'utf-8';
  37. /**
  38. * 驱动数据对像
  39. * @var [type]
  40. */
  41. protected $dataAccess;
  42. /**
  43. * 私有证书路径
  44. * @var [type]
  45. */
  46. protected $privateCertPath;
  47. /**
  48. * 公有证书路径
  49. * @var [type]
  50. */
  51. protected $publicCertPath;
  52. /**
  53. * 后台配置的商户信息将在初始化时自动解析完成
  54. * @var [type]
  55. */
  56. protected $sellerInfo;
  57. /**
  58. * 设定支付接口允许的域名
  59. * @var [type]
  60. */
  61. protected $domain;
  62. /**
  63. * 返回类型
  64. * @var [type]
  65. */
  66. protected $returnType;
  67. /**
  68. * 是否手机版
  69. *
  70. * @var [type]
  71. */
  72. public $isMobile;
  73. /**
  74. * 开启调试模式
  75. * @var integer
  76. */
  77. public $debug = 0;
  78. /**
  79. * 初始化
  80. */
  81. public function __construct(PaymentDataAccess $dataAccess) {
  82. // $this->getConfig();
  83. $this->domain = PAY_URI;
  84. $this->notifyUrl = "http://{$this->domain}/pay-{$this->paymentName}Notify";
  85. $this->redirectUrl = "http://{$this->domain}/pay-{$this->paymentName}Redirect";
  86. $this->dataAccess = $dataAccess;
  87. $this->isMobile = is_mobile();
  88. $this->init();
  89. }
  90. /**
  91. * 设置返回类型
  92. * @param [type] $type [description]
  93. */
  94. public function setReturnType($type) {
  95. $this->returnType = $type;
  96. }
  97. /**
  98. * 发起配置
  99. * @param array $orderInfo 订单信息
  100. * @return [type] 发起支付流程
  101. */
  102. public function toPay() {
  103. }
  104. /**
  105. * 接收通知
  106. * @param array $orderInfo 订单信息
  107. * @return [type] [description] 返回为1或success视接口而定
  108. */
  109. public function notify() {
  110. }
  111. /**
  112. * 返回跳转
  113. * @param array $orderInfo 订单信息
  114. * @return [type] [description]
  115. */
  116. public function redirect() {
  117. }
  118. /**
  119. * 检查支付信息
  120. * @param array $orderInfo 订单信息
  121. * @return [type] 0=失败,1=成功
  122. */
  123. public function check() {
  124. # code...
  125. }
  126. /**
  127. * 返回DATAACCESS
  128. */
  129. public function DataAccess() {
  130. return $this->dataAccess;
  131. }
  132. /**
  133. * 获取配置信息
  134. * @return [type] [description]
  135. */
  136. public function getConfig() {
  137. if (empty($this->paymentName)) {
  138. throw new \Exception("支付名不能为空", 1);
  139. }
  140. $this->paymentConfig = $M('payment')->where('payment_name', $this->paymentName)->find();
  141. if (!$this->paymentConfig) {
  142. throw new \Exception("支付不存在", 1);
  143. }
  144. $this->privateCertPath = $this->paymentConfig['cert_priv_path'];
  145. $this->publicCertPath = $this->paymentConfig['cert_pub_path'];
  146. $this->sellerInfo = empty($this->paymentConfig['seller_info']) ? '' : json_decode($this->paymentConfig['seller_info'], 1);
  147. if (!(is_array($this->sellerInfo) && count($this->sellerInfo) > 0)) {
  148. throw new \Exception("商户信息不存在", 1);
  149. }
  150. }
  151. /**
  152. * 系统初始化
  153. * @return [type] [description]
  154. */
  155. public function init() {
  156. }
  157. /**
  158. * 跳转到支付接口
  159. * @param [type] $orderInfo 订单信息
  160. * @param string $action 提交方式
  161. * @return [type] void
  162. */
  163. public function goPay($orderInfo = array(), $action = "POST", $url = '') {
  164. if ($this->debug) {
  165. $inputType = "text";
  166. $script = '';
  167. } else {
  168. $inputType = "hidden";
  169. $script = 'document.getElementById("submitForm").submit();';
  170. }
  171. if (is_array($orderInfo) && count($orderInfo) > 0) {
  172. foreach ($orderInfo as $k => $v) {
  173. $forms .= "<input type={$inputType} name='{$k}' value='{$v}' />";
  174. }
  175. }
  176. if (empty($url)) {
  177. $url = $this->getWay;
  178. }
  179. $str = "<html>
  180. <head>
  181. <title>Payment By CreditCard online</title>
  182. <meta http-equiv='Content-Type' content='text/html; charset={$this->charset}'>
  183. </head>
  184. <body>
  185. <form action='{$url}' method='{$action}' id='submitForm' name='E_FORM'>
  186. {$forms}
  187. </form>
  188. </body>
  189. </html>
  190. <script type='text/javascript'>
  191. {$script}
  192. </script>
  193. ";
  194. //toLog($str);
  195. echo $str;
  196. }
  197. /**
  198. * 返回支付类型的数组
  199. * @return [type] [description]
  200. */
  201. public function getPaymentType() {
  202. # code...
  203. }
  204. /**
  205. * 提取证书中的公钥和私钥KEy,以数据形式返回
  206. * @return [type] [description]
  207. */
  208. public function certPatchKey() {
  209. $priContent = file_get_contents($this->privateCertPath);
  210. $pubContent = file_get_contents($this->publicCertPath);
  211. $priKey = openssl_get_privatekey($priContent);
  212. $pubKey = openssl_pkey_get_public($pubContent);
  213. if (empty($priKey) || empty($pubKey)) {
  214. throw new \Exception("支付的私钥和公钥不能为空", 1);
  215. }
  216. return array('private' => $priKey, 'public' => $pubKey);
  217. }
  218. /**
  219. * 处理CURL提交
  220. * @param [type] $url [description]
  221. * @param string $action [description]
  222. * @param array $data [description]
  223. * @return [type] [description]
  224. */
  225. function goPayCurl($url, $action = "POST", $data = array(), $header = array()) {
  226. //toLog($url);
  227. //toLog($data);
  228. if (is_array($data)) {
  229. $data = http_build_query($data);
  230. }
  231. $ch = curl_init();
  232. $res = curl_setopt($ch, CURLOPT_URL, $url);
  233. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  234. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  235. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  236. curl_setopt($ch, CURLOPT_POST, 1);
  237. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  238. if (is_array($header) && count($header) > 0) {
  239. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  240. curl_setopt($ch, CURLOPT_HEADER, 0);
  241. }
  242. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  243. // curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
  244. $result = curl_exec($ch);
  245. $errno = curl_errno($ch);
  246. curl_close($ch);
  247. if ($result == NULL) {
  248. return 0;
  249. }
  250. return $result;
  251. }
  252. /**
  253. * 发送信息到远程
  254. * @param string $action [description]
  255. * @param integer $status [description]
  256. * @return [type] [description]
  257. */
  258. function toRemoteUri($action = 'notify', $status = 0) {
  259. $data = array(
  260. 'orderSn' => $this->dataAccess->orderSn,
  261. 'money' => $this->dataAccess->money,
  262. 'goodsName' => ($this->dataAccess->goodsName == null ? '' : $this->dataAccess->goodsName),
  263. 'status' => $status,
  264. );
  265. $sign = http_build_query($data);
  266. $sign = urldecode($sign);
  267. $key = md5($sign);
  268. $sign = md5($sign . '&' . $key);
  269. $data['sign'] = $sign;
  270. if ($action == 'notify') {
  271. if ($remote_type == 0) {
  272. $url = "http://" . WEB_URI . "/Home/ReceiveNotify/Notify";
  273. return $this->goPayCurl($url, 'POST', $data);
  274. }
  275. } else {
  276. $url = "http://" . WEB_URI . "/Home/ReceiveNotify/Redirect";
  277. $this->goPay($data, 'POST', $url);
  278. }
  279. }
  280. /**
  281. * 发送信息到远程
  282. * @param string $action [description]
  283. * @param integer $status [description]
  284. * @return [type] [description]
  285. */
  286. function reciveRemoteDetails() {
  287. $data = array(
  288. 'orderSn' => trim($_POST['orderSn']),
  289. 'money' => trim($_POST['money']),
  290. 'goodsName' => trim($_POST['goodsName']),
  291. 'status' => trim($_POST['status']),
  292. );
  293. $sourceSign = trim($_POST['sign']);
  294. $sign = http_build_query($data);
  295. $sign = urldecode($sign);
  296. $key = md5($sign);
  297. $sign = md5($sign . '&' . $key);
  298. if ($sign == $sourceSign) {
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. /**
  304. * 提示信息
  305. * @param [type] $key [description]
  306. * @return [type] [description]
  307. */
  308. function notifyMsg($key) {
  309. }
  310. /**
  311. * 提示信息
  312. * @param [type] $msg [description]
  313. * @param integer $code [description]
  314. * @return [type] [description]
  315. */
  316. function showNotice($msg, $code = -1) {
  317. if ($this->returnType == 'json') {
  318. JsonReturn('', $code, $msg);
  319. } else {
  320. exit($msg);
  321. }
  322. }
  323. /**
  324. * 成功信息
  325. * @param [type] $data [description]
  326. * @return [type] [description]
  327. */
  328. function jsonSuccess($data) {
  329. if ($this->returnType == 'json') {
  330. JsonReturn($data, 1, 'success');
  331. }
  332. }
  333. /**
  334. * 检查金额
  335. * @param [type] $money [description]
  336. * @return [type] [description]
  337. */
  338. function checkMoney($money) {
  339. }
  340. }
  341. ?>