Skip to content

Commit

Permalink
Merge pull request #13 from peacemoon/paypalexpresscheckout
Browse files Browse the repository at this point in the history
Add paypal express checkout driver
  • Loading branch information
calvinfroedge committed Feb 28, 2015
2 parents 9f48e13 + 8178927 commit 01f344c
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/drivers/paypal_expresscheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
// API Username sdk-three_api1.sdk.com
// API Password QFZCWN5HZM8VBG7Q
// API Signature A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU

$config['api_username'] = "seller_1343132693_biz_api1.gmail.com";
$config['api_password'] = "1343132718";
$config['api_signature'] = "AFcWxV21C7fd0v3bYYYRCpSSRl31A00tjBOqwuLySVNFyTJmdRM7Gcnw";

return $config;
6 changes: 6 additions & 0 deletions config/payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
'oneoff_payment_button_success' => '100',
'oneoff_payment_button_local_failure' => '000',
'oneoff_payment_button_gateway_failure' => '011',
'request_express_checkout_success' => '100',
'request_express_checkout_local_failure' => '000',
'request_express_checkout_gateway_failure' => '011',
'do_express_checkout_success' => '100',
'do_express_checkout_local_failure' => '000',
'do_express_checkout_gateway_failure' => '011',
'reference_payment_success' => '100',
'reference_payment_local_failure' => '000',
'reference_payment_gateway_failure' => '011',
Expand Down
2 changes: 2 additions & 0 deletions language/english/response_details_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'missing_first_name' => 'First name is required but was not provided in the request',
'missing_last_name' => 'Last name is required but was not provided in the request',
'missing_currency_code' => 'Currency is required but was not provided in the request',
'missing_token' => 'Token is required but was not provided in the request',
'missing_payer_id' => 'Payer Identifier is required but was not provided in the request',
'no_details' => 'No further details provided',
'is_not_a_param' => 'Is not a valid parameter for this method',
);
Expand Down
6 changes: 6 additions & 0 deletions language/english/response_messages_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
'oneoff_payment_button_success' => 'The payment button was generated successfully',
'oneoff_payment_button_local_failure' => 'There was a problem generating the payment button',
'oneoff_payment_button_gateway_failure' => 'The payment button could not be generated',
'request_express_checkout_success' => 'Express check is called successfully',
'request_express_checkout_local_failure' => 'There was a problem calling express checkout',
'request_express_checkout_gateway_failure' => 'Express checkout is failed to run',
'do_express_checkout_success' => 'Express check is called successfully',
'do_express_checkout_local_failure' => 'There was a problem calling express checkout',
'do_express_checkout_gateway_failure' => 'Express checkout is failed to run',
'reference_payment_success' => 'The payment was successful.',
'reference_payment_local_failure' => 'The payment could not be sent to the payment gateway because it failed local validation.',
'reference_payment_gateway_failure' => 'The payment was declined by the payment gateway.',
Expand Down
272 changes: 272 additions & 0 deletions lib/payment_drivers/paypal_expresscheckout_driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?php

