如何在 Joomla 3.x 组件中 post 条带化支付表单

How to post Stripe payment form in Joomla 3.x component

我正在尝试在名为 com_swa 的自定义 Joomla 3.x 组件中实现 Stripe 支付按钮。我正在尝试通过 Stripe's checkout. I've been following this example from the Stripe documentation 执行此操作,但我在执行 POST.

时遇到了问题

这是我目前的代码:

com_swa/views/ticketpurchase/tmpl/default.php:

<form action="<?php echo JUri::root() .'index.php?option=com_swa&task=ticketpurchase.http_post' ?>" method="POST" >
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_eXamPleK3y"
        data-amount="<?php echo $item->price * 100 ?>"
        data-currency="GBP"
        data-label="Buy now!"
        data-name='SWA'
        data-description="Test description"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-locale="auto"
        data-zip-code="true"
        data-email="<?php echo $this->user->email ?>" >
    </script>
</form>

com_swa/controllers/ticketpurchase.php:

private function http_post() {

    JLog::add( "http_post called", JLog::INFO, 'com_swa' );

    $token  = $_POST['stripeToken'];
    $ammount = $_POST['data-amount'];
    $currency = $_POST['data-currency'];

    var_dump("Token: " . $token);

    $customer = \Stripe\Customer::create(array(
        'email' => $email,
        'source'  => $token
    ));

    $charge = \Stripe\Charge::create(array(
        'customer' => $customer->id,
        'amount'   => $ammount,
        'currency' => $currency
    ));

    var_dump("Charge: " . $charge);
    JLog::add('Charge: ' . $charge, JLog::INFO, 'com_swa');

}

但这只会导致 500 - View not found [name, type, prefix]: home, html, swaView 错误。我检查了日志文件,没有日志表明 http_post() 方法已被调用。

现在我只想 var_dump 或记录一些变量,但将来我想根据 $charge 的内容重定向到不同的视图。

如有任何帮助,我们将不胜感激!

所以我睡了一觉,回到了这个新面孔,意识到我做错了几件事。我会在这里列出它们,以防其他人遇到同样的问题。

  1. 视图中的表单没有隐藏输入 optiontask
  2. 我试图在控制器中调用的函数是私有的,而它本应是 public。

这是我的代码现在的样子:

com_swa/views/ticketpurchase/tmpl/default.php:

<form action="<?php echo JRoute::_('index.php?option=com_swa&task=ticketpurchase'); ?>" method="POST" >
    <input type="hidden" name="option" value="com_swa" />
    <input type="hidden" name="task" value="ticketpurchase.http_post" />
    <input type="hidden" name="ticketId" value="<?php echo $item->id ?>">
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_tDaDvORCWuyXb0VRIHtMStDR"
        data-amount="<?php echo $item->price * 100 ?>"
        data-currency="GBP"
        data-label="Buy now!"
        data-name="SWA"
        data-description="<?php echo $item->event_name . ' - ' . $item->ticket_name; ?>"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-locale="auto"
        data-zip-code="true"
        data-email="<?php echo $this->user->email ?>" >
    </script>
</form>

com_swa/controllers/ticketpurchase.php:

public function http_post() {

    JLog::add( "http_post called", JLog::INFO, 'com_swa' );

    $token = $this->input->getString('stripeToken');
    $email = $this->input->getString('stripeEmail');
    $ticketId = $this->input->getString('ticketId');

    $model = $this->getModel('ticketpurchase');
    $tickets = $model->getItems();
    $member = $model->getMember();

    $ticket = null;
    foreach ($tickets as $t) {
        if ($t->id == $ticketId) {
            $ticket = $t;
            break;
        }
    }

    if ($ticket != null) {
        try {
            $charge = \Stripe\Charge::create(array(
                'description' => $ticket->event_name . ' - ' . $ticket->ticket_name,
                'amount' => $ticket->price * 100,
                'currency' => 'GBP',
                'receipt_email' => $email,
                'source' => $token,
                'metadata' => array(
                    'ticket_id' => $ticket->id,
                    'member_id' => $member->id
                    )
            ));
            var_dump("Charge: " . $charge);
            JLog::add('Charge: ' . $charge, JLog::INFO, 'com_swa');

        } catch (\Stripe\Error\Base $e) {
            var_dump($e);
        }
    } else {
        // can't find ticketId in items.
    }
    die();
}