Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First Commit

  • Loading branch information...
commit 0feb2a258a572e7cc47d497475821062b1c7854a 0 parents
@omarabid authored
Showing with 571 additions and 0 deletions.
  1. +63 −0 files/core.js
  2. +23 −0 files/style.css
  3. +273 −0 paypal.php
  4. +212 −0 plug.php
63 files/core.js
@@ -0,0 +1,63 @@
+;
+(function($) {
+ $(document).ready(function() {
+ var common_data = {
+ version: $('#paypal_setec_version').val(),
+ user: $('#paypal_setec_user').val(),
+ password: $('#paypal_setec_password').val(),
+ signature: $('#paypal_setec_signature').val()
+ },
+ setec_data = {
+ amount: $('#paypal_setec_amount').val(),
+ currency: $('#paypal_setec_currency').val(),
+ returnurl: $('#paypal_setec_returnurl').val(),
+ cancelurl: $('#paypal_setec_cancelurl').val(),
+ paymentaction: $('#paypal_setec_paymentaction').val()
+ },
+ getec_data = {
+ token: $('#paypal_getec_token').val()
+ },
+ doec_data = {
+ token: $('#paypal_doec_token').val(),
+ payerid: $('#paypal_doec_payerid').val(),
+ amount: $('#paypal_doec_amount').val(),
+ currency: $('#paypal_doec_currency').val(),
+ paymentaction: $('#paypal_doec_paymentaction').val()
+ },
+ display_info = $('#display_debug_info');
+ $('#paypal_setec_send').on('click', function(e) {
+ e.preventDefault();
+ var data = {
+ action: 'pp_setec',
+ common: common_data,
+ setec: setec_data
+ };
+ console.info(common_data);
+ $.post(ajaxurl, data, function(response) {
+ display_info.html(response);
+ });
+ });
+ $('#paypal_getec_send').on('click', function(e) {
+ e.preventDefault();
+ var data = {
+ action: 'pp_getec',
+ common: common_data,
+ setec: getec_data
+ };
+ $.post(ajaxurl, data, function(response) {
+ display_info.html(response);
+ });
+ });
+ $('#paypal_doec_send').on('click', function(e) {
+ e.preventDefault();
+ var data = {
+ action: 'pp_doec',
+ common: common_data,
+ setec: doec_data
+ };
+ $.post(ajaxurl, data, function(response) {
+ display_info.html(response);
+ });
+ });
+ });
+})(jQuery);
23 files/style.css
@@ -0,0 +1,23 @@
+.form-table
+{
+ width: 350px;
+}
+.form-table TBODY TD
+{
+ width: 150px;
+}
+.input
+{
+ float: left;
+ width: 350px;
+}
+.response
+{
+ float: left
+}
+.response P
+{
+ width: 400px;
+ min-height: 50px;
+ background-color: grey;
+}
273 paypal.php
@@ -0,0 +1,273 @@
+<?php
+class wp_paypal_gateway
+{
+ /**
+ * PayPal API Version
+ * @string
+ */
+ public $version;
+
+ /**
+ * PayPal account username
+ * @string
+ */
+ public $user;
+
+ /**
+ * PayPal account password
+ * @string
+ */
+ public $password;
+
+ /**
+ * PayPal account signature
+ * @string
+ */
+ public $signature;
+
+ /**
+ * Period of time (in seconds) after which the connection ends
+ * @integer
+ */
+ public $time_out = 60;
+
+ /**
+ * Requires SSL Verification
+ * @boolean
+ */
+ public $ssl_verify;
+
+ /**
+ * PayPal API Server
+ * @string
+ */
+ private $server;
+
+ /**
+ * PayPal API Redirect URL
+ * @string
+ */
+ private $redirect_url;
+
+ /**
+ * Real world PayPal API Server
+ * @string
+ */
+ private $real_server = 'https://api-3t.paypal.com/nvp';
+
+ /**
+ * Read world PayPal redirect URL
+ * @string
+ */
+ private $real_redirect_url = 'https://www.paypal.com/cgi-bin/webscr';
+
+ /**
+ * Sandbox PayPal Server
+ * @string
+ */
+ private $sandbox_server = 'https://api-3t.sandbox.paypal.com/nvp';
+
+ /**
+ * Sandbox PayPal redirect URL
+ * @string
+ */
+ private $sandbox_redirect_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
+
+ /**
+ * Array representing the supported short-terms
+ * @array
+ */
+ private $short_term = array(
+ 'amount' => 'PAYMENTREQUEST_0_AMT',
+ 'currency_code' => 'PAYMENTREQUEST_0_CURRENCYCODE',
+ 'return_url' => 'RETURNURL',
+ 'cancel_url' => 'CANCELURL',
+ 'payment_action' => 'PAYMENTREQUEST_0_PAYMENTACTION',
+ 'token' => 'TOKEN',
+ 'payer_id' => 'PAYERID'
+ );
+
+ /**
+ * When something goes wrong, the debug_info variable will be set
+ * with a string, array, or object explaining the problem
+ * @mixed
+ */
+ public $debug_info;
+
+ /**
+ * Saves the full response once a request succeed
+ * @mixed
+ */
+ public $full_response = false;
+
+ /**
+ * Creates a new PayPal gateway object
+ * @param boolean $sandbox Set to true if you want to enable the Sandbox mode
+ */
+ public function __construct($sandbox = false)
+ {
+ // Set the Server and Redirect URL
+ if ($sandbox) {
+ $this->server = $this->sandbox_server;
+ $this->redirect_url = $this->sandbox_redirect_url;
+ } else {
+ $this->server = $this->real_server;
+ $this->redirect_url = $this->real_redirect_url;
+ }
+
+ // Set the SSL Verification
+ $this->ssl_verify = apply_filters('https_local_ssl_verify', false);
+ }
+
+ /**
+ * Executes a setExpressCheckout command
+ * @param array $param
+ * @return boolean
+ */
+ public function setExpressCheckout($param)
+ {
+ return $this->requestExpressCheckout('SetExpressCheckout', $param);
+ }
+
+ /**
+ * Executes a getExpressCheckout command
+ * @param array $param
+ * @return boolean
+ */
+ public function getExpressCheckout($param)
+ {
+ return $this->requestExpressCheckout('GetExpressCheckoutDetails', $param);
+ }
+
+ /**
+ * Executes a doExpressCheckout command
+ * @param array $param
+ * @return boolean
+ */
+ public function doExpressCheckout($param)
+ {
+ return $this->requestExpressCheckout('DoExpressCheckoutPayment', $param);
+ }
+
+ /**
+ * @param string $type
+ * @param array $param
+ * @return boolean Specifies if the request is successful and the response property
+ * is filled
+ */
+ private function requestExpressCheckout($type, $param)
+ {
+ // Construct the request array
+ $param = $this->replace_short_terms($param);
+ $request = $this->build_request($type, $param);
+
+ // Makes the HTTP request
+ $response = wp_remote_post($this->server, $request);
+
+ // HTTP Request fails
+ if (is_wp_error($response)) {
+ $this->debug_info = $response;
+ return false;
+ }
+
+ // Status code returned other than 200
+ if ($response['response']['code'] != 200) {
+ $this->debug_info = 'Response code different than 200';
+ return false;
+ }
+
+ // Saves the full response
+ $this->full_response = $response;
+
+ // Request succeeded
+ return true;
+ }
+
+ /**
+ * Replace the Parameters short terms
+ * @param array $param The given parameters array
+ * @return array $param
+ */
+ private function replace_short_terms($param)
+ {
+ foreach ($this->short_term as $short_term => $long_term)
+ {
+ if (array_key_exists($short_term, $param)) {
+ $param[$long_term] = $param[$short_term];
+ unset($param[$short_term]);
+ }
+ }
+ return $param;
+ }
+
+ /**
+ * Builds the request array from the object, param and type parameters
+ * @param string $type
+ * @param array $param
+ * @return array $body
+ */
+ private function build_request($type, $param)
+ {
+ // Request Body
+ $body = $param;
+ $body['METHOD'] = $type;
+ $body['VERSION'] = $this->version;
+ $body['USER'] = $this->user;
+ $body['PWD'] = $this->password;
+ $body['SIGNATURE'] = $this->signature;
+
+ // Request Array
+ $request = array(
+ 'method' => 'POST',
+ 'body' => $body,
+ 'timeout' => $this->time_out,
+ 'sslverify' => $this->ssl_verify
+ );
+
+ return $request;
+ }
+
+ /**
+ * Returns the PayPal Body response
+ * @return array $reponse
+ */
+ public function getResponse()
+ {
+ if ($this->full_response) {
+ parse_str(urldecode($this->full_response['body']), $output);
+ return $output;
+ }
+ return false;
+ }
+
+ /**
+ * Returns the redirect URL
+ * @return string $url
+ */
+ public function getRedirectURL()
+ {
+ $output = $this->getResponse();
+ if ($output['ACK'] === 'Success') {
+ $query_data = array(
+ 'cmd' => '_express-checkout',
+ 'token' => $output['TOKEN']
+ );
+ $url = $this->redirect_url . '?' . http_build_query($query_data);
+ return $url;
+ }
+ return false;
+ }
+
+ /**
+ * Returns the response Token
+ * @return string $token
+ */
+ public function getToken()
+ {
+ $output = $this->getResponse();
+ if ($output['ACK'] === 'Success') {
+ return $output['TOKEN'];
+ }
+ return false;
+ }
+}
212 plug.php
@@ -0,0 +1,212 @@
+<?php
+/*
+ Plugin Name: WPTuts PayPal Plugin
+ Plugin URI: http://wp.tutsplus.com
+ Description: WPTuts PayPal Tutorial Plugin
+ Author: Abid Omar
+ Version: 1.0
+ */
+
+// Adds the PayPal Payment Page to the menu
+add_action('admin_menu', 'wptuts_add_menu');
+function wptuts_add_menu()
+{
+ add_options_page('PayPal Test', 'PayPal Test', 'manage_options', 'wptuts-paypal-test', 'wptuts_display_page');
+}
+
+// Loads JavaScript and CSS files
+add_action('admin_print_scripts', 'pp_enqueue_scripts');
+function pp_enqueue_scripts()
+{
+ wp_register_script('pp_core_script', plugins_url('/files/core.js', __FILE__));
+ wp_enqueue_script('pp_core_script');
+}
+
+add_action('admin_print_styles', 'pp_enqueue_styles');
+function pp_enqueue_styles()
+{
+ wp_register_style('pp_core_style', plugins_url('/files/style.css', __FILE__));
+ wp_enqueue_style('pp_core_style');
+}
+
+// Display the Test Page Interface
+function wptuts_display_page()
+{
+ print <<<end
+ <div class="wrap">
+ <div id="icon-options-general" class="icon32"><br></div>
+ <h2>PayPal Test</h2>
+ <div class="input">
+ <h3>Common Settings</h3>
+ <table class="form-table">
+ <tbody>
+ <tr>
+ <td><label for="paypal_setec_version">Version</label></td>
+ <td><input id="paypal_setec_version"/></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_user">User</label></td>
+ <td><input id="paypal_setec_user"/></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_password">Password</label></td>
+ <td><input id="paypal_setec_password"/></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_signature">Signature</label></td>
+ <td><input id="paypal_setec_signature"/></td>
+ </tr>
+ </tbody>
+ </table>
+ <h3>setExpressCheckOut</h3>
+ <table class="form-table">
+ <tbody>
+ <tr>
+ <td><label for="paypal_setec_amount">Amount</label></td>
+ <td><input id="paypal_setec_amount" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_currency">Currency Code</label></td>
+ <td><input id="paypal_setec_currency" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_returnurl">Return URL</label></td>
+ <td><input id="paypal_setec_returnurl" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_cancelurl">Cancel URL</label></td>
+ <td><input id="paypal_setec_cancelurl" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_setec_paymentaction">Payment Action</label></td>
+ <td><input id="paypal_setec_paymentaction" /></td>
+ </tr>
+ </tbody>
+ </table>
+ <p class="submit">
+ <a href="#" class="button-secondary" id="paypal_setec_send">Send</a>
+ </p>
+ <h3>getExpressCheckOut</h3>
+ <table class="form-table">
+ <tbody>
+ <tr>
+ <td><label for="paypal_getec_token">Token</label></td>
+ <td><input id="paypal_getec_token" /></td>
+ </tr>
+ </tbody>
+ </table>
+ <p class="submit">
+ <a href="#" class="button-secondary" id="paypal_getec_send">Send</a>
+ </p>
+ <h3>doExpressCheckOut</h3>
+ <table class="form-table">
+ <tbody>
+ <tr>
+ <td><label for="paypal_doec_token">Token</label></td>
+ <td><input id="paypal_doec_token" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_doec_payerid">Payer ID</label></td>
+ <td><input id="paypal_doec_payerid" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_doec_amount">Amount</label></td>
+ <td><input id="paypal_doec_amount" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_doec_currency">Currency Code</label></td>
+ <td><input id="paypal_doec_currency" /></td>
+ </tr>
+ <tr>
+ <td><label for="paypal_doec_paymentaction">Payment Action</label></td>
+ <td><input id="paypal_doec_paymentaction" /></td>
+ </tr>
+ </tbody>
+ </table>
+ <p class="submit">
+ <a href="#" class="button-secondary" id="paypal_doec_send">Send</a>
+ </p>
+ </div>
+ <div class="response">
+ <h3>Response</h3>
+ <p id="display_debug_info">
+ </p>
+ </div>
+ </div>
+end;
+}
+
+// Handle AJAX Requests
+// SetExpressCheckOut
+add_action('wp_ajax_pp_setec', 'pp_ajax_setec');
+function pp_ajax_setec()
+{
+ global $_POST;
+ require_once ('paypal.php');
+ $paypal = new wp_paypal_gateway (true);
+ $paypal->version = $_POST['common']['version'];
+ $paypal->user = $_POST['common']['user'];
+ $paypal->password = $_POST['common']['password'];
+ $paypal->signature = $_POST['common']['signature'];
+ $param = array(
+ 'amount' => $_POST['setec']['amount'],
+ 'currency_code' => $_POST['setec']['currency'],
+ 'return_url' => $_POST['setec']['returnurl'],
+ 'cancel_url' => $_POST['setec']['cancelurl'],
+ 'payment_action' => $_POST['setec']['paymentaction']
+ );
+ if ($paypal->setExpressCheckout($param)) {
+ var_dump($paypal->getResponse());
+ } else {
+ var_dump($paypal->debug_info);
+ }
+ die();
+}
+
+// GetExpressCheckOut
+add_action('wp_ajax_pp_getec', 'pp_ajax_getec');
+function pp_ajax_getec()
+{
+ global $_POST;
+ require_once ('paypal.php');
+ $paypal = new wp_paypal_gateway (true);
+ $paypal->version = $_POST['common']['version'];
+ $paypal->user = $_POST['common']['user'];
+ $paypal->password = $_POST['common']['password'];
+ $paypal->signature = $_POST['common']['signature'];
+ $param = array(
+ 'amount' => $_POST['getec']['token']
+ );
+ if ($paypal->getExpressCheckout($param)) {
+ print_r($paypal->getResponse());
+ } else {
+ print_r($paypal->debug_info);
+ }
+ die();
+}
+
+// DoExpressCheckOut
+add_action('wp_ajax_pp_doec', 'pp_ajax_doec');
+function pp_ajax_doec()
+{
+ global $_POST;
+ require_once ('paypal.php');
+ $paypal = new wp_paypal_gateway (true);
+ $paypal->version = $_POST['common']['version'];
+ $paypal->user = $_POST['common']['user'];
+ $paypal->password = $_POST['common']['password'];
+ $paypal->signature = $_POST['common']['signature'];
+ $param = array(
+ 'amount' => $_POST['doec']['amount'],
+ 'currency_code' => $_POST['doec']['currency'],
+ 'payment_action' => $_POST['doec']['paymentaction'],
+ 'payer_id' => $_POST['doec']['payerid'],
+ 'token' => $_POST['doec']['token']
+ );
+ if ($paypal->doExpressCheckout($param)) {
+ print_r($paypal->getResponse());
+ } else {
+ print_r($paypal->debug_info);
+ }
+ die();
+}
Please sign in to comment.
Something went wrong with that request. Please try again.