Пример #1
0
 public function buyItem(Request $request)
 {
     $subTotal = 0;
     $data = $request->input('products');
     Session::put('product_info', $data);
     $payer = Paypalpayment::payer();
     $payer->setPaymentMethod("paypal");
     $products = Products::whereIn('id', array_keys($data))->get();
     foreach ($products as $product) {
         $quantity = $data[$product->id]['quantity'];
         $quantity = $product->quantity < $quantity ? $product->quantity : $quantity;
         $product_list[] = Paypalpayment::item()->setName($product->name)->setDescription($product->id)->setCurrency('USD')->setQuantity($quantity)->setPrice($product->price);
         $subTotal += $product->price * $quantity;
     }
     $itemList = Paypalpayment::itemList();
     $itemList->setItems($product_list);
     $details = Paypalpayment::details();
     $details->setShipping("100")->setTax("10")->setSubtotal($subTotal);
     //Payment Amount
     $amount = Paypalpayment::amount();
     $amount->setCurrency("USD")->setTotal($subTotal + 110)->setDetails($details);
     // ### Transaction
     // A transaction defines the contract of a
     // payment - what is the payment for and who
     // is fulfilling it. Transaction is created with
     // a `Payee` and `Amount` types
     $transaction = Paypalpayment::transaction();
     $transaction->setAmount($amount)->setItemList($itemList)->setDescription("Payment description")->setInvoiceNumber(uniqid());
     // ### Payment
     // A Payment Resource; create one using
     // the above types and intent as 'sale'
     $baseUrl = url();
     $redirectUrls = Paypalpayment::redirectUrls();
     $redirectUrls->setReturnUrl("{$baseUrl}/paypal/ok")->setCancelUrl("{$baseUrl}/paypal/error");
     $payment = Paypalpayment::payment();
     $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions(array($transaction));
     try {
         // ### Create Payment
         // Create a payment by posting to the APIService
         // using a valid ApiContext
         // The return object contains the status;
         $payment->create($this->_apiContext);
     } catch (\PPConnectionException $ex) {
         return "Exception: " . $ex->getMessage() . PHP_EOL;
         exit(1);
     }
     //dd($payment);
     return redirect($payment->getApprovalLink());
 }