class PayPal_Expresscheckout_Driver extends Payment_Driver
{
/**
* The PHP Payments Method
*/
private $_lib_method;

/**
* The Paypal API to use
*/
private $_api;

/**
* Test or Production Mode
*/
private $_mode;

/**
* The API endpoint to send the request to
*/
private $_api_endpoint = 'https://api-3t.paypal.com/nvp';

/**
* The API endpoint to send a test request to
*/
private $_api_endpoint_test = 'https://api-3t.sandbox.paypal.com/nvp';

/**
* Current Version of PayPal's API
*/
private $_api_version = '66.0';

/**
* An array for storing all settings
*/
private $_settings = array();

/**
* Constructor method
*/
public function __construct($config)
{
$this->_settings = array(
'USER' => $config['api_username'],
'PWD' => $config['api_password'],
'SIGNATURE' => $config['api_signature'],
'VERSION' => $this->_api_version
);
$this->_mode = $config['mode'];
}

/**
* Caller Magic Method
*/
public function __call($method, $params)
{
$this->_lib_method = $method;

$args = $params[0];
$request = $this->_build_request($args);
$endpoint = ($this->_mode == 'production') ? $this->_api_endpoint : $this->_api_endpoint_test;
$request_string = $endpoint.'?'.$request;

$raw_response = Payment_Request::curl_request($request_string);
return $this->_parse_response($raw_response);
}

/**
* Maps Methods to Keys
*/
public function method_map()
{
$map = array(
'request_express_checkout' => array(
'api' => 'SetExpressCheckout',
'required' => array(
'amt',
'currency_code',
'return_url',
'cancel_url'
),
'keymatch' => array(
'amt' => 'AMT',
'currency_code' => 'CURRENCYCODE',
'return_url' => 'RETURNURL',
'cancel_url' => 'CANCELURL'
),
'static' => array(
'PAYMENTACTION' => 'Sale'
)
),
'do_express_checkout' => array(
'api' => 'DoExpressCheckoutPayment',
'required' => array(
'token', //Reference for a previous payment
'payer_id',
'amt',
'ip_address'
),
'keymatch' => array(
'token' => 'TOKEN',
'payer_id' => 'PAYERID',
'currency_code' => 'CURRENCYCODE',
'amt' => 'AMT',
'ip_address' => 'IPADDRESS'
),
'static' => array(
'PAYMENTACTION' => 'Sale'
)
),
'void_payment' => array(
'api' => 'DoVoid',
'required' => array(
'identifier',
'note'
),
'keymatch' => array(
'identifier' => 'AUTHORIZATIONID',
'note' => 'NOTE'
)
)
);
return $map;
}

/**
* Build the Request
*
* @param array
* @return array
*/
protected function _build_request($params)
{
//Normalize some param formats
$params_adjusted = array();
foreach($params as $k=>$v)
{
if($k == 'currency_code')
{
$val = strtoupper($v);
}
else
{
$val = $v;
}

$params_adjusted[$k] = $val;
}

$args = $this->_match_params($params_adjusted);
$request = http_build_query(array_merge(array('METHOD' => $this->_api), $this->_settings, $args));
return $request;
}

/**
* Match Params
*
* @param array
* @return array
*/
private function _match_params($params)
{
$l = $this->_lib_method;
$map = $this->method_map();
$this->_api = $map[$l]['api'];

$fields = array();
foreach($params as $k=>$v)
{
if(isset($map[$l]['keymatch'][$k]))
{
$key = $map[$l]['keymatch'][$k];
if(!isset($fields[$key]))
{
$fields[$key] = $v;
}
else
{
$fields[$key] .= " $v";
}
}
else
{
error_log("$k is not a valid param for the $l method of the Paypal PaymentsPro driver.");
}
}

if(isset($map[$l]['static']))
{
$static = $map[$l]['static'];

foreach($static as $k=>$v)
{
$fields[$k] = $v;
}
}

return $fields;
}

/**
* Parse the response from the server
*
* @param array
* @return object
*/
protected function _parse_response($response)
{

if($response === FALSE)
{
return Payment_Response::instance()->gateway_response(
'Failure',
$this->_lib_method.'_gateway_failure'
);
}

$results = explode('&',urldecode($response));
foreach($results as $result)
{
list($key, $value) = explode('=', $result);
$gateway_response[$key]=$value;
}

$details = (object) array(
'gateway_response' => (object) array()
);
foreach($gateway_response as $k=>$v)
{
$details->gateway_response->$k = $v;
}

if(isset($gateway_response['L_LONGMESSAGE0']))
{
$details->reason = $gateway_response['L_LONGMESSAGE0'];
}

if(isset($gateway_response['TIMESTAMP']))
{
$details->timestamp = $gateway_response['TIMESTAMP'];
}

if(isset($gateway_response['TRANSACTIONID']))
{
$details->identifier = $gateway_response['TRANSACTIONID'];
}

if(isset($gateway_response['PROFILEID']))
{
$details->identifier = $gateway_response['PROFILEID'];
}

if($gateway_response['ACK'] == 'Success')
{
return Payment_Response::instance()->gateway_response(
'Success',
$this->_lib_method.'_success',
$details
);
}
else
{
return Payment_Response::instance()->gateway_response(
'Failure',
$this->_lib_method.'_gateway_failure',
$details
);
}
}
}
29 changes: 29 additions & 0 deletions lib/payment_methods/do_express_checkout_method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class Do_Express_Checkout_Method implements Payment_Method
{
private $_params;

private $_descrip = "Creates HTML code for a button which directs the user to a hosted payments page on which they can complete the transaction. Similar to Oneoff_Payment.";

public function __construct()
{
$this->_params = array(
'token' => 'EC-6RV20511T7745502A', // token received from paypal
'payer_id' => 'Z98CWZC8PA9Y2', // payer id received from paypal
'amt' => '10.00', // Amount for the payment
'currency_code' => 'EUR',
'ip_address' => '127.0.0.1' // ip address of the payer
);
}

public function get_params()
{
return $this->_params;
}

public function get_description()
{
return $this->_descrip();
}
}
29 changes: 29 additions & 0 deletions lib/payment_methods/request_express_checkout_method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class Request_Express_Checkout_Method implements Payment_Method
{
private $_params;

private $_descrip = "Creates HTML code for a button which directs the user to a hosted payments page on which they can complete the transaction. Similar to Oneoff_Payment.";

public function __construct()
{
$this->_params = array(
'amt' => '10.00', //Amount for the payment
'desc' => 'Your button text',
'currency_code' => 'EUR',
'return_url' => 'http://returnto.me/url', //Your URL for receiving Instant Payment Notification (IPN) about this transaction. If you do not specify this value in the request, the notification URL from your Merchant Profile is used, if one exists.
'cancel_url' => 'http://cancel.purchase', //Link for continue shopping button
);
}

public function get_params()
{
return $this->_params;
}

public function get_description()
{
return $this->_descrip();
}
}

0 comments on commit 01f344c

Please sign in to comment.