Skip to content

Instantly share code, notes, and snippets.

@lokcito
Created July 29, 2020 04:56
Show Gist options
  • Save lokcito/5ebc6f499aba749f8ea9363e0e6cc304 to your computer and use it in GitHub Desktop.
Save lokcito/5ebc6f499aba749f8ea9363e0e6cc304 to your computer and use it in GitHub Desktop.
Culqui Payment With Php Laravel 7 and Guzzle php version 6
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2.5",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0",
"tymon/jwt-auth": "^1.0"
},
"require-dev": {
"facade/ignition": "^2.0",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^4.1",
"phpunit/phpunit": "^8.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Exception;
class PaymentController extends Controller
{
public function tokenize(Request $request) {
$card = $request->input("card");
$year = $request->input("year");
$month = $request->input("month");
$cvv = $request->input("cvv");
$email = $request->input("email");
$body = ["card_number" => $card,
'cvv' => $cvv,
'expiration_month' => $month,
'expiration_year' => $year,
'email' => $email];
$headers = ['Authorization' => 'Bearer pk_test_5rAdQNwzJFpDK4zu',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip, deflate, br',
'decode_content' => false];
$token_for_payment = "---";
try {
$client = new Client();
$response = $client->request('POST',
'https://secure.culqi.com/v2/tokens', [ 'headers' => $headers,
'decode_content' => false,
'json' => $body
]);
$body = $response->getBody();
$data = json_decode($body);
$token_for_payment = $data->id;
}catch(ClientException $ce){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
} catch(BadResponseException $e) {
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}catch(RequestException $re){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}catch(Exception $e){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}
if ( $token_for_payment == "---" ) {
return response()->json([
"status" => false,
"meta" => [
"msg" => "Token no definido."
]
]);
}
$headers = ['Authorization' => 'Bearer sk_test_m39bSIyt4WvilUD1',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip, deflate, br',
'decode_content' => false];
try {
$client = new Client();
$response = $client->request('POST',
'https://api.culqi.com/v2/charges', [ 'headers' => $headers,
'decode_content' => false,
'json' => [
"amount" => "12345",
"currency_code" => "PEN",
"email" => $email,
"source_id" => $token_for_payment
]
]);
$body = $response->getBody();
$data = json_decode($body);
}catch(ClientException $ce){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
} catch(BadResponseException $e) {
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}catch(RequestException $re){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}catch(Exception $e){
$message = $ce->getResponse()->getBody()->getContents();
return response()->json([
"status" => false,
"meta" => [
"msg" => $message
]
]);
}
return response()->json([
"status" => true,
"meta" => [
"msg" => "Pago existoso"
]
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment