This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Payments.php

179 lines
4.3 KiB
PHP
Raw Normal View History

2015-06-29 12:40:00 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the payments handler.
2016-03-08 23:07:58 +00:00
*
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
2015-06-29 12:40:00 +00:00
namespace Sakura;
use \PayPal\Api\Amount;
use \PayPal\Api\Details;
2015-07-01 00:16:22 +00:00
use \PayPal\Api\Item;
use \PayPal\Api\ItemList;
use \PayPal\Api\Payer;
2015-07-01 00:16:22 +00:00
use \PayPal\Api\Payment;
use \PayPal\Api\PaymentExecution;
use \PayPal\Api\RedirectUrls;
use \PayPal\Api\Transaction;
2015-06-29 12:40:00 +00:00
/**
2016-02-02 21:04:15 +00:00
* Sakura PayPal API wrapper.
2016-03-08 23:07:58 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Kamil Rakowski <admin@krakow.pw>
* @author Julian van de Groep <me@flash.moe>
*/
class Payments
{
2016-02-02 21:04:15 +00:00
/**
* Container for the PayPal API
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var \PayPal\Rest\ApiContext
*/
2015-07-01 00:16:22 +00:00
private static $paypal;
2016-02-02 21:04:15 +00:00
/**
* Initialise the wrapper.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @return bool Always true.
*/
public static function init()
{
2015-07-01 00:16:22 +00:00
// Set PayPal object
try {
self::$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
2015-12-04 14:19:10 +00:00
Config::get('paypal_client_id'),
Config::get('paypal_secret')
2015-07-01 00:16:22 +00:00
)
);
} catch (\Exception $e) {
2015-07-01 00:16:22 +00:00
return false;
}
2016-02-02 21:04:15 +00:00
// Set the configuration
2016-01-11 00:05:50 +00:00
self::$paypal->setConfig([
'mode' => Config::get('paypal_mode'),
]);
2015-07-01 00:16:22 +00:00
return true;
}
2016-02-02 21:04:15 +00:00
/**
* Create a new transaction.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param float $total The total amount of money.
* @param string $itemName The name of the item being purchased.
* @param string $transDescription The description of the item.
* @param string $returnUrl The URL that PayPal will redirect back to.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @return bool|null|string If successful; the PayPal approval link.
*/
public static function createTransaction($total, $itemName, $transDescription, $returnUrl)
{
2015-07-01 00:16:22 +00:00
// Create the payer object
$payer = new Payer();
// Set the method
$payer->setPaymentMethod('paypal');
// Create the item
$item = new Item();
// Set the item details
$item->setName($itemName)
->setCurrency('EUR')
->setQuantity(1)
->setPrice($total);
2015-07-01 00:16:22 +00:00
// Create itemlist
$list = new ItemList();
// Add the items
$list->setItems([$item]);
// Create details
$details = new Details();
// Set details
$details->setSubtotal($total);
// Create amount
$amount = new Amount();
// Set amount data
$amount->setCurrency('EUR')
->setTotal($total)
->setDetails($details);
2015-07-01 00:16:22 +00:00
// Create transaction
$trans = new Transaction();
2015-09-14 21:41:43 +00:00
// Set transaction data
$trans->setAmount($amount)
->setItemList($list)
->setDescription($transDescription)
->setInvoiceNumber(uniqid());
2015-07-01 00:16:22 +00:00
// Create redirect url object
$redir = new RedirectUrls();
// Set redirect url data
$redir->setReturnUrl($returnUrl . '?mode=finish&success=true')
->setCancelUrl($returnUrl . '?mode=finish&success=false');
2015-07-01 00:16:22 +00:00
// Create payment object
$payment = new Payment();
// Set payment data (finally)
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redir)
->setTransactions([$trans]);
2015-07-01 00:16:22 +00:00
// Try to create payment
try {
$payment->create(self::$paypal);
} catch (\Exception $ex) {
2015-07-01 00:16:22 +00:00
return false;
}
// Return the approval link if everything is gucci
return $payment->getApprovalLink();
}
2016-02-02 21:04:15 +00:00
/**
* Complete the PayPal transaction.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param string $paymentId ID of the payment.
* @param string $payerId ID of the payer.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @return bool Success indicator.
*/
public static function completeTransaction($paymentId, $payerId)
{
2015-07-01 00:16:22 +00:00
// Attempt to get the payment
$payment = Payment::get($paymentId, self::$paypal);
// Create payment execution object
$execute = new PaymentExecution();
// Set the payer ID
$execute->setPayerId($payerId);
// Attempt to charge the fucker
try {
$payment->execute($execute, self::$paypal);
} catch (\Exception $ex) {
2015-07-01 00:16:22 +00:00
return false;
}
// If everything was cute return true
return true;
}
2015-06-29 12:40:00 +00:00
}