buy.blade.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. @extends('user.layouts')
  2. @section('css')
  3. <link href="/assets/examples/css/pages/invoice.css" type="text/css" rel="stylesheet">
  4. @endsection
  5. @section('content')
  6. <div class="page-content">
  7. <div class="panel">
  8. <div class="panel-body container-fluid">
  9. <div class="page-invoice-table table-responsive">
  10. <table class="table table-hover text-md-center">
  11. <thead>
  12. <tr>
  13. <th>{{trans('home.service_name')}}</th>
  14. <th>{{trans('home.service_desc')}} </th>
  15. <th>{{trans('home.service_price')}}</th>
  16. <th>{{trans('home.service_quantity')}}</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <td class="text-middle">{{$goods->name}} </td>
  22. <td>{{trans('home.service_days')}}
  23. <strong>{{$goods->type==1? $dataPlusDays:$goods->days}} {{trans('home.day')}}</strong>
  24. <br>
  25. <strong>{{$goods->traffic_label}}</strong> {{trans('home.bandwidth')}}
  26. </td>
  27. <td class="text-middle"> ¥{{$goods->price}} </td>
  28. <td class="text-middle"> x 1</td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
  33. @if($goods->type <= 2)
  34. <div class="row">
  35. <div class="col-lg-3 pl-30">
  36. <div class="input-group">
  37. <input type="text" class="form-control" name="coupon_sn" id="coupon_sn"
  38. placeholder="{{trans('home.coupon')}}"/>
  39. <div class="input-group-btn">
  40. <button type="submit" class="btn btn-info" onclick="redeemCoupon()"><i
  41. class="icon wb-loop" aria-hidden="true"></i> {{trans('home.redeem_coupon')}}
  42. </button>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="col-lg-3 offset-lg-6 text-right">
  47. <p>{{trans('home.service_subtotal_price')}}
  48. <span>¥{{$goods->price}}</span>
  49. </p>
  50. <p class="page-invoice-amount">{{trans('home.service_total_price')}}
  51. <span class="grand-total">¥{{$goods->price}}</span>
  52. </p>
  53. </div>
  54. @endif
  55. <div class="col-md-12 mb-30">
  56. <div class="float-right">
  57. <div class="btn-group btn-group-lg">
  58. @include('user.components.purchase')
  59. @if($goods->type <= 2)
  60. <button class="btn btn-flat" onclick="pay('credit','0')"><img
  61. src="/assets/images/payment/creditpay.svg" height="48px"
  62. alt="{{trans('home.service_pay_button')}}"/></button>
  63. @endif
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. @endsection
  72. @section('script')
  73. <script type="text/javascript">
  74. // 校验优惠券是否可用
  75. function redeemCoupon() {
  76. const coupon_sn = $('#coupon_sn').val();
  77. const goods_price = '{{$goods->price}}';
  78. $.ajax({
  79. type: "POST",
  80. url: "/redeemCoupon",
  81. async: false,
  82. data: {_token: '{{csrf_token()}}', coupon_sn: coupon_sn, price: '{{$goods->price}}'},
  83. dataType: 'json',
  84. success: function (ret) {
  85. $(".input-group-prepend").remove();
  86. if (ret.status === 'success') {
  87. $("#coupon_sn").parent().prepend('<div class="input-group-prepend"><span class="input-group-text bg-green-700"><i class="icon wb-check white" aria-hidden="true"></i></span></div>');
  88. // 根据类型计算折扣后的总金额
  89. let total_price = 0;
  90. if (ret.data.type === 2) {
  91. total_price = goods_price * (1 - ret.data.discount / 10);
  92. $(".page-invoice-amount").parent().prepend('<p>优惠码 - ' + ret.data.name + ' ' + ret.data.discount + '折<br> 优惠 <span>¥ - ' + total_price.toFixed(2) + '</span></p>');
  93. total_price = goods_price - total_price;
  94. } else {
  95. total_price = goods_price - ret.data.amount;
  96. total_price = total_price > 0 ? total_price : 0;
  97. if (ret.data.type === 1) {
  98. $(".page-invoice-amount").parent().prepend('优惠码-' + ret.data.name + ' <span>¥ - ' + ret.data.amount + '</span>');
  99. }
  100. }
  101. // 四舍五入,保留2位小数
  102. $(".grand-total").text('¥' + total_price.toFixed(2));
  103. swal.fire({
  104. title: ret.message,
  105. type: 'success',
  106. timer: 1300,
  107. showConfirmButton: false
  108. });
  109. } else {
  110. $(".grand-total").text("¥" + goods_price);
  111. $("#coupon_sn").parent().prepend('<div class="input-group-prepend"><span class="input-group-text bg-red-700"><i class="icon wb-close white" aria-hidden="true"></i></span></div>');
  112. swal.fire({
  113. title: ret.title,
  114. text: ret.message,
  115. type: 'error'
  116. });
  117. }
  118. }
  119. });
  120. }
  121. // 检查预支付
  122. function pay(method, pay_type) {
  123. // 存在套餐 和 购买类型为套餐时 出现提示
  124. if ('{{$activePlan}}' === '1' && '{{$goods->type}}' === '2') {
  125. swal.fire({
  126. title: '套餐存在冲突',
  127. html: '<p>当前购买套餐将自动设置为 <code>预支付套餐</code><p><ol class="text-left"><li> 预支付套餐会在生效中的套餐失效后自动开通!</li><li> 您可以在支付后手动激活套餐!</li></ol>',
  128. type: 'info',
  129. showCancelButton: true,
  130. cancelButtonText: '返 回',
  131. confirmButtonText: '继 续',
  132. }).then((result) => {
  133. if (result.value) {
  134. contiousPay(method, pay_type);
  135. }
  136. })
  137. } else {
  138. contiousPay(method, pay_type);
  139. }
  140. }
  141. function contiousPay(method, pay_type) {
  142. const goods_id = '{{$goods->id}}';
  143. const coupon_sn = $('#coupon_sn').val();
  144. $.ajax({
  145. type: "POST",
  146. url: "/payment/purchase",
  147. async: false,
  148. data: {
  149. _token: '{{csrf_token()}}',
  150. goods_id: goods_id,
  151. coupon_sn: coupon_sn,
  152. method: method,
  153. pay_type: pay_type
  154. },
  155. dataType: 'json',
  156. success: function (ret) {
  157. if (ret.status === 'success') {
  158. swal.fire({
  159. title: ret.message,
  160. type: 'success',
  161. timer: 1300,
  162. showConfirmButton: false
  163. });
  164. if (method === 'credit') {
  165. swal.fire({title: ret.message, type: 'success', timer: 1000, showConfirmButton: false})
  166. .then(() => window.location.href = '/invoices')
  167. }
  168. if (ret.data) {
  169. window.location.href = '/payment/' + ret.data;
  170. } else if (ret.url) {
  171. window.location.href = ret.url;
  172. }
  173. } else if (ret.status === 'info') {
  174. swal.fire({title: ret.title, text: ret.message, type: 'question'});
  175. } else {
  176. swal.fire({
  177. title: ret.message,
  178. type: 'error'
  179. })
  180. }
  181. },
  182. error: function () {
  183. swal.fire('未知错误', '请开工单通知客服', 'error')
  184. }
  185. });
  186. }
  187. </script>
  188. @endsection