PldController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/11/6
  6. * Time: 16:59
  7. */
  8. namespace app\payment\controllers;
  9. use app\payment\PaymentController;
  10. use app\common\helpers\Url;
  11. use app\common\models\AccountWechats;
  12. use app\frontend\modules\finance\models\BalanceRecharge;
  13. use app\common\services\Pay;
  14. class PldController extends PaymentController
  15. {
  16. private $attach = [];
  17. private $pld_proportion = 0;
  18. private $balance_proportion = 0;
  19. public function preAction()
  20. {
  21. parent::preAction();
  22. if (empty(\YunShop::app()->uniacid)) {
  23. $this->attach = explode('a', $_GET['OrderID']);
  24. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->attach[0];
  25. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  26. }
  27. //PLD币和余额的兑换比例
  28. $this->pld_proportion = \Setting::get('plugin.pld_pay.pld_proportion') ?:1;
  29. $this->balance_proportion = \Setting::get('plugin.pld_pay.balance_proportion') ?:1;
  30. }
  31. //异步充值通知
  32. public function notifyUrl()
  33. {
  34. $parameter = $_GET;
  35. \Log::debug('------------PLD异步通知----------------'.$_SERVER['QUERY_STRING']);
  36. if(!empty($parameter)){
  37. if($this->getSignResult($parameter)) {
  38. $recharge_log = BalanceRecharge::ofOrderSn($this->attach[1])->withoutGlobalScope('member_id')->first();
  39. if ($recharge_log && $recharge_log->status != 1) {
  40. $this->log($parameter);
  41. \Log::info('------PLD验证成功-----');
  42. $data = [
  43. 'total_fee' => $this->proportionPrice($parameter['Amount']),
  44. 'out_trade_no' => $this->attach[1],
  45. 'trade_no' => 'pld',
  46. 'unit' => 'yuan',
  47. 'pay_type' => 'PLD支付',
  48. 'pay_type_id' => 23,
  49. ];
  50. $this->payResutl($data);
  51. \Log::info('----PLD结束----');
  52. echo 'ok';
  53. } else {
  54. if ($recharge_log && $recharge_log->status == 1) {
  55. echo 'ok';
  56. }
  57. }
  58. } else {
  59. //签名验证失败
  60. echo '签名验证失败';
  61. }
  62. }else {
  63. echo '无参数';
  64. }
  65. }
  66. //同步充值通知
  67. public function returnUrl()
  68. {
  69. $parameter = $_GET;
  70. $this->log($parameter);
  71. if(!empty($parameter)){
  72. if($this->getSignResult($parameter)) {
  73. $recharge_log = BalanceRecharge::ofOrderSn($this->attach[1])->withoutGlobalScope('member_id')->first();
  74. if ($recharge_log && $recharge_log->status != 1) {
  75. \Log::info('------PLD验证成功-----');
  76. $data = [
  77. 'total_fee' => $this->proportionPrice($parameter['Amount']),
  78. 'out_trade_no' => $this->attach[1],
  79. 'trade_no' => 'pld',
  80. 'unit' => 'yuan',
  81. 'pay_type' => 'PLD支付',
  82. 'pay_type_id' => 23,
  83. ];
  84. $this->payResutl($data);
  85. \Log::info('----PLD结束----');
  86. redirect(Url::absoluteApp('member', ['i' => \YunShop::app()->uniacid]))->send();
  87. } else {
  88. if ($recharge_log && $recharge_log->status == 1) {
  89. \Log::debug('--------PLD充值已记录------------');
  90. redirect(Url::absoluteApp('member', ['i' => \YunShop::app()->uniacid]))->send();
  91. }
  92. //其他错误
  93. \Log::debug('----PLD充值记录不存在----');
  94. }
  95. } else {
  96. //签名验证失败
  97. \Log::debug('----PLD签名验证失败----');
  98. }
  99. }else {
  100. \Log::debug('----参数为空----');
  101. }
  102. redirect(Url::absoluteApp('home'))->send();
  103. }
  104. /**
  105. * 反转充值比例,用于做余额充值金额验证
  106. * @param $amount int 第三方返回金额
  107. * @return float
  108. */
  109. protected function proportionPrice($amount)
  110. {
  111. $amount = ($amount / $this->pld_proportion) * $this->balance_proportion;
  112. return floatval($amount);
  113. }
  114. /**
  115. * 签名验证
  116. *
  117. * @return bool
  118. */
  119. public function getSignResult($parameter)
  120. {
  121. $key = $parameter['OrderID'].$parameter['Amount'].'zhijie';
  122. $md5_key = md5(md5($key));
  123. return $parameter['Sign'] == $md5_key;
  124. }
  125. /**
  126. * 支付日志
  127. *
  128. * @param $post
  129. */
  130. public function log($data)
  131. {
  132. //访问记录
  133. Pay::payAccessLog();
  134. //保存响应数据
  135. Pay::payResponseDataLog($this->attach[1], 'PLD充值支付', json_encode($data));
  136. }
  137. }