IRefill.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace refill;
  3. abstract class IRefill
  4. {
  5. protected $mName;
  6. protected $mSotreID;
  7. protected $mRefillType;
  8. protected $mOfficialSN;
  9. protected $mCardType = [];
  10. protected $mAmounts = [];
  11. protected $mPeriod;
  12. public function __construct($cfgs)
  13. {
  14. $this->mName = $cfgs['name'];
  15. $this->mSotreID = $cfgs['store_id'];
  16. $this->mRefillType = $cfgs['refill_type'];
  17. foreach ( $cfgs['amount'] as $k => $v) {
  18. $this->mAmounts[$k] = $v;
  19. }
  20. $this->mPeriod = $cfgs['period'];
  21. $this->mOfficialSN = $cfgs['official_sn'] ?? false;
  22. }
  23. abstract public function add($card_no,$card_type,$amount,$params);
  24. abstract public function query($refill_info);
  25. public function name() {
  26. return $this->mName;
  27. }
  28. public function store_id() {
  29. return $this->mSotreID;
  30. }
  31. public function official_sn() {
  32. return $this->mOfficialSN === true;
  33. }
  34. public function callback() {
  35. return true;
  36. }
  37. public function refill_type() {
  38. return $this->mRefillType;
  39. }
  40. protected function match_period()
  41. {
  42. $period = $this->mPeriod;
  43. if(empty($period)) {
  44. return true;
  45. }
  46. $cur_time = time();
  47. $date = date('Y-m-d',$cur_time);
  48. $day_start = strtotime("{$date} {$period['start']}");
  49. if(empty($period['end'])) {
  50. $day_end = strtotime("{$date}") + 86400;
  51. }
  52. else {
  53. $day_end = strtotime("{$date} {$period['end']}");
  54. }
  55. return ($cur_time >= $day_start && $cur_time < $day_end);
  56. }
  57. public function amounts()
  58. {
  59. return $this->mAmounts;
  60. }
  61. public function goods(int $amout)
  62. {
  63. if(array_key_exists($amout,$this->mAmounts)) {
  64. $params = $this->mAmounts[$amout];
  65. return [$params['goods_id'], $params['price']];
  66. }
  67. else {
  68. return [];
  69. }
  70. }
  71. public function check(int $amount,int $card_type) : array
  72. {
  73. if(!array_key_exists($amount,$this->mAmounts)) {
  74. return [false,'额度不支持'];
  75. }
  76. if(!in_array($card_type,$this->mCardType)) {
  77. return [false,'不支持该类型'];
  78. }
  79. if(!$this->match_period()) {
  80. return [false,'不在充值时间范围内'];
  81. }
  82. return [true,'success'];
  83. }
  84. protected function check_empty($value)
  85. {
  86. if (!isset($value))
  87. return true;
  88. if ($value === null)
  89. return true;
  90. if (trim($value) === "")
  91. return true;
  92. return false;
  93. }
  94. }