PayHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace common\pay\otczhifu;
  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['otczhifu.payUrl']; //网关地址
  23. }
  24. if ($this->merId == null) {
  25. $this->merId = Yii::$app->params['otczhifu.merId']; //商户号码
  26. }
  27. if ($this->privateKey == null) {
  28. $this->privateKey = __DIR__ . '/prem/otczhifu_private.pem'; //密钥
  29. }
  30. if ($this->Key == null) {
  31. $this->Key = Yii::$app->params['otczhifu.key']; //公钥
  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['s'] = 'PaymentOrder.AddPayment'; //支付类型
  43. $data['name'] = $this->merId; //商户号
  44. $data['num'] = $deposit['amount']; //金额(数量和美元是挂钩的)
  45. $data['returnUrl'] = $this->notifyUrl; //回调地址(异步回调)
  46. $data['paymentNo'] = $deposit['order_sn']; //订单号
  47. $data['memo'] = $deposit['order_sn']; //备注
  48. $reuslt = PayUtils::makeSign($data, $this->Key,$this->payUrl); //开始支付(传递数据,公钥,网关地址)
  49. return $reuslt;
  50. }
  51. /**
  52. * @param array $data
  53. * @return bool
  54. */
  55. public function handleNotify($data)
  56. {
  57. file_put_contents("data.txt",$data); //记录返回的数据
  58. $tt = PayUtils::response($data, $this->privateKey,$this->Key); //验签函数(需要参数, 私钥,公钥)
  59. if ($tt) { //验签函数通过
  60. $merOrderId = $tt;
  61. file_put_contents("dingdan.txt",$tt); //记录下来订单的号码
  62. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  63. if ($reuslt['type'] != 1) {
  64. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  65. $configData = Config::find()->asArray()->one();
  66. if ($configData['auto_deposit'] == 1 && $res) {
  67. $syncDespositModel = new SyncDesposit();
  68. $syncDespositModel->login = $reuslt['login'];
  69. $syncDespositModel->amount = $reuslt['amount'];
  70. $syncDespositModel->comment = 'Deposit';
  71. $syncDespositModel->memo = $merOrderId;
  72. $syncDespositModel->type = 2;
  73. $syncDespositModel->in_time = time();
  74. $syncDespositModel->save();
  75. }
  76. return true;
  77. }
  78. }else{
  79. return false;
  80. }
  81. }
  82. public function outNotify($success)
  83. {
  84. if ($success == true) {
  85. return "true";
  86. } else {
  87. // 返回的数据格式
  88. return "false";
  89. }
  90. }
  91. /**
  92. * @param array $data
  93. * @return bool
  94. */
  95. public function handleReturn($data)
  96. {
  97. $tt = PayUtils::response($data, $this->privateKey,$this->Key);
  98. if ($tt) {
  99. $merOrderId = $tt;
  100. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  101. if ($reuslt) {
  102. return true;
  103. }
  104. }else{
  105. return false;
  106. }
  107. }
  108. public function outReturn($success)
  109. {
  110. }
  111. }