/** * Return information about pro-rated credit or false if there is none. * * Returns a standard object: * $data->description = The description of the credit * $data->amount = The monetary amount of the credit * $data->money = The formated monetary amount of the credit * * return object or false */ public function getProRateInfo() { $data = false; $proRateAmount = 0; if ($this->isSpreedlySubscription()) { if (Cart66Common::isLoggedIn() && Cart66Session::get('Cart66Cart')) { if ($subscriptionId = Cart66Session::get('Cart66Cart')->getSpreedlySubscriptionId()) { try { $invoiceData = array('subscription-plan-id' => $subscriptionId, 'subscriber' => array('customer-id' => Cart66Session::get('Cart66AccountId'))); $invoice = new SpreedlyInvoice(); $invoice->createFromArray($invoiceData); $this->_creditAmount = abs((double) $invoice->invoiceData->{'line-items'}->{'line-item'}[1]->amount); $data = new stdClass(); $data->description = $invoice->invoiceData->{'line-items'}->{'line-item'}[1]->description; $data->amount = $this->_creditAmount; $data->money = Cart66Common::currency($this->_creditAmount); if ($data->amount > 0) { $proRateAmount = $data->amount; } Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Spreedly Invoice: " . print_r($invoice->invoiceData, true)); } catch (SpreedlyException $e) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Unable to locate spreedly customer: " . Cart66Session::get('Cart66AccountId')); } } } } Cart66Session::set('Cart66ProRateAmount', $proRateAmount, true); return $data; }
public function testPayInvoice() { global $test_site_name, $test_token; Spreedly::configure($test_site_name, $test_token); SpreedlySubscriber::wipe(); // create invoice for existing customer $trial_plan = SpreedlySubscriptionPlan::find_by_name("Free Trial"); $sub = SpreedlySubscriber::create(75, null, "charlie"); $sub->activate_free_trial($trial_plan->id); // !!!! $sub = SpreedlySubscriber::find(75); $annual = SpreedlySubscriptionPlan::find_by_name("Annual"); $invoice = SpreedlyInvoice::create($sub->get_id(), $annual->id, $sub->screen_name, "*****@*****.**"); $response = $invoice->pay("4222222222222", "visa", "123", "13", date("Y") + 1, "Test", "User"); $this->assertTrue($response instanceof SpreedlyErrorList); $response = $invoice->pay("4222222222222", "visa", "123", "12", date("Y") - 1, "Test", "User"); $this->assertTrue($response instanceof SpreedlyErrorList); // declined try { $response = $invoice->pay("4012888888881881", "visa", "123", "12", date("Y") + 1, "Test", "User"); $this->fail("An exception should have been thrown"); } catch (SpreedlyException $e) { $this->assertEquals(403, $e->getCode()); } $response = $invoice->pay("4222222222222", "visa", "123", "12", date("Y") + 1, "Test", "User"); $this->assertTrue($response->closed); // test paying paid invoice try { $response = $invoice->pay("4222222222222", "visa", "123", "12", date("Y") + 1, "Test", "User"); $this->fail("An exception should have been thrown"); } catch (SpreedlyException $e) { $this->assertEquals(403, $e->getCode()); } // test adding fees $this->assertEquals(75, $sub->get_id()); $sub->add_fee("Daily Bandwidth Charge", "313 MB used", "Traffic Fees", 2.34); }
/** * Create a Spreedly subscription. * * @param int $accountId The primary key from the Cart66 accounts table associated with this subscription * @param int $subscriptionId The id of the spreedly subscription plan * @param mixed $paymentMethod Either 'on-file' or a SpreedlyCreditCard object */ public function createSpreedlySubscription($accountId, $subscriptionId, $productId, $paymentMethod = 'on-file') { $subscriptionCreated = false; if (is_numeric($accountId) && $accountId > 0) { if (!$this->loadByAccountId($accountId)) { $this->accountId = $accountId; } $subscriber = new SpreedlySubscriber(); $subscriber->hydrate($this->_getSpreedlySubscriberDataArray()); $subscription = new SpreedlySubscription($subscriptionId); if ('free_trial' == strtolower((string) $subscription->planType)) { $subscriberData = $subscriber->toArray(true); // prune the empty data Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Creating a new subscriber before assigning free trial. " . print_r($subscriberData, true)); $subscriber->create($subscriberData); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Preparing to assign free trial plan ({$subscriptionId}) to new subscriber: " . print_r($subscriber->toArray(), true)); $subscriber->assignFreeTrialPlan($subscriptionId); } else { $invoice = new SpreedlyInvoice(); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Creating a Spreedly invoice for subscription id: {$subscriptionId}"); $invoice->create($subscriber, $subscriptionId); $invoice->pay($paymentMethod); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Spreedly invoice has been created and paid."); } $this->productId = $productId; $this->save(); } }