Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
umarsheikh13 committed Nov 16, 2015
0 parents commit fea48d2
Show file tree
Hide file tree
Showing 37 changed files with 1,796 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
THE EXTENSION IS PROVIDED IN THE HOPE THAT IT WILL BE USEFUL,
BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW THE AUTHOR WILL
BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS
OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED
BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF THE AUTHOR HAS BEEN ADVISED
OF THE POSSIBILITY OF SUCH DAMAGES.

Attribution-Noncommercial-No Derivative Works 3.0 Unported
http://creativecommons.org/licenses/by-nc-nd/3.0/
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# WooGiving

This is an experimental plugin which adds a JustGiving payment gateway to WooCommerce.

## Installation

Download the master zip, extract it and upload it to your plugins directory. Once you active the plugin go to WooCommerce > Checkout > JustGiving to set it up.

## Gift Aid (UK only)

If you're accepting donations in exchange for products/services gift aid cannot be claimed. Unfortunately you can't prevent your donors from claiming gift aid so I would recommend using this plugin without providing anything in exchange for the donations.

## License

THE EXTENSION IS PROVIDED IN THE HOPE THAT IT WILL BE USEFUL,
BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW THE AUTHOR WILL
BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS
OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED
BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF THE AUTHOR HAS BEEN ADVISED
OF THE POSSIBILITY OF SUCH DAMAGES.

Attribution-Noncommercial-No Derivative Works 3.0 Unported
http://creativecommons.org/licenses/by-nc-nd/3.0/
111 changes: 111 additions & 0 deletions class.woogiving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

// Create WooGiving payment gateway class

class WooGiving extends WC_Payment_Gateway {

// Configure payment gateway

function __construct() {

// Configuration

$this->id = 'woogiving';
$this->method_title = __( 'JustGiving', 'woogiving' );
$this->method_description = __( 'Adds a JustGiving payment gateway to WooCommerce.', 'woogiving' );
$this->title = __( 'JustGiving', 'woogiving' );
$this->icon = plugin_dir_url( __FILE__ ) . 'img/jg-logo.png';
$this->has_fields = false;

// Get settings and turn into variables

$this->init_form_fields();
$this->init_settings();

foreach ( $this->settings as $setting_key => $value ) {
$this->$setting_key = $value;
}

// Save settings

if ( is_admin() ) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}

}

// Build settings for payment gateway

public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woogiving' ),
'label' => __( 'Enable JustGiving', 'woogiving' ),
'type' => 'checkbox',
'default' => 'no'
),
'username' => array(
'title' => __( 'JustGiving Fundraising Page Username', 'woogiving' ),
'type' => 'text',
'desc_tip' => __( 'The justgiving page URL username that you would like your users to donate to. For example https://www.justgiving.com/thisistheusername.', 'woogiving' )
),
'app_id' => array(
'title' => __( 'App ID', 'woogiving' ),
'type' => 'text',
'desc_tip' => __( 'Signup for an JustGiving Developer account here: http://pages.justgiving.com/developer', 'woogiving' ),
),
'api_login' => array(
'title' => __( 'JustGiving Developer Username', 'woogiving' ),
'type' => 'text'
),
'api_password' => array(
'title' => __( 'JustGiving Developer Password', 'woogiving' ),
'type' => 'password'
),
'description' => array(
'title' => __( 'Description', 'woogiving' ),
'type' => 'textarea',
'desc_tip' => __( 'The payment description on the checkout page.', 'woogiving' ),
'default' => __( 'Pay securely via JustGiving.', 'woogiving' )
),
);
}

// Redirect user to JustGiving

public function process_payment( $order_id ) {

global $woocommerce;

// Get customer order

$customer_order = new WC_Order( $order_id );

// Generate URL

$justgiving_qstr = array(
'amount' => $customer_order->order_total,
'reference' => woogiving_create_ref( $order_id ),
'currency' => get_woocommerce_currency(),
'exitUrl' => plugin_dir_url( __FILE__ ) . 'process.php?wg_action=process&wg_order_id=' . str_replace( '#', '', $customer_order->get_order_number() ) . '&jg_donation_id=JUSTGIVING-DONATION-ID'
);
$justgiving_url = 'https://www.justgiving.com/'.rawurlencode($this->username).'/4w350m3/donate/?' . http_build_query( $justgiving_qstr );

// Redirect to JustGiving

return array(
'result' => 'success',
'redirect' => $justgiving_url,
);

}

// Validate fields

public function validate_fields() {
return true;
}

}

