Ejegg has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/335742 )
Change subject: WIP normalize recurring messages at the listener ...................................................................... WIP normalize recurring messages at the listener This will require a slight tweak to the consumer in CRM, to detect already-normalized recurring payments. Each message type will handle its own normalization, as with some other classes. Also gets the silly KeyedOpaque thing out of Paypal since we can write directly to the Redis queues now. Bug: T107372 Change-Id: Ic982667bab0e216677557139c43592a921d948d4 --- M PaymentProviders/PayPal/Job.php M PaymentProviders/PayPal/Message.php A PaymentProviders/PayPal/PaymentMessage.php A PaymentProviders/PayPal/RecurringMessage.php A PaymentProviders/PayPal/RefundMessage.php A PaymentProviders/PayPal/SubscriptionMessage.php M SmashPig.yaml 7 files changed, 124 insertions(+), 72 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig refs/changes/42/335742/1 diff --git a/PaymentProviders/PayPal/Job.php b/PaymentProviders/PayPal/Job.php index 2871afb..b76727f 100644 --- a/PaymentProviders/PayPal/Job.php +++ b/PaymentProviders/PayPal/Job.php @@ -51,77 +51,34 @@ throw new Exception( 'Invalid PayPal message: ' . json_encode( $request ) ); } - $msg_type = null; + $msgClass = null; + $queue = ''; foreach ( $this->config->val( 'messages' ) as $type => $conf ) { if ( in_array( $txn_type, $conf['txn_types'] ) ) { - $msg_type = $type; + $msgClass = $conf['class']; + $queue = $conf['queue']; } } - if ( ! $msg_type ) { + if ( !$msgClass ) { throw new Exception( 'Invalid PayPal message type: ' . $txn_type ); } // Transform into new message. - // FIXME this could just be an array, but we need compat with - // keyedopaque* until activemq goes away - $new_msg = new Message; - // FIXME hacks because the recurring consumer doesn't want - // a normalized message - if ( $msg_type === 'recurring' ) { - foreach ( $request as $key => $val ) { - $new_msg->$key = $val; - } - } else { - $map = $this->config->val( 'var_map' ); - foreach ( $map as $rx => $tx ) { - if ( array_key_exists( $rx, $request ) ) { - $new_msg->$tx = $request[$rx]; - } - } - - // FIXME: var map can't put one thing in two places - if ( isset( $new_msg->contribution_tracking_id ) ) { - $new_msg->order_id = $new_msg->contribution_tracking_id; - } - - // FIXME represent special case as var_map config override? - if ( $msg_type === 'refund' ) { - $new_msg->gateway_refund_id = $request['txn_id']; - $new_msg->gross_currency = $request['mc_currency']; - if ( isset( $new_msg->type ) && - $new_msg->type === 'chargeback_settlement' ) { - $new_msg->type = 'chargeback'; - } else { - $new_msg->type = $msg_type; - } - } - - // If someone's PayPal account is set to their name we don't want - // it to go in the address box. They should put in a business name - // or something. - if ( isset( $new_msg->supplemental_address_1 ) - && $new_msg->supplemental_address_1 === - "{$new_msg->first_name} {$new_msg->last_name}" ) { - unset( $new_msg->supplemental_address_1 ); - } - - // FIXME once recurring uses normalized msg it needs this too - $new_msg->date = strtotime( $new_msg->date ); - } + $creator = array( $msgClass, 'fromIpnMessage' ); + $normalized = call_user_func( $creator, $request ); if ( $txn_type == 'express_checkout' ) { - $new_msg->gateway = 'paypal_ec'; + $normalized['gateway'] = 'paypal_ec'; } else { - $new_msg->gateway = 'paypal'; + $normalized['gateway'] = 'paypal'; } - - SourceFields::addToMessage( $new_msg ); + SourceFields::addToMessage( $normalized ); // Save to appropriate queue. - $this->config->object( 'data-store/' . $msg_type ) - ->push( $new_msg ); + $this->config->object( 'data-store/' . $queue ) + ->push( $normalized ); // FIXME random document formats if ( substr( $txn_type, 0, 7 ) === 'subscr_' ) { @@ -130,7 +87,7 @@ $log_id = "txn_id:{$request['txn_id']}"; } - Logger::info( "Message {$log_id} pushed to {$msg_type} queue." ); + Logger::info( "Message {$log_id} pushed to {$queue} queue." ); // TODO It would be nice if push() returned something useful so we // could return something here too diff --git a/PaymentProviders/PayPal/Message.php b/PaymentProviders/PayPal/Message.php index 8f670ca..d2a84bf 100644 --- a/PaymentProviders/PayPal/Message.php +++ b/PaymentProviders/PayPal/Message.php @@ -1,11 +1,42 @@ <?php namespace SmashPig\PaymentProviders\PayPal; -use SmashPig\Core\DataStores\KeyedOpaqueStorableObject; +use SmashPig\Core\Context; -class Message extends KeyedOpaqueStorableObject { -// _ __ ___ ___ _ __ -// | '_ \ / _ \ _____ / _ \| '_ \ -// | | | | (_) |_____| (_) | |_) | -// |_| |_|\___/ \___/| .__/ -// |_| +/** + * abstract static inheritance? Whatamidoing? + */ +abstract class Message { + static function fromIpnMessage( $ipnArray ) { + $config = Context::get()->getConfiguration(); + + $message = $ipnArray; + $map = $config->val( 'var_map' ); + foreach ( $map as $rx => $tx ) { + if ( array_key_exists( $rx, $ipnArray ) ) { + $message[$tx] = $ipnArray[$rx]; + } + } + + if ( isset( $message['contribution_tracking_id'] ) ) { + $message['order_id'] = $message['contribution_tracking_id']; + } + + // If someone's PayPal account is set to their name we don't want + // it to go in the address box. They should put in a business name + // or something. + if ( isset( $message['supplemental_address_1'] ) + && $message['supplemental_address_1'] === + "{$message['first_name']} {$message['last_name']}" ) { + unset( $message['supplemental_address_1'] ); + } + + // FIXME once recurring uses normalized msg it needs this too + $message['date'] = strtotime( $message['date'] ); + + + static::normalizeMessage( $message, $ipnArray ); + return $message; + } + + abstract function normalizeMessage( &$message, $ipnArray ); } diff --git a/PaymentProviders/PayPal/PaymentMessage.php b/PaymentProviders/PayPal/PaymentMessage.php new file mode 100644 index 0000000..185a54d --- /dev/null +++ b/PaymentProviders/PayPal/PaymentMessage.php @@ -0,0 +1,10 @@ +<?php + +namespace SmashPig\PaymentProviders\PayPal; + +class PaymentMessage extends Message { + + function normalizeMessage( &$message, $ipnMessage ) { + // TODO: Implement normalizeMessage() method. + } +} diff --git a/PaymentProviders/PayPal/RecurringMessage.php b/PaymentProviders/PayPal/RecurringMessage.php new file mode 100644 index 0000000..f063cec --- /dev/null +++ b/PaymentProviders/PayPal/RecurringMessage.php @@ -0,0 +1,10 @@ +<?php + +namespace SmashPig\PaymentProviders\PayPal; + +class RecurringMessage extends Message { + + function normalizeMessage( &$message, $ipnMessage ) { + // TODO: Implement normalizeMessage() method. + } +} diff --git a/PaymentProviders/PayPal/RefundMessage.php b/PaymentProviders/PayPal/RefundMessage.php new file mode 100644 index 0000000..5fe9103 --- /dev/null +++ b/PaymentProviders/PayPal/RefundMessage.php @@ -0,0 +1,17 @@ +<?php + +namespace SmashPig\PaymentProviders\PayPal; + +class RefundMessage extends Message { + + function normalizeMessage( &$message, $ipnMessage ) { + $message['gateway_refund_id'] = $ipnMessage['txn_id']; + $message['gross_currency'] = $ipnMessage['mc_currency']; + if ( isset( $message['type'] ) && + $message['type'] === 'chargeback_settlement' ) { + $message['type'] = 'chargeback'; + } else { + $message['type'] = 'refund'; + } + } +} diff --git a/PaymentProviders/PayPal/SubscriptionMessage.php b/PaymentProviders/PayPal/SubscriptionMessage.php new file mode 100644 index 0000000..965c7ec --- /dev/null +++ b/PaymentProviders/PayPal/SubscriptionMessage.php @@ -0,0 +1,10 @@ +<?php + +namespace SmashPig\PaymentProviders\PayPal; + +class SubscriptionMessage extends Message { + + function normalizeMessage( &$message, $ipnMessage ) { + // TODO: Implement normalizeMessage() method. + } +} diff --git a/SmashPig.yaml b/SmashPig.yaml index e09d775..274437e 100644 --- a/SmashPig.yaml +++ b/SmashPig.yaml @@ -43,12 +43,6 @@ - 'mysql:host=127.0.0.1;dbname=fredge' recurring: - class: SmashPig\Core\DataStores\MultiQueueWriter - constructor-parameters: - - - - recurring-new - - recurring-new: class: PHPQueue\Backend\Predis constructor-parameters: - @@ -374,7 +368,8 @@ txn_type: new_case messages: - verified: + payment: + class: SmashPig\PaymentProviders\PayPal\PaymentMessage valid_statuses: # TODO is this message type agnostic? - Completed - Reversed @@ -386,23 +381,45 @@ - express_checkout - masspay - virtual_terminal - recurring: + queue: + - donations + + recurring: # new style recurring payments + class: SmashPig\PaymentProviders\PayPal\RecurringMessage txn_types: + - recurring_payment + - recurring_payment_expired + - recurring_payment_failed + - recurring_payment_profile_cancel - recurring_payment_profile_created + - recurring_payment_skipped + - recurring_payment_suspended + - recurring_payment_suspended_due_to_max_failed_payment + queue: + - recurring + + subscription: # old style recurring payments + class: SmashPig\PaymentProviders\PayPal\SubscriptionMessage + txn_types: - subscr_cancel - subscr_eot - subscr_failed - subscr_modify - subscr_signup # the following mean we got money \o/ - - recurring_payment - subscr_payment + queue: + - recurring + refund: + class: SmashPig\PaymentProviders\PayPal\RefundMessage txn_types: - adjustment - refund # FIXME: if case_type=chargeback, then txn_type might be blank # and the message should go to the refund queue. + queue: + - refund-new # FIXME make the -new go away globalcollect: actions: -- To view, visit https://gerrit.wikimedia.org/r/335742 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic982667bab0e216677557139c43592a921d948d4 Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/SmashPig Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits