public function submit($ordernumber) { $order = Order::where('ordernumber', "=", $ordernumber)->first(); if ($order->payment_method == self::PAYMENT_BANK) { Mail::send('emails.moneyorder', ['order' => $order], function ($message) use($order) { $message->from(self::$shopmail, 'LEAF Music'); $message->subject('LEAF Music Order ' . $order->ordernumber . ' - waiting for payment'); $message->to($order->email)->bcc(self::$shopmail)->replyTo(self::$shopmail); }); Mail::send('emails.kaatmail', ['order' => $order], function ($message) use($order) { $message->from(self::$shopmail, 'LEAF Music'); $message->subject('LEAF Music Order ' . $order->ordernumber . ' - waiting for payment'); $message->to(self::$shopmail); }); $order->status = Order::STATUS_WAITING; $order->save(); return Redirect::action('WelcomeController@result', [$order->ordernumber]); } else { // setup PayPal api context $paypal_conf = \Config::get('paypal'); $api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret'])); $api_context->setConfig($paypal_conf['settings']); $payer = new Payer(); $payer->setPaymentMethod("paypal"); $itemarray = []; $details = new Details(); $subtotal = 0; foreach ($order->orderitems as $orderitem) { //http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html //http://learninglaravel.net/integrate-paypal-sdk-into-laravel-4-laravel-5/link if ($orderitem->item_id != Item::SHIPPING) { $item = new \PayPal\Api\Item(); $item->setName($orderitem->item->title)->setCurrency('EUR')->setQuantity($orderitem->amount)->setSku($orderitem->item_id)->setPrice($orderitem->itemprice); $itemarray[] = $item; $subtotal += $orderitem->amount * $orderitem->itemprice; } else { $details->setShipping($orderitem->itemprice)->setTax(0); } } $details->setSubtotal($subtotal); $itemlist = new ItemList(); $itemlist->setItems($itemarray); $transaction = new Transaction(); $amount = new Amount(); $amount->setCurrency("EUR")->setTotal($order->totalamount)->setDetails($details); $transaction->setAmount($amount)->setItemList($itemlist)->setDescription("Payment description")->setInvoiceNumber($order->order_number); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl(action('WelcomeController@paypalReturn', [self::PAYPAL_OK, $order->ordernumber]))->setCancelUrl(action('WelcomeController@paypalReturn', [self::PAYPAL_PROBLEM, $order->ordernumber])); $payment = new Payment(); $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions(array($transaction)); try { $payment->create($api_context); } catch (\PayPal\Exception\PayPalConnectionException $pce) { // Don't spit out errors or use "exit" like this in production code return view('problem', ["order" => $order]); } $approvalUrl = $payment->getApprovalLink(); return Redirect::to($approvalUrl); } }
protected function initializePay($package, $user) { $payer = new \PayPal\Api\Payer(); $payer->setPaymentMethod('paypal'); $total = $package->price + $package->tax; $item = new \PayPal\Api\Item(); $item->setName($package->name)->setCurrency('USD')->setQuantity(1)->setPrice($package->price); $itemList = new \PayPal\Api\ItemList(); $itemList->setItems([$item]); $details = new \PayPal\Api\Details(); $details->setTax($package->tax)->setSubtotal($package->price); $amount = new \PayPal\Api\Amount(); $amount->setCurrency("USD")->setTotal($total)->setDetails($details); $transaction = new \PayPal\Api\Transaction(); $transaction->setAmount($amount)->setItemList($itemList)->setDescription($package->name)->setInvoiceNumber(uniqid()); $baseUrl = "http://trafficmonitor.ca/"; $redirectUrls = new \PayPal\Api\RedirectUrls(); $redirectUrls->setReturnUrl($baseUrl . "plan/success")->setCancelUrl($baseUrl . "packages"); $payment = new \PayPal\Api\Payment(); $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions(array($transaction)); try { $payment->create($this->paypal()); $transaction = new Transaction(array("user_id" => $user->id, "property" => "package", "property_id" => $package->id, "payment_id" => $payment->getId(), "amount" => $total)); $transaction->save(); } catch (Exception $e) { die($e); } return $approvalUrl = $payment->getApprovalLink(); }
private static function _setDescription(PayPal\Api\Item &$item, $description = '') { $description = !empty($description) ? $description : 1; $item->setDescription($description); }
public function credit_card() { $card = new \PayPal\Api\CreditCard(); $card->setType($this->card_type)->setNumber($this->card_number)->setExpireMonth($this->card_month)->setExpireYear($this->card_year)->setCvv2($this->card_cvv)->setFirstName($this->card_fname)->setLastName($this->card_lname); $fi = new \PayPal\Api\FundingInstrument(); $fi->setCreditCard($card); $payer = new \PayPal\Api\Payer(); $payer->setPaymentMethod('credit_card')->setFundingInstruments(array($fi)); $items = array(); foreach ($this->items as $item) { $item2 = new \PayPal\Api\Item(); $item2->setName($item[0])->setDescription($item[1])->setCurrency(CURRENCY)->setQuantity($item[2])->setPrice($item[3]); $items[] = $item2; } $itemList = new \PayPal\Api\ItemList(); $itemList->setItems($items); $amount = new \PayPal\Api\Amount(); $amount->setCurrency(CURRENCY)->setTotal($this->total); $transaction = new \PayPal\Api\Transaction(); $transaction->setAmount($amount)->setItemList($itemList)->setDescription($this->description); $payment = new \PayPal\Api\Payment(); $payment->setIntent($this->intent)->setPayer($payer)->setTransactions(array($transaction)); try { $payment->create($this->apiContext); return array('id' => $payment->getId(), 'total' => $this->total, 'items' => $this->items, 'details' => $this->description, 'state' => $payment->getstate()); } catch (Exception $e) { throw new \Exception('PayPal error: ' . $e->getMessage()); } }