I am working on Payment integration.
This is my model class
Model: PayData:
{
protected $fillable = [
'key',
'email',
'amount',
'ref',
'mobileNumber'
];
}
I have some plain PHP Code I want to convert into Laravel. I want to create controller and view with the plain PHP Code:
Plain PHP Code below:
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 function
<!-- place below the html form -->
<script>
function payWithStack(){
var handler = PaystackPop.setup({
key: 'paste your key here',
email: 'customer@email.com',
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>
initialize.php
<?php
$curl = curl_init();
$email = "your@email.com";
$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']);
callback.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>"
}
How do I achieve these plain PHP code to Laravel
</div>
Oke first: Take your time to learn and understand the Laravel framework.
Laravel is a PHP framework, this means that you don't really convert plain PHP into Laravel PHP. Any plain PHP will work in Laravel, but things sometimes have to be in a certain structure or order.
My suggestion is to start by setting up a laravel project, follow some tutorials on Laracast and watch a few youtube videos about the structure of laravel.
Then:
I hope this helps to get started.
Have fun learning/coding/creating!