From 4a99b04e1d65c01df5d0d8fe9a55e32eb35fc0ac Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 5 Dec 2011 16:03:39 +1000 Subject: [PATCH 1/5] Commenting phpme_map function and renaming the second param to make it a better descriptor of what it represents --- common/helpers.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/common/helpers.php b/common/helpers.php index e66c0d7..235d22a 100644 --- a/common/helpers.php +++ b/common/helpers.php @@ -1,12 +1,19 @@ $value pairs where the $value represents a key to match in the $from_array + * @param $return_type String Whether to return the newly created array as an Array (default) or object. + */ +function phpme_map( $from_array, $keys, $return_type = 'Array' ) { $return = array(); - - foreach ( $map as $to_key => $from_key ) { + + foreach ( $keys as $to_key => $from_key ) if ( isset( $from_array[$from_key] ) ) - $return[$to_key] = $from_array[$from_key]; - } - + $return[$to_key] = $from_array[$from_key]; + if ( $return_type == 'Object' ) $return = (Object) $return; From 347e14a6a117c989e3689869fd2f2e89e41ff8b2 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 5 Dec 2011 16:35:35 +1000 Subject: [PATCH 2/5] Better comments for the PHP_Merchant_Paypal_Express_Checkout class. --- gateways/paypal-express-checkout.php | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/gateways/paypal-express-checkout.php b/gateways/paypal-express-checkout.php index d9d9805..05215bd 100644 --- a/gateways/paypal-express-checkout.php +++ b/gateways/paypal-express-checkout.php @@ -8,6 +8,12 @@ public function __construct( $options = array() ) { parent::__construct( $options ); } + /** + * Creates and returns the payment component of a PayPal NVP API request. + * + * @param $action String The PayPal Express Checkout payment action. One of Sale, Authorization or Order. For more details, see here: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_SetExpressCheckout#id1055FM0B05Z__N507DD + * @return Array An array of name value pairs for each element representing a payment in a PayPal NVP API request. + */ protected function add_payment( $action ) { $request = array( 'PAYMENTREQUEST_0_AMT' => $this->format( $this->options['amount'] ), @@ -61,6 +67,11 @@ protected function add_payment( $action ) { return $request; } + /** + * Creates and returns the Shipping component of a PayPal NVP API request. + * + * @return Array An array of name value pairs for each element required to explain shipping information to PayPal via an NVP API request. + */ protected function add_address() { $map = array( 'name' => 'PAYMENTREQUEST_0_SHIPTONAME', @@ -83,6 +94,13 @@ protected function add_address() { return $request; } + /** + * Creates and returns the entire order details component of the PayPal NVP API request string. + * + * @uses self::add_address() to add an address to the request + * @uses self::add_payment() to add the payment details to the request + * @return Array An array of name value pairs for each element representing a payment via the PayPal NVP API. + */ protected function build_checkout_request( $action, $options = array() ) { $request = array(); @@ -107,6 +125,12 @@ protected function build_checkout_request( $action, $options = array() ) { return $request; } + /** + * Initiates an Express Checkout payment by calling PayPal to perform the SetExpressCheckout NVP API method. + * + * @uses self::build_checkout_request() to create the request + * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. + */ public function setup_purchase( $options = array() ) { $this->options = array_merge( $this->options, $options ); $this->requires( 'amount', 'return_url', 'cancel_url' ); @@ -116,18 +140,35 @@ public function setup_purchase( $options = array() ) { return new PHP_Merchant_Paypal_Express_Checkout_Response( $response_str ); } + /** + * Creates and returns all the name => value pairs required to get checkout details from PayPal, which is just the token for the checkout. + * + * @return Array An array of name value pairs for each element required to perform a GetExpressCheckoutDetails NVP API request + */ public function build_get_details_request( $token ) { return array( 'TOKEN' => $token, ); } + /** + * Gets the details of an express checkout transaction by calling PayPal to perform the GetExpressCheckoutDetails NVP API method. + * + * @uses self::build_get_details_request() to create the request + * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. + */ public function get_details_for( $token ) { $request = $this->build_get_details_request( $token ); $response_str = $this->commit( 'GetExpressCheckoutDetails', $request ); return new PHP_Merchant_Paypal_Express_Checkout_Response( $response_str ); } + /** + * Completes an Express Checkout transaction by calling PayPal to perform the DoExpressCheckoutPayment NVP API method. + * + * @uses self::build_checkout_request() to create the request + * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. + */ public function purchase( $options = array() ) { $this->options = array_merge( $this->options, $options ); $this->requires( 'amount', 'token', 'payer_id' ); From 0fdbfad52383933ff6f5404f7c092ad3404efddf Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 5 Dec 2011 16:36:00 +1000 Subject: [PATCH 3/5] Initial commit of Digital Goods class. --- gateways/paypal-digital-goods.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 gateways/paypal-digital-goods.php diff --git a/gateways/paypal-digital-goods.php b/gateways/paypal-digital-goods.php new file mode 100644 index 0000000..9c8eeb5 --- /dev/null +++ b/gateways/paypal-digital-goods.php @@ -0,0 +1,22 @@ +options['items'] ); $i++ ) + $request += array( "L_PAYMENTREQUEST_0_ITEMCATEGORY{$i}" => 'Digital', + + return $request; + } + +} \ No newline at end of file From 340617c01dfd63d40807c0942485de8f8dc6bc28 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 5 Dec 2011 18:33:24 +1000 Subject: [PATCH 4/5] Commenting & fixing the odd bug in PHP_Merchant_Paypal_Digital_Goods. --- gateways/paypal-digital-goods.php | 40 ++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/gateways/paypal-digital-goods.php b/gateways/paypal-digital-goods.php index 9c8eeb5..ff56a23 100644 --- a/gateways/paypal-digital-goods.php +++ b/gateways/paypal-digital-goods.php @@ -9,14 +9,52 @@ public function __construct( $options = array() ) { parent::__construct( $options ); } + /** + * Creates and returns the payment component of a PayPal Digital Goods NVP API request. + * + * PayPal requires the category component for all items in a digital goods purchase to be set as Digital. + * This function specifies that all goods are digital, then calls @see parent::add_payment() to create + * the rest of the API request (which is the same as a vanilla Express Checkout request). + * + * @uses parent::add_payment() to create non digital goods components of the request. + * @return Array An array of name value pairs for each element representing a payment in a PayPal Digital Goods NVP API request. + */ protected function add_payment( $action ) { $request = parent::add_payment( $action ); // Make sure PayPal knows all goods are digital for( $i = 0; $i < count( $this->options['items'] ); $i++ ) - $request += array( "L_PAYMENTREQUEST_0_ITEMCATEGORY{$i}" => 'Digital', + $request += array( "L_PAYMENTREQUEST_0_ITEMCATEGORY{$i}" => 'Digital' ); return $request; } + /** + * For Digital Goods purchases, PayPal requires the PAYMENTREQUEST_n_ITEMAMT. This function sets the 'items' flag to required + * then calls @see parent::setup_purchase() to initiate an Express Checkout payment. + * + * @uses self::requires() to flag 'items' as required + * @uses parent::setup_purchase() to create and make the request. + * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. + */ + public function setup_purchase( $options = array() ) { + $this->requires( 'items' ); + + return parent::setup_purchase( $options ); + } + + /** + * For Digital Goods purchases, PayPal requires the PAYMENTREQUEST_n_ITEMAMT. This function sets the 'items' flag to required + * then calls @see parent::setup_purchase() to complete the payment. + * + * @uses self::requires() to flag 'items' as required + * @uses parent::setup_purchase() to create and make the request. + * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. + */ + public function purchase( $options = array() ) { + $this->requires( 'items' ); + + return parent::purchase( $options ); + } + } \ No newline at end of file From 18afa3c92ecd6392857f9f91e527762eb08c501b Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Mon, 12 Dec 2011 10:26:33 +1000 Subject: [PATCH 5/5] Adding get_script() function for outputing iframe/DGFlow script & adding it to specified submit button elemement. Removing "item" requirement for purchase. --- gateways/paypal-digital-goods.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/gateways/paypal-digital-goods.php b/gateways/paypal-digital-goods.php index ff56a23..d7fbc72 100644 --- a/gateways/paypal-digital-goods.php +++ b/gateways/paypal-digital-goods.php @@ -38,8 +38,6 @@ protected function add_payment( $action ) { * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. */ public function setup_purchase( $options = array() ) { - $this->requires( 'items' ); - return parent::setup_purchase( $options ); } @@ -52,9 +50,30 @@ public function setup_purchase( $options = array() ) { * @return PHP_Merchant_Paypal_Express_Checkout_Response An object containing the details of PayPal's response to the request. */ public function purchase( $options = array() ) { - $this->requires( 'items' ); - return parent::purchase( $options ); } + + /** + * The Javascript to invoke the digital goods in context checkout process. + * + * No need to call this function manually, required scripts are automatically printed with @see print_buy_buttion(). + * If you do print this script manually, print it after the button in the DOM to ensure the + * click event is properly hooked. + */ + public function get_script( $args = array() ){ + + if( empty( $args['element_id'] ) ) + $args['element_id'] = 'paypal-submit'; + + $dg_script = '' + . ''; + + return $dg_script; + } + + } \ No newline at end of file