PayHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace common\pay\fulinmen;
  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['fulinmen.payUrl'];
  22. }
  23. if ($this->merNo == null) {
  24. $this->merNo = Yii::$app->params['fulinmen.merNo'];
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['fulinmen.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.0'; //版本号码
  40. $data['customerid'] = $this->merNo; //商户号码
  41. $data['sdorderno'] = $deposit['order_sn']; //订单号码
  42. $data['total_fee'] = sprintf("%.2f",$deposit['rmb']); //订单金额(以元为单位,保留两位小数)
  43. $data['paytype'] = 'bank'; //支付类型(网银)
  44. $data['bankcode'] = $params['bankCode']; //网银支付(额外参数:什么银行)
  45. //$data[''] = ''; //用户号
  46. $data['notifyurl'] = $this->notifyUrl; //异步地址
  47. $data['returnurl'] = $this->returnUrl; //同步地址
  48. $data['sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  49. return self::createHtml($data, $this->payUrl);
  50. }
  51. /**
  52. * @param array $data
  53. * @return bool
  54. */
  55. public function handleNotify($data)
  56. {
  57. if (PayUtils::checkSign($data, $this->secretKey)) {
  58. if ( isset($data['status']) && $data['status'] == "1" ) {
  59. $merOrderId = trim($data['sdorderno']);
  60. /** @var Deposit $model */
  61. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  62. if ($reuslt['type'] != 1) {
  63. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  64. $configData = Config::find()->asArray()->one();
  65. if ($configData['auto_deposit'] == 1 && $res) {
  66. $syncDespositModel = new SyncDesposit();
  67. $syncDespositModel->login = $reuslt['login'];
  68. $syncDespositModel->amount = $reuslt['amount'];
  69. $syncDespositModel->comment = 'Deposit';
  70. $syncDespositModel->memo = $merOrderId;
  71. $syncDespositModel->type = 2;
  72. $syncDespositModel->in_time = time();
  73. $syncDespositModel->save();
  74. }
  75. }
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. public function outNotify($success)
  82. {
  83. if ($success == true) {
  84. return "success";
  85. } else {
  86. return "FAIL";
  87. }
  88. }
  89. /**
  90. * @param array $data
  91. * @return bool
  92. */
  93. public function handleReturn($data)
  94. {
  95. if (PayUtils::checkSign($data, $this->secretKey)) {
  96. if (isset($data['status']) && $data['status'] == "1") {
  97. $merOrderId = trim($data['sdorderno']);
  98. /** @var Deposit $model */
  99. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  100. if ($model) {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. public function outReturn($success)
  108. {
  109. }
  110. }