I m using stripe for my payment. Everything works fine exept one thing.
I have 4 different plans : Free(0€) / free + extra (4,90€) / premium (49€) / premium + extra (53,90€).
The user can update the subsciption, like that :
// i get the $plan variable by my ajax request
$newPlan = $plan;
// i find the name of the current user's plan in my database
$payment = $em->getRepository('CacPaymentBundle:Payment')->findOneByUser($id);
$customerId = $payment->getCustomerId();
// i update my user's stripe plan
$cu = \Stripe\Customer::retrieve($customerId);
$planId = $cu->subscriptions->data[0]->id;
$subscription = $cu->subscriptions->retrieve($planId);
$subscription->plan = $newPlan;
// i update my plan in my database
$subscription->save();
$payment->setPlan($newPlan);
$em->persist($payment);
$em->flush();
Here is my probleme : In my stripe back office, the user has the right plan but the amount due is not correct.
When i update to free + extra to premium with extra, the amount due is 102€ and few cents
I don't understand why my amount is not correct and my plan is correct
Any idea is welcome :)
Thanks
Stripe calculates a percentage of the subscription with respect to time.
That's why the amount I get was not the one to which I wanted
Related
I'm using Memberpress and Memberpress Corporate on my WordPress site and I'm trying to add a custom function when a member signs up under a specific membership type or purchases a specific membership type. When this happens I need to grab the corporate account ID and do something with it.
I'm using the hook mepr-event-transaction-completed as this fires for both recurring and non-recurring transactions, though I also tried mepr-event-non-recurring-transaction-completed just to be sure.
This is my code:
$transaction = $event->get_data();
$membership_type_ids = array(1, 2, 4);
if (in_array($transaction->product_id, $membership_type_ids) && $transaction->txn_type == 'payment') {
$org_id = $transaction->corporate_account_id;
my_custom_function($org_id);
}
When the user is signing up for this membership type with a subscription, this is no problem, I can retrieve this, however if they are signing up with a one-time non-recurring transaction, the corporate account id is returning as 0, even though when I go to check the database, there is a corporate account id there.
Does the corporate account id get set at a different time for non-recurring transactions?
Okay, so after speaking to Memberpress, it turns out this just doesn't get set at the right time.
I used a workaround as so:
if($transaction->corporate_account_id !== "0" && $transaction->corporate_account_id !== 0) {
//some irrelevant code here about what to do if the corporate id actually works
} else {
write_log('sending cron to add new user due to corporate id returning as 0, please check in 2 minutes. tran_num = '.$transaction->trans_num);
wp_schedule_single_event( strtotime("+2 minutes"), 'send_fix_for_zero_transaction', array($transaction),false );
return;
}
add_action( 'send_fix_for_zero_transaction', 'single_transaction_create_corporate' );
function single_transaction_create_corporate($transaction) {
//NOTE: this function is only used it a one-time transaction is created in the backend to create a corporate membership, because there is a bug in memberpress that means the corporate_id isn't sent back by the event. This event will only fire if that is the case, otherwise this is handled by rc_setup_new_org. This should eventually be deprecated when Memberpress fix their issues.
$trans_num = $transaction->trans_num;
$full_trans = MeprTransaction::get_one_by_trans_num($trans_num);
//do whatever you need to do with the transaction here, you have the number now
}
OK, I've been banging my head against this for a few days now. I'm building a payment process into a PHP application which will allow for upselling products after a customer approves a payment.
I can get the payment charged to the customer without an issue, but if they select any kind of upsell product which requires the order value to change, then I get errors even though it is following to the letter what was in the documentation I could find...
Below is the test function I'm using, this is the function which is called when the user is redirected back to the website AFTER approving the payment.
public function confirmOrder($payer_id, $payment_id, $incentives = false){
//GET PAYMENT
$payment = Payment::get($payment_id, $this->apiContext);
//CREATE EXECUTION WITH PAYER ID
$execution = new PaymentExecution();
$execution->setPayerId($payer_id);
//APPLY PAYMENT AMOUNT - Original amount was 7.00
$amount = new Amount();
$amount = $amount->setCurrency('GBP')->setTotal('8.00');
//PATCH REPLACE
$patchReplace = new Patch();
$patchReplace = $patchReplace->setOp('replace')->setPath('/transactions/0/amount')->setValue($amount);
//CREATE PATCH OBJECT
$patchRequest = new PatchRequest();
$patchRequest = $patchRequest->setPatches(array($patchReplace));
try {
$payment->update($patchRequest, $this->apiContext);
} catch (PayPalConnectionException $ex) {
return "PATCH ERROR: State(" . $payment->getState() . ") ".$ex->getData();
}
}
This isn't the final code I will use but right now I'm just trying to get an order updated before I build in more of the logic. This code gives me the following error:
PATCH ERROR: State(created) {"name":"PAYMENT_STATE_INVALID","message":"This request is invalid due to the current state of the payment","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#PAYMENT_STATE_INVALID","debug_id":"9caacdc1d652b"}
You can see I'm outputting the getState() which is coming back as 'created' which would normally be fine for updating in everything I can find but it is still failing.
Does anyone else have experience with the PayPal PHP SDK and could help point me in the right direction?
In PayPal, once the user has approved the payment from the consent screen, the amount cannot be changed, as you can understand the user agreed to pay only the amount he/she saw on the consent screen. Modifying the amount after user approves it, is not allowed, and that's why you are seeing that state exception, as the state is already approved.
Anyway, looking at your scenario, I think you might be interested in either creating order, or capture, instead of sale.
Also, in our PayPal-PHP-SDK, there are a lot of interesting documents that could help you understand PHP-SDK better. We also provide a lot of runnable samples, that you could setup in your local machine, by one command only.
I have a magento website
We have a store
In a store we have multiple store views (US, EU and UK)
Each store view has it's own currency, etc.
The base currency is GBP at the default config (main)
My problem is that the display currencies work well. Each store view has it's own individual price (no automatic conversion). Everything seems to be working and in order. However, on the final payment emails and actual connection with payment providers (PayPal/Sage). The base currency is always used. Although the display appears in the currency for each store view.
My question is why are the store view currencies not used with PayPal, emails, etc. Although the amounts, display currency, etc work fine?
It turns out that Base Currency can be set on each Store View. However, this option was not presented on the admin side. I had to change the system.xml
app/code/core/Mage/Directory/etc/system.xml
<label>Base Currency</label>
I have to set the appropriate to change from 0 to 1
<show_in_store>1</show_in_store>
Once this was done, I could see Base Currency under "Currency Options" even within a store view. This now works well and everything seems to be working fine.
No PHP code changes or any additional plugins required.
When I had run into this issue with a rather large Magento store, this quick fix worked pretty well for me: Magento knowledge-base paypal base currency tweak
Just note that, that fix probably won't work out of the box but it'll take some tweaking
Here it is some solutions.
You might custom some codes
If you are using Paypal Express,
\app\code\core\Mage\Paypal\Model\Express.php
protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
{
$order = $payment->getOrder();
// prepare api call
$token = $payment->getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_TOKEN);
$api = $this->_pro->getApi()
->setToken($token)
->setPayerId($payment->
getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID))
->setAmount($amount)
->setPaymentAction($this->_pro->getConfig()->paymentAction)
->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
->setInvNum($order->getIncrementId())
**->setCurrencyCode($order->getOrderCurrencyCode())** // should be used getOrderCurrencyCode();
->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled)
;
if ($order->getIsVirtual()) {
$api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
} else {
$api->setAddress($order->getShippingAddress());
$api->setBillingAddress($order->getBillingAddress());
}
// call api and get details from it
$api->callDoExpressCheckoutPayment();
$this->_importToPayment($api, $payment);
return $this;
}
\app\code\core\Mage\Paypal\Model\Standard.php
public function getStandardCheckoutFormFields()
{
$orderIncrementId = $this->getCheckout()->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
/* #var $api Mage_Paypal_Model_Api_Standard */
$api = Mage::getModel('paypal/api_standard')->setConfigObject($this->getConfig());
$api->setOrderId($orderIncrementId)
**->setCurrencyCode($order->getOrderCurrencyCode())** // should be used getOrderCurrencyCode();
//->setPaymentAction()
->setOrder($order)
->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
->setReturnUrl(Mage::getUrl('paypal/standard/success'))
->setCancelUrl(Mage::getUrl('paypal/standard/cancel'));
// export address
$isOrderVirtual = $order->getIsVirtual();
$address = $isOrderVirtual ? $order->getBillingAddress() : $order->getShippingAddress();
if ($isOrderVirtual) {
$api->setNoShipping(true);
} elseif ($address->validate()) {
$api->setAddress($address);
}
// add cart totals and line items
$api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
->setIsLineItemsEnabled($this->_config->lineItemsEnabled)
;
$api->setCartSummary($this->_getAggregatedCartSummary());
$api->setLocale($api->getLocaleCode());
$result = $api->getStandardCheckoutRequest();
return $result;
}
I'm having some trouble. What I want to do is automatically generate a single random coupon code in Magento each time someone subscribes to our newsletter. The coupon is 10 dollars off anything and will have an exp. date of two weeks after subscription.
So, I'm trying to write a simple script that trips when the "subscribe to our newsletter" form is submitted that will talk to Magento, ask Magento for a single random coupon code, set a few basic price rules (10 bucks off anything, one use per customer, one use per coupon, expires two weeks from generation) and then return a random coupon code (ex:WELCOME5798) which can be stored in a variable that will be passed, along w/ first+last name and e-mail to MailChimp via the MailChimp API. I have all this figured out EXCEPT for how to get Mage to generate such a code via a PHP script and then return said code (i.e. I have my form and I know how to pass values to MailChimp).
I'm new to Magento, so I'm having a tough time. I've seen the code in Mage/SalesRule/Model/Coupon and I've seen some examples of people solving somewhat similar questions, such as here: Magento - Create Unique Coupon Codes through code and mail it to the customer
But I'm really at a loss for where to start making this work for my own purposes. Could use some help/setting straight. :( Thanks folks.
So, what is your question? How to generate coupon for your requirements? Or how to arrange it in module?
You can use event newsletter_subscriber_save_after to inject your custom actions to the subscribe process.
Here is an example of coupon creation according to your needs
<?php
/**
* Create coupon for fixed price discount
*
* #param int $customer_id
* #param float $discount
*/
public function createCoupon($customer_id, $discount)
{
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
$websitesId = Mage::getModel('core/website')->getCollection()->getAllIds();
$customer_name = $customer->getName();
$couponCode = Mage::helper('core')->getRandomString(9);
$model = Mage::getModel('salesrule/rule');
$model->setName('Discount for ' . $customer_name);
$model->setDescription('Discount for ' . $customer_name);
$model->setFromDate(date('Y-m-d'));
$model->setToDate(date('Y-m-d', strtotime('+2 days')));
$model->setCouponType(2);
$model->setCouponCode($couponCode);
$model->setUsesPerCoupon(1);
$model->setUsesPerCustomer(1);
$model->setCustomerGroupIds($customerGroupIds);
$model->setIsActive(1);
$model->setConditionsSerialized('a:6:{s:4:\"type\";s:32:\"salesrule/rule_condition_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";}');
$model->setActionsSerialized('a:6:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";}');
$model->setStopRulesProcessing(0);
$model->setIsAdvanced(1);
$model->setProductIds('');
$model->setSortOrder(1);
$model->setSimpleAction('by_fixed');
$model->setDiscountAmount($discount);
$model->setDiscountStep(0);
$model->setSimpleFreeShipping(0);
$model->setTimesUsed(0);
$model->setIsRss(0);
$model->setWebsiteIds($websitesId);
try {
$model->save();
} catch (Exception $e) {
Mage::log($e->getMessage());
}
}
In PHP I do a simple calculation. Where we are, tax (GST) is charged at 10%. So, What I'm doing in my php code is:
$a=$_REQUEST["amount"]; // this contains 504.95
$amount = $a*.10;
BUT, When I click "Purchase" and it takes me to PayPal, instead of showing up with order details, PayPal says:
"The link you have used to enter the
PayPal system contains an incorrectly
formatted item amount."
Here's what I've tried:
I have tried making a purchase WITHOUT tax. And it worked.
I have tried making a purchase with a value of 504 INSTEAD OF 504.95 and it worked.
So, how can I charge 10% tax on 504.95?
Here's my code:
$amnt = $_REQUEST['amount'];
$amount = $amnt*.10;
$p->add_field('tax', $amount);
Any help at all is appreciated.
Thank you
UPDATE:
Okay,
So I think I may have figured out how to get the right way to send the data, by using
number_format($price*0.10);
But, it's not calculating properly on the paypal webpage!
I have a sample item priced at $504.85, and it is supposed to be taxed 10%.
But PayPal is only charging $50 (that does not equal 10%)
What the!?
Ok cool, I've got it.
I am now using:
$amnt = $_REQUEST['amount'];
$ship = $_REQUEST['freight'];
$comb = $amnt+$ship;
$amount = $comb*0.10;
$p->add_field('tax', number_format($amount,2,'.',''));
And it works perfectly. Thanks!