/** * @dataProvider validRedirectUrlsProvider */ public function testValidRedirectUrls($return_url, $cancel_url) { $redirectUrls = new RedirectUrls(); $redirectUrls->setReturn_url($return_url); $redirectUrls->setCancel_url($cancel_url); $this->assertEquals($return_url, $redirectUrls->getReturnUrl()); $this->assertEquals($cancel_url, $redirectUrls->getCancelUrl()); }
public static function createNewPayment() { $payer = new Payer(); $payer->setPayment_method("credit_card"); $payer->setFunding_instruments(array(FundingInstrumentTest::createFundingInstrument())); $transaction = new Transaction(); $transaction->setAmount(AmountTest::createAmount()); $transaction->setDescription("This is the payment description."); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturn_url("http://localhost/return"); $redirectUrls->setCancel_url("http://localhost/cancel"); $payment = new Payment(); $payment->setIntent("sale"); $payment->setRedirect_urls($redirectUrls); $payment->setPayer($payer); $payment->setTransactions(array($transaction)); return $payment; }
public function createPayment($orderid, $userid) { if ($orderid > 0) { $orderid = (int) $orderid; $paypalCredentials = $this->getPaypalCredentials(); $apiContext = new ApiContext(new OAuthTokenCredential($paypalCredentials["clientid"], $paypalCredentials["secret"])); $apiContext->setConfig($this->paypalConfig); $payer = new Payer(); $payer->setPayment_method("paypal"); $order = new Orders(); $orderdetail = $order->getOrderDetail($orderid); //Setting up items for the paypal order detail //On future version this will have multiple packages based on cart content //When items were added api were responding abnormal /* $item = new Item(); $item->setName($orderdetail["title"]); $item->setCurrency($orderdetail["currency"]); $item->setQuantity($orderdetail["quantity"]); $item->setPrice($orderdetail["package_price"]); $item->setSku($orderdetail["package_guid"]); $itemList = new ItemList(); $itemList->setItems(array($item)); */ $amount = new Amount(); $amount->setCurrency($orderdetail["currency"]); $amount->setTotal($orderdetail["total"]); $orderDescription = "Qty: " . $orderdetail["quantity"] . " for " . $orderdetail["title"] . " - " . $orderdetail["title_summary"]; $transaction = new Transaction(); $transaction->setDescription($orderDescription); $transaction->setAmount($amount); //$transaction->setItemlist($itemList); $redirectUrls = new RedirectUrls(); $humanorderid = $order->getHumanOrderId($orderid); $redirectUrls->setReturn_url($paypalCredentials["returnurl"] . "&orderid=" . $humanorderid); $redirectUrls->setCancel_url($paypalCredentials["cancelurl"] . "&orderid=" . $humanorderid); $payment = new Payment(); $payment->setIntent("sale"); $payment->setPayer($payer); $payment->setRedirect_urls($redirectUrls); $payment->setTransactions(array($transaction)); try { $payment->create($apiContext); $this->setPaypalInfoOrder($orderid, $payment->getId(), $payment->getCreateTime(), $payment->getUpdateTime(), $payment->getState(), $userid); // Retrieve buyer approval url from the `payment` object. foreach ($payment->getLinks() as $link) { if ($link->getRel() == 'approval_url') { $approvalUrl = $link->getHref(); } } $result = array("status" => true, "approval_url" => $approvalUrl); return $result; } catch (PayPal\Exception\PPConnectionException $ex) { //Logging error $this->log->addError($ex->getMessage() . LOG_LINESEPARATOR . "TRACE: " . $ex->getTraceAsString() . LOG_LINESEPARATOR); $result = array("status" => false, "approval_url" => ""); return $result; } } else { //Logging error $this->log->addWarning("Invalid or null order id." . LOG_LINESEPARATOR); $result = array("status" => false, "approval_url" => ""); return $result; } }
$amount->setTotal("1.00"); // ### 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 = new Transaction(); $transaction->setAmount($amount); $transaction->setDescription("This is the payment description."); // ### Redirect urls // Set the urls that the buyer must be redirected to after // payment approval/ cancellation. $baseUrl = getBaseUrl(); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturn_url("{$baseUrl}/ExecutePayment.php?success=true"); $redirectUrls->setCancel_url("{$baseUrl}/ExecutePayment.php?success=false"); // ### Payment // A Payment Resource; create one using // the above types and intent as 'sale' $payment = new Payment(); $payment->setIntent("sale"); $payment->setPayer($payer); $payment->setRedirect_urls($redirectUrls); $payment->setTransactions(array($transaction)); // ### Api Context // Pass in a `ApiContext` object to authenticate // the call and to send a unique request id // (that ensures idempotency). The SDK generates // a request id if you do not pass one explicitly. $apiContext = new ApiContext($cred, 'Request' . time()); // ### Create Payment