public function testNotifyEndpoint()
 {
     $this->setMockHttpResponse('PaymentExpress/Mock/PxPayCompletePurchaseSuccess.txt');
     //mock the 'result' get variable into the current request
     $this->getHttpRequest()->query->replace(array('result' => 'abc123'));
     //mimic a redirect or request from offsite gateway
     $response = $this->get("paymentendpoint/UNIQUEHASH23q5123tqasdf/notify");
     $message = GatewayMessage::get()->filter('Identifier', 'UNIQUEHASH23q5123tqasdf')->first();
     //redirect works
     $this->assertNull($response->getHeader('Location'));
     $payment = $message->Payment();
     $this->assertDOSContains(array(array('ClassName' => 'PurchaseRequest'), array('ClassName' => 'PurchaseRedirectResponse'), array('ClassName' => 'CompletePurchaseRequest'), array('ClassName' => 'PurchasedResponse')), $payment->Messages());
 }
 public function getDetailsForDataGrid($separator = ' - ')
 {
     $details = [];
     if ($this->Note) {
         $details[] = $this->Note;
     }
     switch ($this->Status) {
         case 'Updated':
             $log = $this->ChangeLog;
             $separator = '<br/>';
             if (isset($log['OrderItem'])) {
                 if (is_array($log['OrderItem']) && isset($log['OrderItem']['Quantity']) && isset($log['Quantity'])) {
                     if (isset($log['OrderItem']['_brandnew'])) {
                         $details[] = sprintf('Added %s of %s', $log['Quantity'], isset($log['OrderItem']['Title']) ? $log['OrderItem']['Title'] : 'item', $log['Quantity']);
                     } elseif ($log['Quantity']) {
                         $details[] = sprintf('Set %s to %s', isset($log['OrderItem']['Title']) ? $log['OrderItem']['Title'] : 'item', $log['Quantity'], $log['Quantity']);
                     } else {
                         $details[] = sprintf('Removed %s', isset($log['OrderItem']['Title']) ? $log['OrderItem']['Title'] : 'item');
                     }
                 } elseif (is_object($log['OrderItem']) && $log['OrderItem'] instanceof OrderItem && isset($log['Quantity'])) {
                     if (isset($log['OrderItem']->_brandnew)) {
                         $details[] = sprintf('Added %s of %s', $log['Quantity'], $log['OrderItem']->Buyable()->Title);
                     } elseif ($log['Quantity']) {
                         $details[] = sprintf('Set %s to %s', $log['OrderItem']->Buyable()->Title, $log['Quantity']);
                     } else {
                         $details[] = sprintf('Removed %s', $log['OrderItem']->Buyable()->Title);
                     }
                 }
             }
             if (isset($log['ShippingAddress']) && $log['ShippingAddress']) {
                 $details[] = 'Ship to: ' . implode(', ', array_filter([$log['ShippingAddress']->Name, $log['ShippingAddress']->toString()]));
                 if (!$this->Order()->SeparateBillingAddress) {
                     $details[] = 'Bill to: ' . implode(', ', array_filter([$log['ShippingAddress']->Name, $log['ShippingAddress']->toString()]));
                 }
             }
             if (isset($log['BillingAddress']) && $log['BillingAddress']) {
                 $details[] = 'Bill to: ' . implode(', ', array_filter([$log['BillingAddress']->Name, $log['BillingAddress']->toString()]));
             }
             if (isset($log['Member'])) {
                 $details[] = 'Member: ' . $log['Member']->Name;
             }
             $allowed = ['IPAddress', 'Reference', 'SeparateBillingAddress', 'Notes', 'Referrer'];
             if (!$this->Order()->IsCart()) {
                 $allowed[] = 'Total';
             }
             $log = array_intersect_key($log, array_flip($allowed));
             if (!empty($log)) {
                 foreach ($log as $field => $trans) {
                     if (is_array($trans) && array_key_exists('before', $trans)) {
                         $details[] = $this->Order()->fieldLabel($field) . ' changed from ' . ($trans['before'] ?: '<em class="orderStatusLog-detail--none">none</em>') . ' to ' . $trans['after'];
                     } elseif (is_string($trans)) {
                         $details[] = $this->Order()->fieldLabel($field) . ': ' . $trans;
                     }
                 }
             }
             break;
         case 'Processing':
             if (($component = $this->Order()->has_many('Payments')) && count($component) && ($lastPayment = $this->Order()->Payments()->filter(['Created:LessThan' => $this->Created])->first())) {
                 $details[] = 'Via ' . GatewayInfo::nice_title($lastPayment->Gateway);
                 $details[] = 'Charging ' . GatewayInfo::nice_title($lastPayment->obj('Money')->Nice());
                 if ($gatewayMessage = GatewayMessage::get()->filter(['PaymentID' => $lastPayment->ID, 'Reference:not' => ''])->first()) {
                     if ($gatewayMessage->Reference) {
                         $details[] = 'Reference: ' . $gatewayMessage->Reference;
                     }
                 }
             }
             break;
         case 'Paid':
             if (($component = $this->Order()->has_many('Payments')) && count($component) && ($lastPayment = $this->Order()->Payments()->filter(['Status' => 'Captured', 'Created:LessThan' => $this->Created])->first())) {
                 $details[] = 'Via ' . GatewayInfo::nice_title($lastPayment->Gateway);
                 $details[] = 'Charged ' . GatewayInfo::nice_title($lastPayment->obj('Money')->Nice());
                 if ($gatewayMessage = GatewayMessage::get()->filter(['PaymentID' => $lastPayment->ID, 'Reference:not' => ''])->first()) {
                     if ($gatewayMessage->Reference) {
                         $details[] = 'Reference: ' . $gatewayMessage->Reference;
                     }
                 }
             }
             break;
     }
     $details = [count($details) ? $separator . implode($separator, $details) : ''];
     if ($this->Sent) {
         $details[] = 'Notified customer on: ' . $this->Sent;
     }
     if ($this->Author()->exists()) {
         $details[] = 'Author: ' . $this->Author()->Name;
     }
     return implode('<br/>', $details);
 }
 public function testFailedOffSiteCompletePurchase()
 {
     $this->setMockHttpResponse('PaymentExpress/Mock/PxPayCompletePurchaseFailure.txt');
     //mock the 'result' get variable into the current request
     $this->getHttpRequest()->query->replace(array('result' => 'abc123'));
     //mimic a redirect or request from offsite gateway
     $response = $this->get("paymentendpoint/UNIQUEHASH23q5123tqasdf/complete");
     $message = GatewayMessage::get()->filter('Identifier', 'UNIQUEHASH23q5123tqasdf')->first();
     //redirect works
     $headers = $response->getHeaders();
     $this->assertEquals(Director::baseURL() . "shop/incomplete", $headers['Location'], "redirected to shop/incomplete");
     $payment = $message->Payment();
     $this->assertDOSContains(array(array('ClassName' => 'PurchaseRequest'), array('ClassName' => 'PurchaseRedirectResponse'), array('ClassName' => 'CompletePurchaseRequest'), array('ClassName' => 'CompletePurchaseError')), $payment->Messages());
     $this->assertEquals("Void", $payment->Status, "Payment has been void");
 }
 /**
  * Get the message storing the identifier for this payment
  * @return GatewayMessage the transaction
  */
 private function getRequestMessage()
 {
     return GatewayMessage::get()->filter('Identifier', $this->request->param('Identifier'))->first();
 }