WelfarePrize.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Welfare\Services\WelfareOrderService;
  4. use App\Modules\Welfare\Services\WelfareRecordService;
  5. use Illuminate\Console\Command;
  6. use EasyWeChat\Foundation\Application;
  7. class WelfarePrize extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'welfare:prize {--day=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '发红包';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->start();
  38. }
  39. //开始
  40. private function start(){
  41. $prices = $this->getAllPrizes();
  42. for ($i = 0;$i <2 ;$i++){
  43. if($prices[$i]){
  44. // 'welfare_records.id,welfare_records.amount,welfare_users.openid';
  45. foreach ($prices[$i] as $v){
  46. $this->prize($v->id,$v->openid,$v->amount);
  47. }
  48. }
  49. }
  50. }
  51. //获取要发红包的列表
  52. private function getAllPrizes(){
  53. $day = $this->option('day');
  54. if($day){
  55. $now = WelfareRecordService::getNow(true,1,$day);
  56. return [[],$now];
  57. }
  58. $zero = strtotime(date('Y-m-d 00:00:00'));
  59. $yesterday = [];
  60. if(time() - $zero <=60){
  61. $yesterday = WelfareRecordService::getNow(true,100);
  62. }
  63. $now = WelfareRecordService::getNow();
  64. return [$yesterday,$now];
  65. }
  66. //发红包并记录
  67. private function prize($price_id,$openid,$amount){
  68. $params = [
  69. 'mch_appid'=>'wxe559e68b007476f6',
  70. 'mchid' =>'1493525602',
  71. 'nonce_str'=>'',
  72. 'openid'=>$openid,
  73. 'check_name'=>'NO_CHECK',
  74. 'amount'=>$amount*100,
  75. 'desc'=>'福利',
  76. 'spbill_create_ip'=>'47.97.120.133',
  77. 'partner_trade_no'=>date("YmdHis").hexdec(uniqid()),
  78. 'sign'=>''
  79. ];
  80. $create_info = WelfareOrderService::create($params);
  81. $options = [
  82. 'app_id'=>'wxe559e68b007476f6',
  83. 'payment'=>[
  84. 'merchant_id'=>'1493525602',
  85. 'key'=>'ee245088b93ba88008279d95f6d31223',
  86. 'cert_path'=>public_path('wxpay/ydylm/apiclient_cert.pem'),
  87. 'key_path'=>public_path('wxpay/ydylm/apiclient_key.pem')
  88. ]
  89. ];
  90. $app = new Application($options);
  91. $merchantPay = $app->merchant_pay;
  92. $result = $merchantPay->send([
  93. 'partner_trade_no' => $params['partner_trade_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  94. 'openid' => $openid,
  95. 'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  96. 'amount' => $amount*100, // 企业付款金额,单位为分
  97. 'desc' => '福利', // 企业付款操作说明信息。必填
  98. 'spbill_create_ip'=>'47.97.120.133'
  99. ]);
  100. $status = 'FAIL';
  101. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
  102. $status = 'SUCCESS';
  103. }
  104. WelfareOrderService::updateResult($create_info->id,['result'=>json_encode($result)]);
  105. WelfareRecordService::update($price_id,['status'=>$status]);
  106. }
  107. }