public function setUp()
 {
     EEM_Payment::reset();
     EEM_Transaction::reset();
     //		EEM_Payment_Method::reset();
     parent::setUp();
 }
 public function test_just_approved()
 {
     $p1 = EE_Payment::new_instance();
     $this->assertFalse($p1->just_approved());
     $p1->set_status(EEM_Payment::status_id_approved);
     $this->assertTrue($p1->just_approved());
     $id = $p1->save();
     $this->assertTrue($p1->just_approved());
     EEM_Payment::reset();
     //now try with a payment that began as approved
     //note that we've reset EEM_payment so this is just like
     //it had been created on a previous request
     $p2 = EEM_Payment::instance()->get_one_by_ID($id);
     $this->assertFalse($p2->just_approved());
     $p2->set_status(EEM_Payment::status_id_pending);
     $p2->save();
     EEM_Payment::reset();
     //again, pretend this next part is a subsequent request
     $p3 = EEM_Payment::instance()->get_one_by_ID($id);
     $this->assertFalse($p3->just_approved());
     $p3->set_status(EEM_Payment::status_id_approved);
     $this->assertTrue($p3->just_approved());
 }
 public function test_process_payment__offsite__declined_then_approved()
 {
     /** @type EE_Payment_Method $pm */
     $pm = $this->new_model_obj_with_dependencies('Payment_Method', array('PMD_type' => 'Mock_Offsite'));
     $transaction = $this->_new_typical_transaction();
     global $wp_actions;
     EE_Registry::instance()->load_helper('Array');
     $successful_payment_actions = EEH_Array::is_set($wp_actions, 'AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 0);
     /** @type EE_Payment_Processor $payment_processor */
     $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
     $payment = $payment_processor->process_payment($pm, $transaction, NULL, NULL, 'success', 'CART', TRUE, TRUE);
     $this->assertInstanceOf('EE_Payment', $payment);
     //verify it's already been saved
     $this->assertNotEquals(0, $payment->ID());
     //assert that the payment still has its default status
     $this->assertEquals(EEM_Payment::instance()->field_settings_for('STS_ID')->get_default_value(), $payment->status());
     $this->assertEquals(EEM_Payment::instance()->field_settings_for('STS_ID')->get_default_value(), $this->_get_payment_status_in_db($payment));
     //assert that the we haven't notified of successful payment JUST yet...
     $this->assertEquals($successful_payment_actions, EEH_Array::is_set($wp_actions, 'AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 0));
     //PENDING IPN
     $payment = $payment_processor->process_ipn(array('status' => EEM_Payment::status_id_pending, 'gateway_txn_id' => $payment->txn_id_chq_nmbr()), $transaction, $pm);
     $this->assertEquals(EEM_Payment::status_id_pending, $payment->status());
     $this->assertEquals(EEM_Payment::status_id_pending, $this->_get_payment_status_in_db($payment));
     //and the payment-approved action should have NOT been triggered
     $this->assertEquals($successful_payment_actions, EEH_Array::is_set($wp_actions, 'AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 0));
     //APPROVED IPN
     $payment = $payment_processor->process_ipn(array('status' => EEM_Payment::status_id_approved, 'gateway_txn_id' => $payment->txn_id_chq_nmbr()), $transaction, $pm);
     //payment should be what the gateway set it to be, which was approved
     $this->assertEquals(EEM_Payment::status_id_approved, $payment->status());
     $this->assertEquals(EEM_Payment::status_id_approved, $this->_get_payment_status_in_db($payment));
     //and the payment-approved action should have been triggered
     $this->assertEquals($successful_payment_actions + 1, EEH_Array::is_set($wp_actions, 'AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 0));
     //DUPLICATE SUCCESS IPN
     //for this, we need to reset payment model so we fetch a NEW payment object, instead of reusing the old
     //and because the payment method caches a payment method type which caches a gateway which caches the payment model,
     //we also need to reset the payment method
     EEM_Payment::reset();
     $pm = EEM_Payment_Method::reset()->get_one_by_ID($pm->ID());
     $payment = $payment_processor->process_ipn(array('status' => EEM_Payment::status_id_approved, 'gateway_txn_id' => $payment->txn_id_chq_nmbr()), $transaction, $pm);
     //payment should be what the gateway set it to be, which was failed
     $this->assertEquals(EEM_Payment::status_id_approved, $payment->status());
     //and the payment-approved action should have NOT been triggered this time because it's a duplicate
     $this->assertEquals($successful_payment_actions + 1, EEH_Array::is_set($wp_actions, 'AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 0));
 }
 /**
  *
  * @group 7151
  */
 function test_refresh_entity_map_from_db__serialized_object()
 {
     //get an object purposefully out-of-sync with the DB
     //call this and make sure it's wiped clean and
     $p = $this->new_model_obj_with_dependencies('Payment', array('PAY_amount' => 25));
     $p->save();
     $this->assertEquals($p, EEM_Payment::instance()->get_from_entity_map($p->ID()));
     //ok now remember that ID, serialize the payment, and otherwise remove the object
     $p_id = $p->ID();
     $p_serialized = serialize($p);
     unset($p);
     EEM_Payment::reset();
     //now manually update it in teh DB, but not the model object
     global $wpdb;
     $affected = $wpdb->query($wpdb->prepare("update {$wpdb->prefix}esp_payment SET PAY_amount = 100, TXN_ID = 0 WHERE PAY_ID = %d", $p_id));
     $this->assertEquals(1, $affected);
     //now unserialize it and verify it's what we thought it was
     $p_unserialized = unserialize($p_serialized);
     $this->assertEquals($p_id, $p_unserialized->ID());
     $this->assertEquals(25, $p_unserialized->get('PAY_amount'));
     //and when it's refreshed, its PAY_amount should be updated too and it should no longer have any transaction cached or even findable
     $p_unserialized = EEM_Payment::instance()->refresh_entity_map_from_db($p_unserialized->ID());
     $this->assertEquals(100, $p_unserialized->get('PAY_amount'));
     $this->assertEquals(0, $p_unserialized->get('TXN_ID'));
     $this->assertEquals(array(), $p_unserialized->get_all_from_cache('Transaction'));
     $this->assertEquals(null, $p_unserialized->transaction());
 }