Laravel - paypal最多超过30秒

时间:2017-12-16 09:37:22

标签: paypal laravel-5.5 timeoutexception

支付宝在laravel中保持最长30秒超时,有时它可以顺利进行但是当购物车产品增加时,它将保持返回此错误,有时错误是创建付款时,有时是执行付款,非常不稳定,如何我能解决这个问题吗?我试过将超时设置扩展到300,但它仍然会返回超时错误。

这是我的创建付款代码:

$item_array = [];
      $shipping = $request->shipping;
      $tax = $request->tax;
      $tax_amount = 0;
      $subtotal = $total = 0;
      $currency = "MYR";
      $payer = new Payer();
      $payer->setPaymentMethod("paypal");
      $credential = new OAuthTokenCredential(env('PAYPAL_SANDBOX_CLIENT_ID'), env('PAYPAL_SANDBOX_SECRET'));
      $apiContext = new ApiContext($credential, null);
      $order = Order::findOrFail($id);
      // change order product status
      foreach ($order->orderProduct as $order_product_key => $order_product) {
        // get variant
        $variant_title_array = array();
        if (count($order_product->variantValue) > 0) {
          foreach ($order_product->variantValue->variantOption as $variant_option) {
            array_push($variant_title_array, $variant_option->name);
          }
        }

        $variant= "";
        if (count($variant_title_array) > 0) {
          $variant = " ( ".implode(', ', $variant_title_array)." )";
        }

        // calculate product price and promotion price
        $product_price = $compare_price = "";
        $product_promotion = get_cart_product_promotion ($order_product, $order);

        $product_price = get_product_price_in_cart ($order_product);

        // if promotion exits
        if (count($product_promotion) != 0) {
          $compare_price = get_product_price_in_cart ($order_product);

          $product_price = calculate_product_price_after_promotion ($compare_price, $product_promotion);
        }

        $item = new Item();
        $item->setName($order_product->product->title . $variant)
            ->setCurrency($currency)
            ->setQuantity($order_product->quantity)
            ->setSku(get_product_sku_in_cart ($order_product)) // Similar to `item_number` in Classic API
            ->setPrice((float) str_replace('RM ','', $product_price));
        $item_array[$order_product_key] = $item;
        $subtotal += (float) str_replace('RM ','', $product_price) * $order_product->quantity;
      }

      // calculate tax
      $tax_amount = ($subtotal * $tax) / 100;


      $itemList = new ItemList();
      $itemList->setItems($item_array);

      $shipping_address = new ShippingAddress();

      $shipping_address->setCity('City')
        ->setCountryCode('AR')
        ->setPostalCode('200')
        ->setLine1('Adress Line1')
        ->setState('State')
        ->setRecipientName('Recipient Name');

      $details = new Details();
      $details->setShipping($shipping)
          ->setTax($tax_amount)
          ->setSubtotal($subtotal);

      $total = $subtotal + $shipping + $tax_amount;
      $amount = new Amount();
      $amount->setCurrency($currency)
          ->setTotal($total)
          ->setDetails($details);

      $transaction = new Transaction();
      $transaction->setAmount($amount)
          ->setItemList($itemList)
          ->setDescription("Payment description")
          ->setInvoiceNumber(uniqid());

      $baseUrl = url('/');
      $redirectUrls = new RedirectUrls();
      $redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
          ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");

      $payment = new Payment();
      $payment->setIntent("sale")
          ->setPayer($payer)
          ->setRedirectUrls($redirectUrls)
          ->setTransactions(array($transaction));

      $request = clone $payment;

      try {
          $payment->create($apiContext);
      } catch (PayPalConnectionException $ex) {
          echo $ex->getCode(); // Prints the Error Code
          echo $ex->getData(); // Prints the detailed error message
          die($ex);
      } catch (Exception $ex) {
          die($ex);
      }

      $approvalUrl = $payment->getApprovalLink();

      $response = array(
        'paymentID' => $payment->id
      );

      return json_encode($response);

这是我的执行付款代码:

$shipping = $request->shipping;
      $tax = $request->tax;
      $tax_amount = 0;
      $subtotal = $total = 0;
      $currency = "MYR";

      $credential = new OAuthTokenCredential(env('PAYPAL_SANDBOX_CLIENT_ID'), env('PAYPAL_SANDBOX_SECRET'));
      $apiContext = new ApiContext($credential, null);
      $paymentId = $request->paymentID;
      $payment = Payment::get($paymentId, $apiContext);

      $transaction = new Transaction();
      $amount = new Amount();
      $details = new Details();

      $order = Order::findOrFail($id);

      // store order address
      $order_address = OrderAddress::firstOrCreate(
        [
          'order_id' => $order->id
        ],
        [
          'name' => $request->shipping_name ,
          'phone' => $request->shipping_phone ,
          'address1' => $request->shipping_address1 ,
          'address2' => $request->shipping_address2 ,
          'postcode' => $request->shipping_postcode ,
          'state' => $request->shipping_state ,
          'country' => $request->shipping_country ,
          'city' => $request->shipping_city ,
          'order_id' => $order->id
        ]
      );

      // update address if exist
      $order_address->update([
        'name' => $request->shipping_name ,
        'phone' => $request->shipping_phone ,
        'address1' => $request->shipping_address1 ,
        'address2' => $request->shipping_address2 ,
        'postcode' => $request->shipping_postcode ,
        'state' => $request->shipping_state ,
        'country' => $request->shipping_country ,
        'order_id' => $order->id
      ]);

      // change order product status
      foreach ($order->orderProduct as $order_product) {

        // calculate product price and promotion price
        $product_price = $compare_price = "";
        $product_promotion = get_cart_product_promotion ($order_product, $order);

        $product_price = get_product_price_in_cart ($order_product);

        // if promotion exits
        if (count($product_promotion) != 0) {
          $compare_price = get_product_price_in_cart ($order_product);

          $product_price = calculate_product_price_after_promotion ($compare_price, $product_promotion);
        }

        $product_price = (float) str_replace("RM ","",$product_price);

        // add promotion to order product if promotion exist
        if ($compare_price != "") {
          $order_product->order_promotion_id = $order->orderPromotion->promotion_id;
        }

        $order_product->price = $product_price;
        $order_product->status = 2;

        $subtotal += $product_price * $order_product->quantity;

        $order_product->save();
      }

      // calculate tax
      $tax_amount = ($subtotal * $tax) / 100;

      // change cart detail
      $current_date_time = Carbon::now();
      $order->update([
        'subtotal' => $subtotal,
        'status' => 2,
        'submited_on' => $current_date_time,
        'payment_method' => "paypal"
      ]);

      // update shipping fees
      if ($shipping > 0) {
        $order->shipping_fees = $shipping;
      }

      // update tax
      if ($tax > 0) {
        $order->tax = ($subtotal * $tax) / 100;
      }

      $order->save();


      // paypal settings
      $total = $subtotal + $shipping + $tax_amount;

      $details->setShipping($shipping)
          ->setTax($tax_amount)
          ->setSubtotal($subtotal);
      $amount->setCurrency($currency);
      $amount->setTotal($total);
      $amount->setDetails($details);
      $transaction->setAmount($amount);
      // Add the above transaction object inside our Execution object.
      $execution->addTransaction($transaction);

      try {

          $result = $payment->execute($execution, $apiContext);

          try {
              $payment = Payment::get($paymentId, $apiContext);
          } catch (Exception $ex) {

              exit(1);
          }
      } catch (Exception $ex) {

          exit(1);
      }


      echo "success";

0 个答案:

没有答案