Example #1
0
 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;
 }
Example #2
0
 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;
     }
 }
$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
// Create a payment by posting to the APIService
// using a valid apiContext
// The return object contains the status and the
// url to which the buyer must be redirected to
// for payment approval
try {
    $payment->create($apiContext);