I am working with stripe test payment and want to store my order in db also but facing an error i.e.
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
My controller is Checkout function
public function postCheckOut(Request $request){
if(!Session::has('cart')){
return redirect()->route('shop.cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
/* Stripe Test Api key */
Stripe::setApiKey('-------');
try{
$charge = \Stripe\Charge::create(array(
"amount" => $cart->totalPrice * 100,
"currency" => "usd",
// "source" => "$request->input('stripeToken')", // obtained with Stripe.js
'card' => array(
'number' => $request->get("cnumber"),
'exp_month' => $request->get("exp-month"),
'exp_year' => $request->get("exp-year"),
'cvc' => $request->get("cvc"),
),
"description" => "product purchased"
));
/* Saving order data after purchasing done */
$order = new Order();
$order->user_id = Auth::id();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
$order->payment_id = $charge->id;
// dd($order);exit;
/* Saving order data by orm relation calling login user then its order function and then save orders */
Auth::user()->orders->save($order);
} catch (\Exception $e){
return redirect()->route('checkout')->with('error', $e->getMessage());
}
Session::forget('cart');
return redirect()->route('product.index')->with('success', 'Product purchased');
}
please guide what i am doing wrong. I check almost what i can.
Because you are already getting value for foreign key Try directly storing it like this
$order->save();
else you can also try this
Auth::user()->orders()->save($order);
instead of
Auth::user()->orders->save($order);
Refer to docs
Related
I'm trying to handle monthly subscription with stripe :
I create a Payment Controller with actions :
create-checkout-session Action :
/**
* #Route ("/create-checkout-session", name="checkout")
*/
public function checkout(Request $request)
{
$data = json_decode($request->getContent());
\Stripe\Stripe::setApiKey('sk_test_...');
try {
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price' => $data->priceId,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
'mode' => 'subscription',
'success_url' => $this->generateUrl('success', ["session_id" => "{CHECKOUT_SESSION_ID}"], UrlGeneratorInterface::ABSOLUTE_URL),
'cancel_url' => $this->generateUrl('error', [], UrlGeneratorInterface::ABSOLUTE_URL),
]);
} catch (\Exception $e) {
return new JsonResponse(["error" => ['message' => $e->getMessage()]]);
}
return new JsonResponse(["sessionId" => $checkout_session['id']]);
}
success Action :
/**
* #Route(
* "/{_locale}/success/{session_id}",
* name="success",
* defaults={"_locale"="en"},
* requirements={"_locale"="en|fr"}
* )
* #param Request $request
* #return Response
*/
public function success(Request $request): Response
{
$locale = $request->getLocale();
// Get current user
$user = $this->getUser();
\Stripe\Stripe::setApiKey('sk_test_...');
$session = \Stripe\Checkout\Session::retrieve($request->get('session_id'));
$customer = \Stripe\Customer::retrieve($session->customer);
return $this->render('payment/success.html.twig', [
'current_language' => $locale,
'user' => $user,
'session' => $session,
'customer' => $customer,
]);
}
And i created a script :
// Create an instance of the Stripe object with your publishable API key
const stripe = Stripe('pk_test_..');
const createCheckoutSession = function (priceId) {
return fetch("/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function (result) {
return result.json();
});
};
$(".subscription-btn").click(function (e) {
e.preventDefault();
let PRICE_ID = $(this).data('priceCode');
console.log(PRICE_ID);
createCheckoutSession(PRICE_ID).then(function (data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, you should display the localized error message to your
// customer using `error.message`.
if (result.error) {
alert(result.error.message);
}
})
});
});
The payment work but i got this eror after redirect to success
Invalid checkout.session id: {CHECKOUT_SESSION_ID}
How can i fix this error and how can i handle monthly payment inside my project ?
I had the same problem. I just add :
$this->router->generate('my_confirm_route', ['myrouteparam' => $myRouteParam], UrlGeneratorInterface::ABSOLUTE_URL).'?session_id={CHECKOUT_SESSION_ID}';
And in controller :
$request->query->get('session_id')
Leroy is correct, but I'd suggest you confirm that the success_url you're generating actually looks the way it's supposed to - i.e. isn't having those {}s urlencoded - as that might be what's going on here.
Beyond that I believe your approach is correct, and Leroy's is not - I think you're not providing Stripe with an unencoded URL so it's not recognizing {CHECKOUT_SESSION_ID} as a template string as it should be.
I was able to fix the problem by simply decoding the url before sending it.
urldecode($this->generateUrl('stripe-checkout-success', ['session' => '{CHECKOUT_SESSION_ID}'], UrlGeneratorInterface::ABSOLUTE_URL))
You pass that exact value to your PSP as success_url.
'success_url' => $this->generateUrl('success', ["session_id" => "{CHECKOUT_SESSION_ID}"], UrlGeneratorInterface::ABSOLUTE_URL),
Then you are getting back that redirect url, as literal : /en/success/{CHECKOUT_SESSION_ID}
Next you lookup that exact value {CHECKOUT_SESSION_ID} from some api.
$session = \Stripe\Checkout\Session::retrieve($request->get('session_id'));
I'm not sure, but I think you need to fetch the session id from the success_url's body instead of the the $request object.
I also think you can skip the session_id in the success_url, because that's the users's callback url. While the user is being redirected back to your application, the user is still logged in, You don't want to send your session id to your PSP.
I think your feedback is in the post body, based on what you've configured at stripe. Doing something like this should give you what you want.
First of all, remove the session id from the success_url
'success_url' => $this->generateUrl('success', UrlGeneratorInterface::ABSOLUTE_URL),
Next, change your success url logic to use the post body.
/**
* #Route(
* "/{_locale}/success",
* name="success",
* defaults={"_locale"="en"},
* requirements={"_locale"="en|fr"}
* )
* #param Request $request
* #return Response
*/
public function success(Request $request): Response
{
...
$body = $request->getContent();
$feedback = json_decode($body, true);
$session = \Stripe\Checkout\Session::retrieve($feedback['id']);
...
}
PS, bare in mind that I haven't read the documentation about Stripe. But I strongly believe that they send you their session id (which you created) back in some form.
I am trying to reproduce the behaviour of the facebook batch requests function on their graph api.
So I think that the easiest solution is to make several requests on a controller to my application like:
public function batchAction (Request $request)
{
$requests = $request->all();
$responses = [];
foreach ($requests as $req) {
$response = $this->get('some_http_client')
->request($req['method'],$req['relative_url'],$req['options']);
$responses[] = [
'method' => $req['method'],
'url' => $req['url'],
'code' => $response->getCode(),
'headers' => $response->getHeaders(),
'body' => $response->getContent()
]
}
return new JsonResponse($responses)
}
So with this solution, I think that my functional tests would be green.
However, I fill like initializing the service container X times might make the application much slower. Because for each request, every bundle is built, the service container is rebuilt each time etc...
Do you see any other solution for my problem?
In other words, do I need to make complete new HTTP requests to my server to get responses from other controllers in my application?
Thank you in advance for your advices!
Internally Symfony handle a Request with the http_kernel component. So you can simulate a Request for every batch action you want to execute and then pass it to the http_kernel component and then elaborate the result.
Consider this Example controller:
/**
* #Route("/batchAction", name="batchAction")
*/
public function batchAction()
{
// Simulate a batch request of existing route
$requests = [
[
'method' => 'GET',
'relative_url' => '/b',
'options' => 'a=b&cd',
],
[
'method' => 'GET',
'relative_url' => '/c',
'options' => 'a=b&cd',
],
];
$kernel = $this->get('http_kernel');
$responses = [];
foreach($requests as $aRequest){
// Construct a query params. Is only an example i don't know your input
$options=[];
parse_str($aRequest['options'], $options);
// Construct a new request object for each batch request
$req = Request::create(
$aRequest['relative_url'],
$aRequest['method'],
$options
);
// process the request
// TODO handle exception
$response = $kernel->handle($req);
$responses[] = [
'method' => $aRequest['method'],
'url' => $aRequest['relative_url'],
'code' => $response->getStatusCode(),
'headers' => $response->headers,
'body' => $response->getContent()
];
}
return new JsonResponse($responses);
}
With the following controller method:
/**
* #Route("/a", name="route_a_")
*/
public function aAction(Request $request)
{
return new Response('A');
}
/**
* #Route("/b", name="route_b_")
*/
public function bAction(Request $request)
{
return new Response('B');
}
/**
* #Route("/c", name="route_c_")
*/
public function cAction(Request $request)
{
return new Response('C');
}
The output of the request will be:
[
{"method":"GET","url":"\/b","code":200,"headers":{},"body":"B"},
{"method":"GET","url":"\/c","code":200,"headers":{},"body":"C"}
]
PS: I hope that I have correctly understand what you need.
There are ways to optimise test-speed, both with PHPunit configuration (for example, xdebug config, or running the tests with the phpdbg SAPI instead of including the Xdebug module into the usual PHP instance).
Because the code will always be running the AppKernel class, you can also put some optimisations in there for specific environments - including initiali[zs]ing the container less often during a test.
I'm using one such example by Kris Wallsmith. Here is his sample code.
class AppKernel extends Kernel
{
// ... registerBundles() etc
// In dev & test, you can also set the cache/log directories
// with getCacheDir() & getLogDir() to a ramdrive (/tmpfs).
// particularly useful when running in VirtualBox
protected function initializeContainer()
{
static $first = true;
if ('test' !== $this->getEnvironment()) {
parent::initializeContainer();
return;
}
$debug = $this->debug;
if (!$first) {
// disable debug mode on all but the first initialization
$this->debug = false;
}
// will not work with --process-isolation
$first = false;
try {
parent::initializeContainer();
} catch (\Exception $e) {
$this->debug = $debug;
throw $e;
}
$this->debug = $debug;
}
My question could be a duplicate of this one, but I can't find any satisfying answer so I will try to make this one more precise.
I am building an import service from an other API. And I don't want any duplicate in my new database.
So here an example of my current implementation:
The Controller:
public function mainAction ()
{
$em = $this->getDoctrine()->getManager();
$persons_data = [
[
'first_name' => 'John',
'last_name' => 'Doe'
],
[
'first_name' => 'John',
'last_name' => 'Doe'
]
];
$array = [];
foreach($persons_data as $person_data)
{
$person = $this->get('my_service')->findOrCreatePerson($person_data);
$array[] = $person;
}
$em->flush();
return new Response();
}
A service function:
public function findOrCreatePerson ($data)
{
$em = $this->em;
$person = $em->getRepository('AppBundle:Person')->findOneBy([
'first_name' => $data['first_name'],
'last_name' => $data['last_name']
]);
if(is_null($person)) {
$person = new Person();
$person->setFirstName($data['first_name']);
$person->setLastName($data['last_name']);
$em->persist($person);
}
return $person
}
I tried to make it as simple as possible.
As you can see, I would like to make only one DB transaction to get some performance improvements.
Problem is, if I don't flush at the end of the findOrCreatePerson() method, the query to the Person repository won't find the first object and will create duplicates in the database.
My question is simple: How should I implement such a thing?
This is a job for memoize!
// Cache
private $persons = [];
public function findOrCreatePerson ($data)
{
// Need unique identifier for persons
$personKey = $data['first_name'] . $data['last_name'];
// Already processed ?
if (isset($this->persons[$personKey])) {
return $this->persons[$personKey];
}
$em = $this->em;
$person = $em->getRepository('AppBundle:Person')->findOneBy([
'first_name' => $data['first_name'],
'last_name' => $data['last_name']
]);
if(is_null($person)) {
$person = new Person();
$person->setFirstName($data['first_name']);
$person->setLastName($data['last_name']);
$em->persist($person);
}
// Cache
$this->persons[$personKey] = $person;
return $person
}
Cerad's answer (memoization) is a good one, but I'd encourage you to reconsider something.
As you can see, I would like to make only one DB transaction to get some performance improvements.
And there are a few things wrong with that sentence.
The main one is that you're conflating flush() with a single, atomic transaction. You can manually manage transaction boundaries, and it's often very advantageous to do so.
The second thing is that when you're talking about bulk imports, you'll quickly learn that the first performance issue you hit is not the database at all. It's the the EntityManager itself. As the EM's internal identity map gets swollen, computing changes to persist to the DB gets very, very slow.
I'd consider rewriting your core loop as follows, and see if it's fast enough. Only then consider memoization if necessary.
$em->beginTransaction();
foreach($persons_data as $person_data)
{
$person = $this->get('my_service')->findOrCreatePerson($person_data);
$em->flush();
$em->clear(); // don't keep previously inserted entities in the EM.
}
$em->commit();
Here is the situation, I have a form:
public function buildForm(FormBuilderInterface $builder, array $options) {
foreach ($this->attributeGroupAttributes as $attribute) {
$builder->add('attribute-' . $attribute->getId(), $attribute->getType(), array(
'label' => $attribute->getName(),
));
}
}.
This form I'm using to validate data added dynamically to another form which was submitted.
This is the process how I do it:
I extract the data from the previous form.
I pass this data to another Action.
Here I get the attributeGroup.
I pass the Attributes in that group into the FormType and create a new form.
After which I put the data into the form via:
$form_validate->submit($normalizedData);
This is the controller:
public function createAction($formData) {
$itemService = $this->get('app.item');
$attributeGroupService = $this->get('app.attribute_group');
$attributeService = $this->get('app.attribute');
$attributeGroup = $attributeGroupService->getById($formData['attributeGroup']);
$attributeGroupAttributes = $attributeGroup->getAttributes();
$form_validate = $this->createForm(new \AppBundle\Form\ItemValidationType($attributeGroupAttributes));
$normalizedData = $itemService->normalizeDataForForm($formData);
$form_validate->submit($normalizedData);
if($form_validate->isValid()){
dump($form_validate->getData());
die();
}
die();
}
The error I get is:
-errors: array:1 [▼
0 => FormError {#855 ▼
-message: "The CSRF token is invalid. Please try to resubmit the form."
#messageTemplate: "The CSRF token is invalid. Please try to resubmit the form."
#messageParameters: []
#messagePluralization: null
-cause: null
-origin: Form {#836}
}
And that way ofcourse $form_validate->isValid() is false.
I am coming across some problems when trying to use ZF2's authentication services. I have to following Module.php getServiceConfig function:
<?php
public function getServiceConfig()
{
return array(
'factories' => array(
'Auth\Model\CustomerTable' => function($sm) {
$tableGateway = $sm->get('CustomerTableGateway');
$table = new CustomerTable($tableGateway);
return $table;
},
'CustomerTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Customer()); // prototype pattern implemented.
return new TableGateway('customer', $dbAdapter, null, $resultSetPrototype);
},
'Auth\Model\AuthStorage' => function($sm){
return new \Auth\Model\AuthStorage('jamietech');
},
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,
'customer','username','password');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($sm->get('Auth\Model\AuthStorage'));
return $authService;
},
),
);
}
The AuthStorage factory simply creates a new AuthStorage for us to keep track of the rememberMe function I have, and the AuthService factory creates a new Authentication Service for us. I can't see anything that I have done wrong but when running the following code in the AuthController.php:
<?php
public function loginAction()
{
//if already login, redirect to success page
if ($this->getAuthService()->hasIdentity()){
return $this->redirect()->toRoute('success');
}
$form = new LoginForm();
return array(
'form' => $form,
'messages' => $this->flashmessenger()->getMessages()
);
}
public function logoutAction()
{
$this->getSessionStorage()->forgetMe();
$this->getAuthService()->clearIdentity();
$this->flashmessenger()->addMessage("You have logged out successfully.");
return $this->redirect()->toRoute('auth', array('action'=>'login'));
}
PHPUnit encounters the following errors when running the PHPUnit command:
1: "testLoginActionCanBeAccessed - Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance of Zend\Db\Adapter\Adapter
1: "testLogoutActionCanBeAccessed - session_regenerate_id(): cannot regenerate session id - headers already sent.
And this error for both login and logout when the -process-isolation command is run:
"Serialization of closure is not allowed in: C;\Users\-----\AppData\Local\Temp\-----
If somebody could help that would be great. I am a ZF noob so try not to be too harsh.
EDIT: BTW THe global.php file includes the service_manager adapter factory illustrated in the ZF2 tutorial application.
Thank you!
Jamie Mclaughlan
did you check these:
autoload_classmap.php (for your module)
in your module.config.php
like this
service_manager' => array(
'aliases' => array(
'mymodule-ZendDbAdapter' => 'Zend\Db\Adapter\Adapter',
),
);
I hope it helps you to find the answer