function testEcommerceRoleCreateOrMerge()
 {
     $member = $this->objFromFixture('Member', 'member1');
     $this->session()->inst_set('loggedInAs', $member->ID);
     $uniqueField = Member::get_unique_identifier_field();
     $this->assertEquals('*****@*****.**', $member->getField($uniqueField), 'The unique field is the email address');
     $this->assertEquals('US', $member->getField('Country'), 'The country is US');
     /* Change the email address to a new one (doesn't exist) */
     $member = EcommerceRole::createOrMerge(array('Country' => 'AU', $uniqueField => '*****@*****.**'));
     $this->assertType('object', $member, 'The member is an object, not FALSE');
     $this->assertEquals('*****@*****.**', $member->getField($uniqueField), 'The unique field is changed (no member with that email)');
     $this->assertEquals('AU', $member->getField('Country'), 'The member country is now AU');
     /* Change the data (update existing record - logged in member owns this email) */
     $member = EcommerceRole::createOrMerge(array('Country' => 'NZ', $uniqueField => '*****@*****.**'));
     $this->assertType('object', $member, 'The member is an object, not FALSE');
     $this->assertEquals('*****@*****.**', $member->getField($uniqueField), 'The unique field is the same (updated own record)');
     $this->assertEquals('NZ', $member->getField('Country'), 'The member country is now NZ');
     /* Change the email address to one exists (we should not get a member back when trying to merge!) */
     $member = EcommerceRole::createOrMerge(array('Country' => 'US', $uniqueField => '*****@*****.**'));
     $this->assertFalse($member, 'No member returned because we tried to merge an email that already exists in the DB');
     /* Log the member out */
     $this->session()->inst_set('loggedInAs', null);
     /* Non-logged in site user creating a new member with email that doesn't exist */
     $member = EcommerceRole::createOrMerge(array('Country' => 'NZ', $uniqueField => '*****@*****.**'));
     $this->assertType('object', $member, 'The member is an object, not FALSE');
     $this->assertEquals('*****@*****.**', $member->getField($uniqueField));
     $this->assertEquals('NZ', $member->getField('Country'), 'The member country is NZ');
     /* Non-logged in site user creating a member with email that DOES exist */
     $member = EcommerceRole::createOrMerge(array('Country' => 'AU', $uniqueField => '*****@*****.**'));
     $this->assertFalse($member, 'The new user tried to create a member with an email that already exists, FALSE returned');
     $member = EcommerceRole::createOrMerge(array('Country' => 'AU', $uniqueField => '*****@*****.**'));
     $this->assertFalse($member, 'Even if the email has a different case, FALSE is still returned');
 }
Exemplo n.º 2
0
    /** 
     * Process the items in the shopping cart from session,
     * creating a new {@link Order} record, and updating the
     * customer's details {@link Member} record.
     * 
     * {@link Payment} instance is created, linked to the order,
     * and payment is processed {@link Payment::processPayment()}
     * 
     * @param array $data Form request data submitted from OrderForm
     * @param Form $form Form object for this action
     * @param HTTPRequest $request Request object for this action
     */
    function processOrder($data, $form, $request)
    {
        $paymentClass = !empty($data['PaymentMethod']) ? $data['PaymentMethod'] : null;
        $payment = class_exists($paymentClass) ? new $paymentClass() : null;
        if (!($payment && $payment instanceof Payment)) {
            user_error(get_class($payment) . ' is not a valid Payment object!', E_USER_ERROR);
        }
        if (!ShoppingCart::has_items()) {
            $form->sessionMessage('Please add some items to your cart', 'bad');
            Director::redirectBack();
            return false;
        }
        // Create new OR update logged in {@link Member} record
        $member = EcommerceRole::createOrMerge($data);
        if (!$member) {
            $form->sessionMessage(_t('OrderForm.MEMBEREXISTS', 'Sorry, a member already exists with that email address.
					If this is your email address, please log in first before placing your order.'), 'bad');
            Director::redirectBack();
            return false;
        }
        $member->write();
        $member->logIn();
        // Create new Order from shopping cart, discard cart contents in session
        $order = ShoppingCart::save_current_order();
        ShoppingCart::clear();
        // Write new record {@link Order} to database
        $form->saveInto($order);
        $order->write();
        // Save payment data from form and process payment
        $form->saveInto($payment);
        $payment->OrderID = $order->ID;
        $payment->Amount = $order->Total();
        $payment->write();
        // Process payment, get the result back
        $result = $payment->processPayment($data, $form);
        // isProcessing(): Long payment process redirected to another website (PayPal, Worldpay)
        if ($result->isProcessing()) {
            return $result->getValue();
        }
        if ($result->isSuccess()) {
            $order->sendReceipt();
        }
        Director::redirect($order->Link());
        return true;
    }