PayHandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace common\pay\trustpay;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\pay\BasePayHandler;
  7. use Yii;
  8. use yii\helpers\VarDumper;
  9. class PayHandler extends BasePayHandler
  10. {
  11. public $payUrl; //网关地址
  12. public $merId; //商户名称
  13. public $privateKey; //商户私钥
  14. public $Key; //商户密钥
  15. /**
  16. * @inheritdoc
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->payUrl == null) {
  22. $this->payUrl = Yii::$app->params['trustpay.payUrl']; //网关地址
  23. }
  24. if ($this->merId == null) {
  25. $this->merId = Yii::$app->params['trustpay.merId']; //商户号码
  26. }
  27. if ($this->privateKey == null) {
  28. $this->privateKey = __DIR__ . '/prem/trustpay_private.pem'; //私钥
  29. }
  30. if ($this->Key == null) {
  31. $this->Key = __DIR__ . '/prem/trustpay_public.pem'; //公钥
  32. }
  33. }
  34. /**
  35. * @param array $deposit
  36. * @param array $params
  37. * @return string
  38. */
  39. public function outPay($deposit, $params = [])
  40. {
  41. $data = []; //存储数据的容器
  42. $data['MerchantID'] = $this->merId; //商户号
  43. $data['OrderNo'] = $deposit['order_sn']; //订单号
  44. $data['ProductName'] = $deposit['order_sn']; //商品名称
  45. $data['Amount'] = sprintf("%.2f",$deposit['amount']); //金额(数量和美元是挂钩的)
  46. $data['CallBackUrl'] = $this->notifyUrl; //回调地址(异步回调)
  47. $data['Sign'] = PayUtils::makeSign($data, $this->privateKey); //签名 (数据,公钥)
  48. //sign最终请求参数
  49. $data['Sign'] = UrlEncode($data['Sign']); //最终请求参数sign需要进行UrlEncode
  50. file_put_contents('tp_sign_after.txt',$data['Sign']);
  51. $result = static::createGetHtml($data, $this->payUrl); //发起get请求
  52. return $result;
  53. }
  54. /**
  55. * @param array $data
  56. * @return bool
  57. */
  58. public function handleNotify($data)
  59. {
  60. $tt = print_r($data,true);
  61. file_put_contents("trustpaydata.txt",$tt); //记录返回的数据
  62. if (PayUtils::verify($data, $this->Key)) { //验签函数通过
  63. file_put_contents('truspay.txt','验签成功');
  64. if($data['Status']==1){ //判断交易的状态(暂时文档并没有说明是什么)
  65. $merOrderId = $data['OrderNo']; //订单号码
  66. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  67. if ($reuslt['type'] != 1) {
  68. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  69. $configData = Config::find()->asArray()->one();
  70. if ($configData['auto_deposit'] == 1 && $res) {
  71. $syncDespositModel = new SyncDesposit();
  72. $syncDespositModel->login = $reuslt['login'];
  73. $syncDespositModel->amount = $reuslt['amount'];
  74. $syncDespositModel->comment = 'Deposit';
  75. $syncDespositModel->memo = $merOrderId;
  76. $syncDespositModel->type = 2;
  77. $syncDespositModel->in_time = time();
  78. $syncDespositModel->save();
  79. }
  80. return true;
  81. }
  82. }else{
  83. return false;
  84. }
  85. }else{
  86. return false;
  87. }
  88. }
  89. public function outNotify($success)
  90. {
  91. if ($success == true) {
  92. return "true";
  93. } else {
  94. // 返回的数据格式
  95. return "false";
  96. }
  97. }
  98. /**
  99. * @param array $data
  100. * @return bool
  101. */
  102. public function handleReturn($data)
  103. {
  104. if (PayUtils::verify($data,$this->Key)) {
  105. if($data['Status']==1){ //判断交易的状态(暂时文档并没有说明是什么)
  106. $merOrderId = $data['OrderNo']; //订单号码
  107. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  108. if ($reuslt) {
  109. return true;
  110. }
  111. }else{
  112. return false;
  113. }
  114. }else{
  115. return false;
  116. }
  117. }
  118. public function outReturn($success)
  119. {
  120. }
  121. // 发起get请求
  122. public static function createGetHtml($params, $action)
  123. {
  124. $encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
  125. $html = <<<eot
  126. <html>
  127. <head>
  128. <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
  129. </head>
  130. <body onload="javascript:document.pay_form.submit();">
  131. <form id="pay_form" name="pay_form" action="{$action}" method="get">
  132. eot;
  133. foreach ($params as $key => $value) {
  134. $html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
  135. }
  136. $html .= <<<eot
  137. <!-- <input type="submit" type="hidden">-->
  138. </form>
  139. </body>
  140. </html>
  141. eot;
  142. return $html;
  143. }
  144. }