PayHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\pay\yunfu;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\helpers\Utils;
  7. use common\pay\BasePayHandler;
  8. use Yii;
  9. class PayHandler extends BasePayHandler
  10. {
  11. public $payUrl; //支付网关
  12. public $merNo; //商户号码
  13. public $secretKey; //密钥
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['yunfu.payUrl'];
  22. }
  23. if ($this->merNo == null) {
  24. $this->merNo = Yii::$app->params['yunfu.merNo'];
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['yunfu.secretKey'];
  28. }
  29. }
  30. /**
  31. * @param array $deposit
  32. * @param array $params
  33. * @return string
  34. */
  35. public function outPay($deposit, $params = [])
  36. {
  37. $data = [];
  38. //提交的参数
  39. $data['version'] = '1.8'; //版本号码
  40. $data['merchantId'] = $this->merNo; //商户号码
  41. $data['goodsName'] = $deposit['order_sn']; //商品名称
  42. $data['payType'] = 'b2c'; //支付类型(B2C网银)
  43. $data['bankCode'] = $params['bankCode']; //网银支付(额外参数:什么银行)
  44. $data['orderId'] = $deposit['order_sn']; //订单号码
  45. $data['amount'] = sprintf("%.2f",$deposit['rmb']); //订单金额(以元为单位,保留两位小数)
  46. $data['notifyUrl'] = $this->notifyUrl; //异步地址
  47. $data['returnUrl'] = $this->returnUrl; //同步地址
  48. $data['signType'] = 'MD5'; //签名方式
  49. $data['sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  50. return self::createHtml($data, $this->payUrl);
  51. }
  52. /**
  53. * @param array $data
  54. * @return bool
  55. */
  56. public function handleNotify($data)
  57. {
  58. if (PayUtils::checkSign($data, $this->secretKey)) {
  59. if ( isset($data['status']) && $data['status'] == "SUCCESS" ) {
  60. $merOrderId = trim($data['orderId']);
  61. /** @var Deposit $model */
  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. }
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public function outNotify($success)
  83. {
  84. if ($success == true) {
  85. return "SUCCESS";
  86. } else {
  87. return "FAIL";
  88. }
  89. }
  90. /**
  91. * @param array $data
  92. * @return bool
  93. */
  94. public function handleReturn($data)
  95. {
  96. if (PayUtils::checkSign($data, $this->secretKey)) {
  97. if (isset($data['status']) && $data['status'] == "SUCCESS") {
  98. $merOrderId = trim($data['orderId']);
  99. /** @var Deposit $model */
  100. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  101. if ($model) {
  102. return true;
  103. }
  104. }
  105. }
  106. return false;
  107. }
  108. public function outReturn($success)
  109. {
  110. }
  111. }