Laravel - 将我的纯 PHP 代码转换为 Laravel(控制器和视图)

问题描述 投票:0回答:2

我正在研究支付集成。

这是我的模型课

型号:PayData:

{
    protected $fillable = [
        'key',
        'email',
        'amount',
        'ref',
        'mobileNumber'
        ];
}

我有一些简单的 PHP 代码想要转换为 Laravel。我想用简单的 PHP 代码创建控制器和视图:

下面是简单的 PHP 代码:

The form is shown below:

<form>
  <script src="https://js.stack.co/v1/inline.js"></script>
  <button type="button" onclick="payWithStack()"> Pay </button> 
</form>

payWithStack 功能

<!-- place below the html form -->
<script>
  function payWithStack(){
    var handler = PaystackPop.setup({
      key: 'paste your key here',
      email: '[email protected]',
      amount: 10000,
      ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
      metadata: {
         custom_fields: [
            {
                display_name: "Mobile Number",
                variable_name: "mobile_number",
                value: "+2348012345678"
            }
         ]
      },
      callback: function(response){
          alert('success. transaction ref is ' + response.reference);
      },
      onClose: function(){
          alert('window closed');
      }
    });
    handler.openIframe();
  }
</script>

初始化.php

<?php
$curl = curl_init();

$email = "[email protected]";
$amount = 30000;  //the amount in kobo. This value is actually NGN 300

// url to go to after payment
$callback_url = 'myapp.com/pay/callback.php';  

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.paystack.co/transaction/initialize",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
 'amount'=>$amount,
 'email'=>$email,
 'callback_url' => $callback_url
  ]),
  CURLOPT_HTTPHEADER => [
  "authorization: Bearer sk_test_36658e3260b1d1668b563e6d8268e46ad6da3273", 
//replace this with your own test key
"content-type: application/json",
"cache-control: no-cache"
],
));

$response = curl_exec($curl);
$err = curl_error($curl);

if($err){
// there was an error contacting the Paystack API
die('Curl returned error: ' . $err);
}

$tranx = json_decode($response, true);

if(!$tranx->status){
 // there was an error from the API
  print_r('API returned error: ' . $tranx['message']);
}

// comment out this line if you want to redirect the user to the payment page
print_r($tranx);
  // redirect to page so User can pay
  // uncomment this line to allow the user redirect to the payment page
  header('Location: ' . $tranx['data']['authorization_url']);

回调.php

<?php

$curl = curl_init();
$reference = isset($_GET['reference']) ? $_GET['reference'] : '';
if(!$reference){
  die('No reference supplied');
}

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "accept: application/json",
    "authorization: Bearer sk_test_36658e3260b1d1668b563e6d8268e46ad6da3273",
    "cache-control: no-cache"
  ],
));

$response = curl_exec($curl);
$err = curl_error($curl);

if($err){
    // there was an error contacting the Paystack API
  die('Curl returned error: ' . $err);
}

$tranx = json_decode($response);

if(!$tranx->status){
  // there was an error from the API
  die('API returned error: ' . $tranx->message);
}

if('success' == $tranx->data->status){
  // transaction was successful...
  // please check other things like whether you already gave value for this ref
  // if the email matches the customer who owns the product etc
  // Give value
  echo "<h2>Thank you for making a purchase. Your file has bee sent your email.</h2>"
}

如何将这些纯 PHP 代码实现到 Laravel

php laravel
2个回答
0
投票

首先:花点时间学习和理解 Laravel 框架。

Laravel 是一个 PHP 框架,这意味着您并没有真正将普通 PHP 转换为 Laravel PHP。任何普通的 PHP 都可以在 Laravel 中运行,但有时必须采用特定的结构或顺序。

我的建议是从建立一个 Laravel 项目开始,遵循 Laracast 上的一些教程并观看一些有关 Laravel 结构的 YouTube 视频。

然后:

  1. 在资源文件夹中为表单和javascript创建一个新视图。(或在视图中引用js文件)
  2. 在 app/http/controllers 文件夹中创建一个控制器。 initialize.php 中的所有代码都可以放入您可以命名为 initalize() 的函数中,而callback.php 中的代码则可以放入您可以命名为callback() 的函数中。
  3. 在routes/web.php文件中至少创建2条路由,1条用于初始化函数,1条用于回调函数。
  4. 不确定堆栈如何工作,但我认为您可以将 2 条路由或至少回调路由添加到堆栈(或其他支付提供商)配置中。

我希望这有助于开始。

祝学习/编码/创造愉快!


0
投票

函数 getJogosByHorario() { $query = "SELECT *, cd.Tipo 作为 TipoCidade, jg.id 作为 id_jogo FROM

bichojogo
jg INNER JOIN usuarios bc ON bc.id = jg.idBanca INNER JOIN cidades cd ON bc.cidadetitle = cd.title 哪里
bichohorario
= :Horario ANDcast(jg.dataCriacao as DATE) = :DataJogo AND jg.Status = '0' AND jg.local = :local 并且 lixeira 为空”; $prepare = Conn::getConn()->prepare($query); $准备->执行( [ 'DataJogo' => inputPost('DataJogo'), '本地' => inputPost('本地'), 'Horario' => inputPost('Horario') ] );

return $prepare->fetchAll();

}

© www.soinside.com 2019 - 2024. All rights reserved.