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()); } }
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(); }
$payment->setPayer($payer); $payment->setTransactions(array($transaction)); try { $payment->create($paypal_rest); if ($payment->getState() == 'approved') { $transactions = $payment->getTransactions(); foreach ($transactions as $txn) { $related_resources = $txn->getRelatedResources(); foreach ($related_resources as $related) { $related_sale = $related->getSale(); if ($related_sale) { $_SESSION['transaction_id'] = $related_sale->id; } } } $_SESSION['payment_id'] = $payment->getId(); $_SESSION['payment_created'] = $payment->getCreateTime(); $_SESSION['payment_state'] = $payment->getState(); } else { $_SESSION['payment_state'] = $payment->getState(); $result_data_html = $_SESSION['payment_state']; echo json_encode(array('result' => 'error', 'result_data' => $result_data_html)); exit; } } catch (\PayPal\Exception\PayPalConnectionException $ex) { $PayPalErrors = json_decode($ex->getData()); // Write to transaction log if (isset($config['LogEnabled']) && $config['LogEnabled']) { $log_array = (array) $PayPalErrors; logTransaction($log_array, $config['LogFilePath']); }