家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RewardController.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Reward;
  5. use App\Models\RewardPrice;
  6. use Carbon\Carbon;
  7. class RewardController extends Controller
  8. {
  9. public $price = 0;
  10. public function __construct() {
  11. $rewardPrice = RewardPrice::find(1);
  12. $this->price = $rewardPrice->price;
  13. }
  14. public function getPrice(Request $request) {
  15. $rewardPrice = RewardPrice::find(1);
  16. return response()->json([
  17. "status" => 0,
  18. "data" => [
  19. "price" => $rewardPrice->price
  20. ]
  21. ]);
  22. }
  23. public function price(Request $request) {
  24. $price = $request->input("price");
  25. if($price) {
  26. $rewardPrice = RewardPrice::find(1);
  27. $rewardPrice->price = $price;
  28. $rewardPrice->save();
  29. $this->price = $price;
  30. return response()->json([
  31. "status" => 0,
  32. "message" => '修改成功'
  33. ]);
  34. } else {
  35. return Response()->json([
  36. "status" => -1,
  37. "message" => "信息不能为空!"
  38. ]);
  39. }
  40. }
  41. public function reward(Request $request) {
  42. $wechat_id = $request->header("openid");
  43. if($wechat_id) {
  44. $price = $this->price;
  45. $order_type = "reward";
  46. $reward = new Reward;
  47. $reward->status = 0;
  48. $reward->price = $price;
  49. $reward->wechat_id = $wechat_id;
  50. $reward->save();
  51. $order_id = $reward->id;
  52. $order = CommonController::getRecord($order_type, $reward->id);
  53. // 先判断微信交易是否失效,失效的话需要重新生成份新的系统内部订单,再重新统一下单
  54. if ($wechat_id && $order) {
  55. // 判断微信交易是否失效
  56. if(Carbon::now()->subHour()->gt($order->created_at)) {
  57. $order_new = clone $order;
  58. $order_new->id = null;
  59. $order_new->exists = false;
  60. $order_new->save();
  61. $order->delete();
  62. $order_id = $order_new->id;
  63. }
  64. $out_trade_no = $order_type . "-" . $order_id;
  65. $app = app('wechat.payment');
  66. $result = $app->order->unify([
  67. 'body' => '玥子轩家政',
  68. 'out_trade_no' => $out_trade_no,
  69. 'total_fee' => $order->price * 100,
  70. 'trade_type' => 'JSAPI',
  71. 'openid' => $wechat_id,
  72. 'notify_url' => 'https://wechat.oskey.cn/api/payments/wechat-notify',
  73. 'time_expire' => Carbon::now()->addHour()->format('YmdHis')
  74. ]);
  75. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  76. //第二次签名
  77. //$result = $app->jssdk->appConfig($result['prepay_id']);
  78. //$config = $app->configForAppPayment($result['prepay_id']);
  79. $config = $app->jssdk->sdkConfig($result['prepay_id']);
  80. return response()->json([
  81. "status" => 0,
  82. "code" => "success",
  83. "msg" => $config,
  84. "out_trade_no" => $out_trade_no
  85. ]);
  86. } else {
  87. return response()->json([
  88. "status" => -1,
  89. "message" => '微信支付签名失败:' . var_export($result, 1)
  90. ]);
  91. }
  92. }
  93. }
  94. }
  95. }