?>
Binary file added img/jg-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions inc/ApiClients/AccountApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
include_once 'ClientBase.php';
include_once 'Http/CurlWrapper.php';

class AccountApi extends ClientBase
{
public $Parent;
public $curlWrapper;

public function __construct($justGivingApi)
{
$this->Parent = $justGivingApi;
$this->curlWrapper = new CurlWrapper();
}

public function Create($createAccountRequest)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/account";
$url = $this->BuildUrl($locationFormat);
$payload = json_encode($createAccountRequest);
$json = $this->curlWrapper->Put($url, $this->BuildAuthenticationValue(), $payload);
return json_decode($json);
}

public function ListAllPages($email)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/account/" . $email . "/pages";
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}

public function IsEmailRegistered($email)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/account/" . $email;
$url = $this->BuildUrl($locationFormat);
$httpInfo = $this->curlWrapper->Head($url, $this->BuildAuthenticationValue());

if($httpInfo['http_code'] == 200)
{
return true;
}
else if($httpInfo['http_code'] == 404)
{
return false;
}
else
{
throw new Exception('IsEmailRegistered returned a status code it wasn\'t expecting. Returned ' . $httpInfo['http_code']);
}
}

public function RequestPasswordReminder($email)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/account/" . $email . "/requestpasswordreminder";
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}

public function IsValid($validateAccountRequest){
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/account/validate";
$url = $this->BuildUrl($locationFormat);
$payload = json_encode($validateAccountRequest);
$json = $this->curlWrapper->PostAndGetResponse($url, $this->BuildAuthenticationValue(), $payload);
return json_decode($json);
}
}
23 changes: 23 additions & 0 deletions inc/ApiClients/CharityApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
include_once 'ClientBase.php';
include_once 'Http/CurlWrapper.php';

class CharityApi extends ClientBase
{
public $Parent;
public $curlWrapper;

public function __construct($justGivingApi)
{
$this->Parent = $justGivingApi;
$this->curlWrapper = new CurlWrapper();
}

public function Retrieve($charityId)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/charity/" . $charityId;
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}
}
35 changes: 35 additions & 0 deletions inc/ApiClients/ClientBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

class ClientBase
{
public $debug;

public function __construct()
{
$this->debug = false;
}

public function BuildUrl($locationFormat)
{
$url = $locationFormat;
$url = str_replace("{apiKey}", $this->Parent->ApiKey, $url);
$url = str_replace("{apiVersion}", $this->Parent->ApiVersion, $url);
return $url;
}

public function BuildAuthenticationValue()
{
if($this->Parent->Username != null && $this->Parent->Username != "")
{
$stringForEnc = $this->Parent->Username.":".$this->Parent->Password;
return base64_encode($stringForEnc);
}

return "";
}

public function WriteLine($string)
{
echo $string . "<br/>";
}
}
39 changes: 39 additions & 0 deletions inc/ApiClients/DonationApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
include_once 'ClientBase.php';
include_once 'Http/CurlWrapper.php';

class DonationApi extends ClientBase
{
public $Parent;
public $curlWrapper;

public function __construct($justGivingApi)
{
$this->Parent = $justGivingApi;
$this->curlWrapper = new CurlWrapper();
}

public function Retrieve($donationId)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/donation/" . $donationId;
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}

public function RetrieveRef($donationId)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/donation/ref/" . $donationId;
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}

public function RetrieveStatus($donationId)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/donation/" . $donationId . "/status";
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}
}
40 changes: 40 additions & 0 deletions inc/ApiClients/EventApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
include_once 'ClientBase.php';
include_once 'Http/CurlWrapper.php';

class EventApi extends ClientBase
{
public $Parent;
public $curlWrapper;

public function __construct($justGivingApi)
{
$this->Parent = $justGivingApi;
$this->curlWrapper = new CurlWrapper();
}

public function Create($event)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/event";
$url = $this->BuildUrl($locationFormat);
$payload = json_encode($event);
$json = $this->curlWrapper->Post($url, $this->BuildAuthenticationValue(), $payload);
return json_decode($json);
}

public function Retrieve($eventId)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/event/" . $eventId;
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}

public function RetrievePages($eventId, $pageSize=50, $pageNumber=1)
{
$locationFormat = $this->Parent->RootDomain . "{apiKey}/v{apiVersion}/event/" . $eventId . "/pages?PageSize=".$pageSize."&PageNum=".$pageNumber;
$url = $this->BuildUrl($locationFormat);
$json = $this->curlWrapper->Get($url, $this->BuildAuthenticationValue());
return json_decode($json);
}
}
Loading

0 comments on commit fea48d2

Please sign in to comment.