I'm using laravel-spark to handle stripe payments on my website, the users can apply coupons on their subscriptions so they will have discounts, now I want to show user discount on the profile page.
I searched on the spark documentation about how to retrieve the current discount and they didn't mention that anywhere.
I found that laravel-spark has this feature implemented in the CouponController method current()
CouponController :
/**
* Get the current discount for the given user.
*
* #param Request $request
* #param string $userId
* #return Response
*/
public function current(Request $request, $userId)
{
$user = Spark::user()->where('id', $userId)->firstOrFail();
if ($coupon = $this->coupons->forBillable($user)) {
return response()->json($coupon->toArray());
}
abort(204);
}
I tried to call this method in my view with Vue as the following:
axios.get('/coupon/user/'+ this.user.id).then(response => {
console.log("response : ", response);
coupon = response.data.data;
if (coupon) {
//do something
}
}, error => {
console.log("some error occurred", error);
});
but the response is always empty with 204 status code, even if the user has a valid coupon on his stripe account.
any suggestions ?!
Finally I found that this method inside the coupon controller get the valid coupons that applied to customers not to subscriptions,
so all you need is to apply the coupons per customer not per subscription on stripe.
or you can write your logic to retrieve discounts via stripe API's , you can find all details here : https://stripe.com/docs/api#discounts
Hope that this will help you .
Related
I'm using Laravel Cashier with stripe payment. One user can have multiple subscriptions. User should able to cancel particular subscription. Is there anyway to cancel subscription by stripe id or plan id?
You can use the PHP Stripe library to do it.
To cancel immediately
$sub = Stripe\Subscription::retrieve($subscription_id);
$sub->cancel();
To cancel after current period end
$sub = Stripe\Subscription::update($subscription_id, [
'cancel_at_period_end' => true
]);
You can use it with Laravel Cashier using subscription id .
In Laravel
You can find the subscription id in the subscriptions table in your database.(column name is stripe_id)
$subscription = \Stripe\Subscription::retrieve($subscription_id);
If you pass the correct subscription id , you will get the subscription details
To cancel the subscription
$sub->cancel();
Update your subscriptions table for the particular subscription id (again column name is stripe_id). In my
\Stripe\Subscription::where('stripe_id', $sub->id)
->update([
'trial_ends_at' => Carbon::now()->toDateTimeString(),
]);
I'm sure this is documented somewhere, but for the life of me I can't find it, but, if you pass the stripe subscription id in as the 2nd parameter you can cancel any subscription the Laravel way...
$user->subscription('default', 'price_abcdefghi12345')->cancel();
You can just call the cancel() method directly on your subscription:
$subscription->cancel();
As you can see here (Github Reference), the method $this->subscription called from your User model just gets a Subscription instance filtering by name:
/**
* Get a subscription instance by name.
*
* #param string $name
* #return \Laravel\Cashier\Subscription|null
*/
public function subscription($name = 'default')
{
return $this->subscriptions->where('name', $name)->first();
}
And, as you can see (Github Reference), the Subscription model has its own cancel method:
/**
* Cancel the subscription at the end of the billing period.
*
* #return $this
*/
public function cancel()
{
// ...
}
So, you can always call it directly on the Subscription instance.
You don't need to have the id to cancel.
You can just call
$subscription->cancel();
and it will work. If you have multiple subscriptions which you would like to cancel at the same time. You can do this. Let's find all the active subscriptions. You can find all the scopes here
$subscriptions = $user->subscriptions()->active()->get(); // getting all the active subscriptions
$subscriptions->map(function($subscription) {
$subscription->cancel(); // cancelling each of the active subscription
});
You can do this by using the cashier Subscription model as like Laravel model
Cancel Subscription
$subscription = Subscription::where('name', 'default')->where('user_id', auth()->id())->first();
$subscription->cancel();
Resume Subscription
$subscription = Subscription::where('name', 'default')->where('user_id', auth()->id())->first();
$subscription->resume();
In magento checkout page, after giving billing information and shipping information, i understand that these details are sending to fedex then the shipping rates are populating in chekout page, before sending theses details to fedex, i want to change the weight of the product, i Want to add additional weights for each products,
suppose user adding a product with weight of 2 pounds, i want to send
these weight to 2*5 = 10pounds, how can i do that in magento? please
help.
Not sure to understand what you want exactly but I'll give a try.
I think you can override Mage_Checkout_OnepageController::savePaymentAction method in your local pool and in your local method, dispatch your own new event 'save_payment_action_before' and pass your objects as parameters.
In your fedex module, create an observer, get your object and change the order weight before it's sent to fedex.
To create your custom event, check this post
Finally i find out that it is happening in the sales/quote/item.php file, there is a function called setProduct, here we need to add addititonal info while setting data.
public function setProduct($product)
{
$batchQty = Mage::getModel('catalog/product')->load($product->getId())->getBatchQty();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$userrole = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
$userrole = strtolower($userrole);
if ($this->getQuote()) {
$product->setStoreId($this->getQuote()->getStoreId());
$product->setCustomerGroupId($this->getQuote()->getCustomerGroupId());
}
if($userrole=="retailer" && $batchQty>0 ){
$this->setData('product', $product)
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setSku($this->getProduct()->getSku())
->setName($product->getName())
->setWeight($this->getProduct()->getWeight()*$batchQty)
->setTaxClassId($product->getTaxClassId())
->setBaseCost($product->getCost())
->setIsRecurring($product->getIsRecurring());
} else {
$this->setData('product', $product)
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setSku($this->getProduct()->getSku())
->setName($product->getName())
->setWeight($this->getProduct()->getWeight())
->setTaxClassId($product->getTaxClassId())
->setBaseCost($product->getCost())
->setIsRecurring($product->getIsRecurring());
}
if ($product->getStockItem()) {
$this->setIsQtyDecimal($product->getStockItem()->getIsQtyDecimal());
}
Mage::dispatchEvent('sales_quote_item_set_product', array(
'product' => $product,
'quote_item' => $this
));
return $this;
}
I am working on API based Payment gateway in magento, When user directly pay from credit card, I am doing my all process by calling api specific function in capture method of payment gateway.
When I will enable 3D secure option for payment gateway I need to redirect user to 3rdparty for verification for that I am using getOrderPlaceRedirectUrl with condition.
With Condition I ma also saving order with pending status but Magento generate the invoice and mark as paid and change status to processing. that I need to do once get successful authentication from 3rdparty.
for updating order status using following code:
$order->setState( Mage_Sales_Model_Order::STATE_NEW, true );
$order->save();
If anyone can help how can I control to not generate invoice in capture method will be very appreciated.
If you use capture() in your payment method, and your capture() method returns without throwing an exception, then Magento assumes that the capture is done, "the money is in your pocket", so it makes the invoice. This is not good if you use a 3rd party payment gateway.
You can do the following: set your *payment_action* to order in your config.xml
<config>
<default>
<payment>
<yourPaymentCode>
<order_status>pending_payment</order_status>
<payment_action>order</payment_action>
...
In your payment method set the attributes and implement the order() method.
Snd set getOrderPlaceRedirectUrl, but you already did that.
class Your_Module_Model_PaymentMethod
extends Mage_Payment_Model_Method_Abstract
{
// set these attributes
protected $_code = 'yourPaymentCode';
protected $_isGateway = true;
protected $_canCapture = false;
protected $_canAuthorize = false;
protected $_canOrder = true;
public function order(Varien_Object $payment, $amount)
{
// initialize your gateway transaction as needed
// $gateway is just an imaginary example of course
$gateway->init( $amount,
$payment->getOrder()->getId(),
$returnUrl,
...);
if($gateway->isSuccess()) {
// save transaction id
$payment->setTransactionId($gateway->getTransactionId());
} else {
// this message will be shown to the customer
Mage::throwException($gateway->getErrorMessage());
}
return $this;
}
And somehow the gateway has to respond. In my case they redirect the customer to $responseUrl given in init(), but they warn that it is possible that the user's browser crashes after payment but before they can be redirected to our store: In that case they call the URL in the background, so handling that URL cannot rely on session data. I made a controller for this:
public function gatewayResponseAction()
{
// again the imaginary example $gateway
$order = Mage::getModel('sales/order')->load( $gateway->getOrderId() );
$payment = $order->getPayment();
$transaction = $payment->getTransaction( $gateway->getTransactionId() );
if ($gateway->isSuccess())
{
$payment->registerCaptureNotification( $gateway->getAmount() );
$payment->save();
$order->save();
$this->_redirect('checkout/onepage/success');
}
else
{
Mage::getSingleton('core/session')
->addError($gateway->getErrorMessage() );
// set quote active again, and cancel order
// so the user can make a new order
$quote = Mage::getModel('sales/quote')->load( $order->getQuoteId() );
$quote->setIsActive( true )->save();
$order->cancel()->save();
$this->_redirect('checkout/onepage');
}
}
Try setting the state to pending payment, something like the following;
$order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
'pending_payment',
'3D Secure Auth Now Taking Place')->save();
P.S. The PayPal module that comes with Magento does exactly what you are trying to do, so take a look at PayPal Standard model and controller for some pointers.
I have an problem with invoice generating for my downloadable product.
I tried to create an invoice. But it shows like this error message Cannot create an invoice without products.
Can any one help to solve this issue.
Thanks
meaning that you have not selected the products that you need to invoice if you look at the code that gives this error in following file
app/code/core/Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php:88: Mage::throwException($this->__('Cannot create an invoice without products.'));
you see that this error is given when no items are found
$savedQtys = $this->_getItemQtys();
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
if (!$invoice->getTotalQty()) {
Mage::throwException($this->__('Cannot create an invoice without products.'));
}
and you can also see that the items are looked from your post
/**
* Get requested items qty's from request
*/
protected function _getItemQtys()
{
$data = $this->getRequest()->getParam('invoice');
if (isset($data['items'])) {
$qtys = $data['items'];
} else {
$qtys = array();
}
return $qtys;
}
meaning that you have not selected or posted whatever needs to be posted to this method.
I have a "product" entity and i want to validate a property(for example price) of this class with a custom callback function.
My custom validation is more complex than the defaults validation provided by sf2(minLength, max, etc). I wish to do something like this:
class Product
{
/**
* #Assert\NotBlank()
* #Assert\CallbackValidationFunction('validatePrice', 'Your price is not the expected')
*/
private $price;
}
function validatePrice($priceValue){
$x = " i want";
return $priceValue == "the value".$x;
}
then, in the errors the message 'Your price is not the expected' is related withthe property $price in Product after a $form->isValid() or a product validation via $this->get('validator');
You'd be better off writing a custom validation constraint. See http://symfony.com/doc/current/cookbook/validation/custom_constraint.html for instructions.