CardRidingOrder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Log;
  5. class CardRidingOrder extends Model
  6. {
  7. public $table = 'card_riding_orders';
  8. // protected $fillable = ['no', 'area_id', 'user_id', 'riding_card_id', 'money', 'discount', 'pay_money', 'pay_type', 'pay_status', 'pay_time', 'status'];
  9. protected $guarded = [];
  10. const NO_TAG = 'R';
  11. const PAY_TYPE_WECHAT = 1;
  12. const PAY_TYPE_ACCOUNT = 2;
  13. const PAY_TYPE_ADMIN_GIVE = 3;
  14. const PAY_TYPE_INVITE_NEW_USER_GIVE = 4;
  15. const PAY_TYPE_NEW_USER_OVER_AUTH_SERVER_GIVE = 5;
  16. const PAY_TYPE_ALIPAYMINI = 6;
  17. public static $payTypeMaps = [
  18. self::PAY_TYPE_WECHAT => '微信支付',
  19. self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
  20. self::PAY_TYPE_ACCOUNT => '余额支付',
  21. self::PAY_TYPE_ADMIN_GIVE => '后台赠送',
  22. self::PAY_TYPE_INVITE_NEW_USER_GIVE => '邀请新用户赠送',
  23. self::PAY_TYPE_NEW_USER_OVER_AUTH_SERVER_GIVE => '新用户完成实名系统赠送'
  24. ];
  25. const PAY_STATUS_OK = 1;
  26. const PAY_STATUS_NO = 0;
  27. public static $payStatusMaps = [
  28. self::PAY_STATUS_OK => '支付成功',
  29. self::PAY_STATUS_NO => '支付失败',
  30. ];
  31. const STATUS_OK = 1;
  32. const STATUS_NO = 0;
  33. public static $statusMaps = [
  34. self::STATUS_OK => '支付成功',
  35. self::STATUS_NO => '支付失败'
  36. ];
  37. /**
  38. * 生成订单号
  39. * @return bool|string
  40. * User: Mead
  41. */
  42. public static function makeNo($tag = null)
  43. {
  44. // 订单流水号前缀
  45. $prefix = $tag . self::NO_TAG . date('YmdHis');
  46. for ($i = 0; $i < 10; $i++) {
  47. // 随机生成 6 位的数字
  48. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  49. // 判断是否已经存在
  50. if (!static::query()->where('no', $no)->exists()) {
  51. return $no;
  52. }
  53. }
  54. Log::warning('find order no failed');
  55. return false;
  56. }
  57. public function pay_order_callback()
  58. {
  59. $order = $this->attributes;
  60. //添加钱包记录
  61. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
  62. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
  63. //修改订单记录
  64. $this->pay_status = CardRidingOrder::PAY_STATUS_OK;
  65. $this->pay_time = now();
  66. $this->pay_type = CardRidingOrder::PAY_TYPE_WECHAT;
  67. $this->status = CardRidingOrder::STATUS_OK;
  68. $this->save();
  69. //支付成功给用户发卡
  70. $cardRiding = CardRiding::find($this->riding_card_id);
  71. $take_effect_time = Carbon::now()->format('Y-m-d H:i:s');
  72. $expiration_time = Carbon::now()->addDays($cardRiding->effective_days)->format('Y-m-d H:i:s');
  73. $data = [
  74. 'user_id' => $this->attributes['user_id'],
  75. 'riding_card_id' => $this->attributes['riding_card_id'],
  76. 'card_riding_order_id' => $this->attributes['id'],
  77. 'take_effect_time' => $take_effect_time,
  78. 'deduction_money' => $cardRiding->deduction_money,
  79. 'expiration_time' => $expiration_time,
  80. 'is_limit_times' => $cardRiding->is_limit_times,
  81. 'can_ridding_times' => $cardRiding->times,
  82. 'day_can_ridding_times' => $cardRiding->day_can_ridding_times,
  83. 'status' => CardRidingUserBags::STATUS_OK,
  84. 'merchant_id' => $order['merchant_id']
  85. ];
  86. // Log::info($data);
  87. CardRidingUserBags::firstOrCreate(['card_riding_order_id' => $this->attributes['id']], $data);
  88. return true;
  89. }
  90. }