将表单值传递给控制器​​变量

时间:2018-06-29 06:22:13

标签: javascript php laravel paypal

正在使用具有多个支付网关的半复杂捐赠系统,并且需要以我的PayPal形式获取一些额外的值才能存储在数据库中。无法将值从主窗体传递到我的PayPal控制器并存储在我的数据库中。

以下是其工作方式的细目分类。

    1. 主表格在到达付款网关之前会形成可选字段

      <input type="text" id="people" name="people" value="">
      
      <select onchange="tripbehalf(this);" id="tripbehalf">
          <option selected="true" value="">Optional Info</option>
          <option value="x">This is for X.</option>
          <option value="y">This is for Y</option>
          <option value="z">Both Options</option>
      </select>
      
    1. 获取值并发送到PayPal表单的脚本

      <script>
      function updateExtraFields(fieldName, selector) {
          $('form input[name="' + fieldName + '"]').val($(selector).val());
      }
      
      $('#tripbehalf').on('change', function() {
          updateExtraFields('d_type', '#tripbehalf');
      });
      
      $('#people').on('change', function() {
          updateExtraFields('people', '#people');
      });
      
      </script>
      
    1. PayPal表单获取值并发送给PayPal控制器

      <form action="{{ route('xxx.store') }}" method="post" target="_top">
          <input type="hidden" name="amount" value=""/>
          <input type="hidden" name="d_type" value=""/>
          <input type="hidden" name="people" value=""/>
              <button type="submit">PayPal</button>
      </form>
      
    1. 将上述值传递给PayPal控制器以用于存储数据

      public function store(Request $request) {
      $item = (new Item)
          ->setName('Poverty Resolutions Donation')
          ->setCurrency('USD')
          ->setQuantity(1)
          ->setPrice($request->amount);
          $d_type=$request->d_type;
          $people=$request->people;
      
          dd($request->all()); // <--THIS RETURNS THE D_TYPE & PEOPLE CORRECT VALUE
      
      return $this->paypal->setItems([$item])->charge($request->amount);
      }
      
  • 4.5我怎么从4变成5

    public function success(Request $request) {
        if (empty($request->PayerID) || empty($request->token)) {
            // TODO: Properly redirect with an error message
            dd('No payer ID or no token');
    }
    
    $apiContext = $this->paypal->apiContext();
    
    $execution = (new PaymentExecution)->setPayerId($request->PayerID);
    $payment = Payment::get($request->paymentId, $apiContext)->execute($execution, $apiContext);
    
    if ($payment->getState() !== 'approved') {
        // TODO: Properly redirect with an error message
        dd('Payment failed');
    }
    
    $payerInfo = $payment->getPayer()->getPayerInfo();
    
    $paypalTransaction = PayPalTransaction::create([
        'payer_id' => $payerInfo->getPayerId(),
        'payment_id' => $payment->getId(),
    ]);
    
    1. 需要在此处传递D_TYPE和PEOPLE值并存储在数据库中。

      $paypalTransaction->transactions()->create([
      'user_id' => auth()->id(),
      'customer_email' => $payerInfo->getEmail(),
      'amount' => $payment->getTransactions()[0]->getAmount()->getTotal() * 100,
      'recurring' => false,
      'gift_trans_fee' => $request->gift_trans_fee === 'true',
      'd_type' => $request->d_type,
      'people' => $request->people,
      ]);
      

需要帮助弄清楚如何将d_typepeople的值放入第5部分

在第5部分中,我们尝试了'd_type' => $d_type'people' => $people,但遇到了错误:Undefined variable: d_type,即使我在第4部分中获取了值

0 个答案:

没有答案