public ActionResult CreatePurchase(PurchaseInfo purchaseInfo)
{
try
{
string action = "Index";
var payer = new Payer
{
payment_method = "credit_card",
funding_instruments = new List<FundingInstrument>(),
payer_info = new PayerInfo
{
email = "*****@*****.**"
}
};
var creditCard = new CreditCard();
if (!string.IsNullOrEmpty(purchaseInfo.CreditCardId))
{
payer.funding_instruments.Add(new FundingInstrument()
{
credit_card_token = new CreditCardToken()
{
credit_card_id = purchaseInfo.CreditCardId
}
});
}
else
{
creditCard = new CreditCard()
{
billing_address = new Address()
{
city = "Orlando",
country_code = "US",
line1 = "123 Test Way",
postal_code = "32803",
state = "FL"
},
cvv2 = purchaseInfo.CVV2,
expire_month = purchaseInfo.ExpMonth,
expire_year = purchaseInfo.ExpYear,
first_name = purchaseInfo.FirstName,
last_name = purchaseInfo.LastName,
number = purchaseInfo.CreditCardNumber,
type = Common.GetCardType(purchaseInfo.CreditCardNumber)
};
payer.funding_instruments.Add(new FundingInstrument()
{
credit_card = creditCard
});
}
if (!purchaseInfo.IsRecurring)
{
var transaction = new Transaction
{
amount = new Amount
{
currency = "USD",
total = purchaseInfo.Amount.ToString()
},
description = "Featured Profile on ProductionHUB",
invoice_number = Common.GetRandomInvoiceNumber()
};
var payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = new List<Transaction>() { transaction }
};
var createdPayment = payment.Create(apiContext);
TempData["info"] = createdPayment.id;
if (createdPayment.state == "approved")
{
action = "Completed";
}
else
{
action = "Rejected";
}
}
else
{
var agreement = new Agreement()
{
name = "Basic profile",
description = "Monthly basic profile in perpetuity",
payer = payer,
plan = new Plan { id = purchaseInfo.BillingPlanId },
start_date = DateTime.UtcNow.AddDays(1).ToString("u").Replace(" ", "T"),
};
var createdAgreement = agreement.Create(apiContext);
TempData["info"] = createdAgreement.create_time;
TempData["success"] = "Recurring agreement created";
}
if (purchaseInfo.SavePaymentInfo)
{
creditCard.external_customer_id = customerId;
creditCard.Create(apiContext);
}
return RedirectToAction(action);
}
catch (Exception exc)
{
TempData["error"] = exc.Message;
}
ViewBag.Cards = CreditCard.List(apiContext, externalCustomerId: customerId);
ViewBag.Plans = plans;
AddPaymentDropdowns();
return View();
}