omise / omise-prestashop

Omise PrestaShop Plugin
https://docs.opn.ooo/prestashop-plugin
MIT License
4 stars 7 forks source link

Create an Omise charge #19

Closed guzzilar closed 7 years ago

guzzilar commented 7 years ago

Note1, original PR was made by @nimid at PR #14, #17, #16. This PR just did (and aimed to) rearrange those 3 PRs to make it easy to review by merging all those 3 PRs to only one, which is, this branch.

Note2, this branch contains the same code at PR #16 (except, this branch already rebased with develop.

1. Objective

Create an Omise charge by using the Omise card token that submitted from the client side.

Related information: Related issue: - Related ticket: - Required pull request: #13

2. Description of change

3. Quality assurance

Environments:

Details:

The screenshot below shows the result page display the successful payment.

screenshot-localhost 1616 2016-12-26 14-19-39

The screenshot below shows charge detail at Omise dashboard. This charge was created from the above checkout.

screenshot-dashboard omise co 2016-12-26 14-21-32

4. Impact of the change

-

5. Priority of change

Normal

6. Additional notes

For the next steps of charge process, such as record the order information to PrestaShop or display the proper result page, it will be developed for next pull request.

guzzilar commented 7 years ago

@nimid I checked at PR #21. I think that design is a bit complex,

nimid commented 7 years ago

@guzzilar

How about an alternative design below?

class OmisePaymentModuleFrontController extends OmiseBasePaymentModuleFrontController
{
    public function postProcess()
    {
        try {
            $this->charge = $this->omise_charge->create(Tools::getValue('omise_card_token'));
        } catch (Exception $e) {
            $this->error_message = $e->getMessage();
            return;
        }

        if ($this->charge->isFailed()) {
            $this->error_message = $this->charge->getErrorMessage();
            return;
        }

        $this->payment_order->save();

        $this->setRedirectAfter('index.php?controller=order-confirmation' .
            '&id_cart=' . $this->context->cart->id .
            '&id_module=' . $this->module->id .
            '&id_order=' . $this->module->currentOrder .
            '&key=' . $this->context->customer->secure_key
        );
    }
}
class OmiseThreeDomainSecurePaymentModuleFrontController extends OmiseBasePaymentModuleFrontController
{
    public function postProcess()
    {
        $this->payment_order->saveAsProcessing();

        try {
            $this->charge = $this->omise_charge->createThreeDomainSecure(Tools::getValue('omise_card_token'));
        } catch (Exception $e) {
            $this->error_message = $e->getMessage();
            return;
        }

        if ($this->charge->isFailed()) {
            $this->error_message = $this->charge->getErrorMessage();
            return;
        }

        $this->setRedirectAfter($this->charge->getAuthorizeUri());
    }
}
abstract class OmiseBasePaymentModuleFrontController extends ModuleFrontController
{
    protected $charge;
    public $display_column_left = false;
    protected $error_message;
    protected $omise_charge;
    protected $payment_order;
    protected $setting;

    public function __construct()
    {
        parent::__construct();

        $this->omise_charge = new Charge();
        $this->payment_order = new PaymentOrder();
        $this->setting = new Setting();
    }

    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign('error_message', $this->error_message);
        $this->setTemplate('payment-error.tpl');
    }

    public function redirect()
    {
        Tools::redirect($this->redirect_after);
    }
}
class Charge
{
    ...

    public function create($card_token)
    {
        $charge_request = array(
            'amount' => $this->getAmount(),
            'card' => $card_token,
            'capture' => $this->getCapture(),
            'currency' => $this->getCurrencyCode(),
            'description' => $this->getChargeDescription(),
        );

        $this->charge_response = OmiseCharge::create($charge_request, '', $this->getSecretKey());

        return $this;
    }

    public function createInternetBanking()
    {
        $charge_request = array(
            'amount' => $this->getAmount(),
            'capture' => $this->getCapture(),
            'currency' => $this->getCurrencyCode(),
            'description' => $this->getChargeDescription(),
            'offsite' => 'internet_banking_scb',
            'return_uri' => $this->getReturnUri(),
        );

        $this->charge_response = OmiseCharge::create($charge_request, '', $this->getSecretKey());

        return $this;
    }

    public function createThreeDomainSecure($card_token)
    {
        $charge_request = array(
            'amount' => $this->getAmount(),
            'card' => $card_token,
            'capture' => $this->getCapture(),
            'currency' => $this->getCurrencyCode(),
            'description' => $this->getChargeDescription(),
            'return_uri' => $this->getReturnUri(),
        );

        $this->charge_response = OmiseCharge::create($charge_request, '', $this->getSecretKey());

        return $this;
    }

...

}

Do you have any reason behind that you chose OmiseThreeDomainSecure instead of OmiseThreeDSecure?

I found the full word for explanation can have more clearly understandable. If a word Domain is long name, the abbreviation is preferred but for more 5 characters (omain), it can help in part of understandable.

In the camel case coding convention, when D beside Secure, it may make the confusion.

guzzilar commented 7 years ago

@nimid Note, I reviewed without checking another PR, so, some of my comments may already been solved in somewhere else in some PRs. (And also, we may have already talked about it in some part of my comments. Feel free to point it out if comments below were mentioned/duplicated in some place even in this PR)

Here we go.

nimid commented 7 years ago

@guzzilar https://github.com/omise/omise-prestashop/pull/19#issuecomment-281457688

Docblock(s) are missing.

Which DocBlocks tag is required?


If classes/charge.php is a controller, I think it better to keep it in controllers folder.

It has been changed.


classes/charge.php shall we rename it or make it more unique name?

OmiseCharge has already defined.

How about OmiseChargeClass?


but classes/charge.php, getCapture() looks unnecessary and it makes function name looks weird.

Done. 672b59305ef127367c4c5fe7c4673e3ba8a7c56d


I think it's gonna be easier for test.

Can you show the test, compare and clarify how it easier?


Vertical alignment would make your code much easier to read.

It is not my code. It is not your code. It is not our code.

It belongs to Omise.

It is not just change the ownership words. It is the attitude in working. It is a way how to behave with it. It is the attitude when communicate with co-worker and people.

I found you have shared a code review guideline. In the guideline, it clarified Avoid selective ownership of code. ("mine", "not mine", "yours").

Is vertical alignment personal preference?

If it is not, can you show the publicly accepted standard for reference that this project should be consistent?

nimid commented 7 years ago

The source code in this pull request has been changed and combined to merge together with the pull request #21.