Skip to content

Instantly share code, notes, and snippets.

@hyrsky
Last active July 8, 2017 14:09
Show Gist options
  • Save hyrsky/c32329a630fcf55e95ba886db7cecb05 to your computer and use it in GitHub Desktop.
Save hyrsky/c32329a630fcf55e95ba886db7cecb05 to your computer and use it in GitHub Desktop.
Quickpay v10 test
<?php
require __DIR__ . '/vendor/autoload.php';
use QuickPay\QuickPay as Client;
class QuickPayBase
{
/**
* Quickpay client
*/
protected $client;
/**
* Quickpay subscription model
*/
protected $object;
/**
* Construct QuickPay object
*
* @param Client $client QuickPay API client
* @param $response QuickPay API response containing the object
*
*/
public function __construct(Client $client, $response, $status_code = NULL) {
$this->client = $client;
$className = get_class($this);
echo "Creating {$className}\n";
if (is_null($status_code)) {
$status_code = 200;
// If no status code accept any success
if (floor($response->httpStatus() / 100) == 2) {
$status_code = $response->httpStatus();
}
}
$object = $this->handleResponse($response, $status_code);
if (property_exists($object, 'type') && $object->type == $className) {
// Save response object
$this->object = $response->asObject();
} else if (property_exists($object, 'type')) {
throw new InvalidArgumentException("Attempting to construct " . $className . " from type {$object->type}");
} else {
throw new InvalidArgumentException("Attempting to construct " . $className . " from invalid type");
}
echo "Created {$className}: {$this->getId()}\n";
}
/**
* Checks response status code and returns response as an object.
*
* @param $response Quickpay API response
* @param int $status_code Excepted status code
*
* @throws Exception If response status code is not equal to status_code
*
* @return stdClass deserialized response
*/
protected function handleResponse($response, $status_code) {
if ($response->httpStatus() != $status_code) {
$errors = $response->asObject();
$errors = json_encode($errors->errors);
echo "Status code: {$response->httpStatus()}\n";
echo "Errors: {$errors}\n";
throw new Exception($response->asObject()->message);
}
return $response->asObject();
}
/**
* Get subscription id
*
* @return string object id
*
*/
public function getId() {
return $this->object->id;
}
/**
* Deserialize object to json
*
* @return string Json string
*
*/
public function to_json() {
return json_encode($this->object, JSON_PRETTY_PRINT);
}
}
class Payment extends QuickPayBase
{
/**
* Create payment
*
* @param Client $client QuickPay API client
* @param string $order_id Unique order number
* @param string $currency Currency
* @param string $description Subscription description
*
* @return Subscription
*/
public static function create(Client $client, $order_id, $currency, $description) {
$response = $client->request->post('/payments', [
"order_id" => $order_id,
"currency" => $currency,
"description" => $description
]);
return new self($client, $response);
}
/**
* Authorize payment with token
*
* @param string $token Token from authorized payment method
* @param int $amount Amount
*/
public function authorize($token, $amount) {
$response = $this->client->request->post("/payments/{$this->getId()}/authorize", [
"card" => ["token" => $token],
"amount" => $amount
]);
$this->handleResponse($response, 202);
echo "Authorized payment {$this->getId()}\n";
}
}
class Subscription extends QuickPayBase
{
/**
* Create subscription
*
* @param Client $client QuickPay API client
* @param string $order_id Unique order number
* @param string $currency Currency
* @param string $description Subscription description
*
* @return Subscription
*/
public static function create(Client $client, $order_id, $currency, $description, $variables) {
$json_variables = json_encode($variables);
echo "Create Subscription ({$order_id}, {$currency}, {$description}, {$json_variables})\n";
$response = $client->request->post('/subscriptions', [
"order_id" => $order_id,
"currency" => $currency,
"description" => $description
]);
return new self($client, $response);
}
/**
* Get subscription
*
* @param Client $client QuickPay API client
* @param string $id Subscription id
*
* @return Subscription
*/
public static function get(Client $client, $id) {
return new self($client, $client->request->get("/subscriptions/{$id}"));
}
/**
* Authorize a subscription with a card token
*
* @param string $token Token from authorized payment method
* @param int $amount Subscription amount
*/
public function authorize($token, $amount) {
$response = $this->client->request->post("/subscriptions/{$this->getId()}/authorize", [
"card" => ["token" => $token],
"amount" => $amount
]);
echo "Validate authorize response\n";
$this->handleResponse($response, 202);
echo "Authorized subscription {$this->getId()}\n";
}
/**
* Create payment for authorized subscription
*
*/
public function createRecurringPayment($amount, $order_id) {
$response = $this->client->request->post("/subscriptions/{$this->getId()}/recurring", [
"amount" => $amount,
"order_id" => $order_id
]);
$payment = new Payment($this->client, $response);
echo "Created payment {$payment->getId()} for subscription {$this->getId()}\n";
return $payment;
}
}
class PaymentController
{
/**
* Quickpay client
*/
protected $client;
/**
* Quickpay api key
*/
protected $apikey;
/**
* Quickpay currency
*/
protected $currency;
public function __construct($apikey, $currency = 'EUR') {
$this->apikey = $apikey;
$this->currency = $currency;
$this->client = new Client(":{$apikey}");
}
/**
* Return unique id
*
* Quickpay expects order ids to be unique so this function should not return duplicates
*/
protected function generateId() {
return uniqid();
}
public function pay(array $request) {
$amount = 1000;
$token = $request['card_token'];
try {
echo "amount: {$amount}\ntoken: {$token}\n";
$payment = Payment::create($this->client, $this->generateId(), $this->currency);
$payment->authorize($token, $amount);
echo "\n\n{$payment->to_json()}";
} catch(Exception $err) {
echo "Message: {$err->getMessage()}";
}
}
public function subscribe(array $request) {
$amount = 1000;
$token = $request['card_token'];
try {
echo "amount: {$amount}\ntoken: {$token}\n";
$variables = new stdClass();
$variables->name = $request['name'];
$variables->email = $request['email'];
$variables->phone = $request['phone'];
$variables->frequency = 'Monthly';
$variables->amount = $amount;
$order_id = $this->generateId();
echo "Subscription order id: {$order_id}\n";
$subscription = Subscription::create(
$this->client,
$order_id,
$this->currency,
"Donation",
$variables
);
$subscription->authorize($token, $amount);
$subscription->createRecurringPayment($amount, $this->generateId());
echo "\n\n{$subscription->to_json()}";
} catch(Exception $err) {
echo "Message: {$err->getMessage()}";
}
}
}
$payments = new PaymentController("xxx-api-key-xxx");
echo "<pre>";
$payments->subscribe($_GET);
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment