public function __construct($values)
 {
     parent::__construct();
     $details = $values['RefundDetails'];
     $this->setGatewayIds($details['AmazonRefundId']);
     $this->date = UtcDate::getUtcTimestamp($details['CreationTimestamp']);
     $this->currency = $details['RefundAmount']['CurrencyCode'];
     $this->gross = $details['RefundAmount']['Amount'];
     // TODO: do we need to use FeeRefunded for anything?
 }
 public function __construct($values)
 {
     parent::__construct();
     $details = $values['CaptureDetails'];
     $this->setGatewayIds($details['AmazonCaptureId']);
     $captureReferenceId = $details['CaptureReferenceId'];
     $this->completion_message_id = "amazon-{$captureReferenceId}";
     $this->order_id = $captureReferenceId;
     $parts = explode('-', $captureReferenceId);
     $this->contribution_tracking_id = $parts[0];
     $this->date = UtcDate::getUtcTimestamp($details['CreationTimestamp']);
     $this->currency = $details['CaptureAmount']['CurrencyCode'];
     $this->gross = $details['CaptureAmount']['Amount'];
     $this->fee = $details['CaptureFee']['Amount'];
     $this->gateway_status = $details['CaptureStatus']['State'];
 }
 /**
  * @param HeadedCsvReader $csv
  */
 protected function parseLine(HeadedCsvReader $csv)
 {
     $status = $csv->currentCol('RefundStatus');
     // Only process completed
     if ($status !== 'Completed') {
         return;
     }
     $msg = array();
     $msg['date'] = UtcDate::getUtcTimestamp($csv->currentCol('LastUpdateTimestamp'));
     $msg['gateway'] = 'amazon';
     $msg['gateway_parent_id'] = $csv->currentCol('AmazonCaptureId');
     $msg['gateway_refund_id'] = $csv->currentCol('AmazonRefundId');
     $msg['gross'] = $csv->currentCol('RefundAmount');
     $msg['gross_currency'] = $csv->currentCol('CurrencyCode');
     if ($csv->currentCol('RefundType') === 'SellerInitiated') {
         $msg['type'] = 'refund';
     } else {
         $msg['type'] = 'chargeback';
     }
     $this->fileData[] = $msg;
 }
 /**
  * Build and insert a database record from a queue message
  *
  * @param array $message Unprocessable message
  * @param string $originalQueue Queue the message was first sent to
  * @param string $error Information about why this message is damaged
  * @param string $trace Full stack trace
  * @param int|null $retryDate When provided, re-process message after
  *  this timestamp
  * @return int ID of message in damaged database
  */
 public function storeMessage($message, $originalQueue, $error = '', $trace = '', $retryDate = null)
 {
     $now = UtcDate::getUtcTimestamp();
     $originalDate = UtcDate::getUtcDatabaseString(DateFields::getOriginalDateOrDefault($message, $now));
     $dbRecord = array('original_date' => $originalDate, 'damaged_date' => UtcDate::getUtcDatabaseString(), 'original_queue' => $originalQueue, 'error' => $error, 'trace' => $trace, 'message' => json_encode($message));
     if ($retryDate) {
         $dbRecord['retry_date'] = UtcDate::getUtcDatabaseString($retryDate);
     }
     // These fields have their own columns in the database
     // Copy the values from the message to the record
     $indexedFields = array('gateway', 'gateway_txn_id', 'order_id');
     foreach ($indexedFields as $fieldName) {
         if (isset($message[$fieldName])) {
             $dbRecord[$fieldName] = $message[$fieldName];
         }
     }
     list($fieldList, $paramList) = self::formatInsertParameters($dbRecord);
     $insert = "INSERT INTO damaged ( {$fieldList} )\n\t\t\tVALUES ( {$paramList} );";
     $this->prepareAndExecute($insert, $dbRecord);
     return $this->getDatabase()->lastInsertId();
 }
 /**
  * @param HeadedCsvReader $csv
  * @throws OutOfBoundsException
  */
 protected function parseLine(HeadedCsvReader $csv)
 {
     $type = $csv->currentCol('TransactionType');
     // Only process captures
     if ($type !== 'Capture') {
         return;
     }
     $msg = array();
     $orderId = $csv->currentCol('SellerReferenceId');
     $parts = explode('-', $orderId);
     $msg['contribution_tracking_id'] = $parts[0];
     $msg['currency'] = $csv->currentCol('CurrencyCode');
     $msg['date'] = UtcDate::getUtcTimestamp($csv->currentCol('TransactionPostedDate'));
     $msg['fee'] = -1 * $csv->currentCol('TotalTransactionFee');
     $msg['gateway'] = 'amazon';
     $msg['gateway_txn_id'] = $csv->currentCol('AmazonTransactionId');
     $msg['gross'] = $csv->currentCol('TransactionAmount');
     $msg['log_id'] = $csv->currentCol('SellerReferenceId');
     $msg['payment_method'] = 'amazon';
     $this->fileData[] = $msg;
 }
 /**
  * @param array $message A message from donation queues
  * @param int $default Value to return when message has no dates
  * @return int The unix timestamp at which the message was originally
  *  enqueued, or $default if no date information exists
  */
 public static function getOriginalDateOrDefault($message, $default = 0)
 {
     // This is the actual queued time
     if (isset($message['source_enqueued_time'])) {
         // This is only ever set to the numeric timestamp
         return $message['source_enqueued_time'];
     }
     // Message missing source field might still have a date
     if (isset($message['date'])) {
         // This field is sometimes not a timestamp
         // FIXME: normalize PayPal recurring before queueing!
         if (is_integer($message['date'])) {
             return $message['date'];
         }
         // Try parsing non-numeric things
         $parsedTimestamp = UtcDate::getUtcTimestamp($message['date']);
         if (!is_null($parsedTimestamp)) {
             return $parsedTimestamp;
         }
     }
     return $default;
 }
 public function testReportAvailable()
 {
     $filename = 'settlement_detail_report_2016_10_13.csv';
     $account = 'WikimediaTest';
     $url = "https://example.com/reports/download/MerchantAccount/{$account}/{$filename}";
     $reportAvailable = new ReportAvailable();
     $reportAvailable->correlationId = 'adyen-' . mt_rand();
     $reportAvailable->merchantAccountCode = $account;
     $reportAvailable->merchantReference = mt_rand();
     $reportAvailable->pspReference = $filename;
     $reportAvailable->reason = $url;
     $reportAvailable->eventDate = '2016-10-14T12:06:20.496+02:00';
     $reportAvailable->runActionChain();
     $job = $this->jobQueue->pop();
     $now = UtcDate::getUtcTimestamp();
     $diff = abs($job['source_enqueued_time']) - $now;
     $this->assertTrue($diff < 60, 'Odd enqueued time');
     $unsetFields = array('source_enqueued_time', 'source_host', 'source_run_id', 'source_version', 'propertiesExcludedFromExport', 'propertiesExportedAsKeys');
     foreach ($unsetFields as $fieldName) {
         unset($job[$fieldName]);
     }
     $expected = array('php-message-class' => 'SmashPig\\PaymentProviders\\Adyen\\Jobs\\DownloadReportJob', 'reportUrl' => $url, 'account' => $account, 'source_name' => 'SmashPig', 'source_type' => 'listener', 'correlationId' => '', 'gateway' => 'adyen');
     $this->assertEquals($expected, $job);
 }
 protected function getDate($row)
 {
     $local = $row['Creation Date'];
     $zone = $row['TimeZone'];
     return UtcDate::getUtcTimestamp($local, $zone);
 }
 /**
  * Using an AtomicReadBuffer implementation for the backend means that
  * if this throws an exception, the message will remain on the queue.
  *
  * @param array $message
  * @param Exception $ex
  */
 protected function handleError($message, Exception $ex)
 {
     if ($ex instanceof RetryableException) {
         $now = UtcDate::getUtcTimestamp();
         if (!isset($message['source_enqueued_time'])) {
             $message['source_enqueued_time'] = UtcDate::getUtcTimestamp();
         }
         $expirationDate = $message['source_enqueued_time'] + Configuration::getDefaultConfig()->val('requeue-max-age');
         if ($now < $expirationDate) {
             $retryDate = $now + Configuration::getDefaultConfig()->val('requeue-delay');
             $this->sendToDamagedStore($message, $ex, $retryDate);
             return;
         }
     }
     $this->sendToDamagedStore($message, $ex);
 }
 protected function parseDonation(array $row, array &$msg)
 {
     $msg['contribution_tracking_id'] = $this->getContributionTrackingId($row['Invoice']);
     $msg['country'] = $row['Country'];
     $msg['currency'] = $row['currency'];
     $msg['email'] = $row['User Mail'];
     $msg['settled_fee'] = $row['Fee'];
     // settled_fee since it's given in USD
     $msg['gateway_txn_id'] = $row['Reference'];
     $msg['log_id'] = $row['Invoice'];
     list($method, $submethod) = ReferenceData::decodePaymentMethod($row['Payment Method Type'], $row['Payment Method']);
     $msg['payment_method'] = $method;
     $msg['payment_submethod'] = $submethod;
     if ($row['Settlement date']) {
         $msg['settled_date'] = UtcDate::getUtcTimestamp($row['Settlement date']);
     }
     if ($row['Amount (USD)']) {
         $msg['settled_currency'] = 'USD';
         $msg['settled_gross'] = $row['Amount (USD)'];
     }
 }