1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| public class PaypalController : Controller
{
//
// GET: /Paypal/
public ActionResult Index()
{
return View();
}
[HttpPost]
public async System.Threading.Tasks.Task<PayPalHttp.HttpResponse> CreateOrder(bool debug = false)
{
var request = new OrdersCreateRequest();
request.Headers.Add("prefer", "return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
try
{
PayPalHttp.HttpResponse response = await PayPalClient.client().Execute(request);
//response = await PayPalClient.client().Execute(request);
//string o = PayPalClient.ObjectToJSONString(response.Result<Order>());
return response;
}
catch (Exception ex)
{
}
debug = true;
if (debug)
{
//var result = response.Result<Order>();
//Console.WriteLine("Status: {0}", result.Status);
//Console.WriteLine("Order Id: {0}", result.Id);
//Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
//Console.WriteLine("Links:");
//foreach (LinkDescription link in result.Links)
//{
// Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
//}
//AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
//Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
}
return null;
}
private static OrderRequest BuildRequestBody()
{
OrderRequest orderRequest = new OrderRequest()
{
CheckoutPaymentIntent = "CAPTURE",
ApplicationContext = new ApplicationContext
{
BrandName = "EXAMPLE INC",
LandingPage = "BILLING",
UserAction = "CONTINUE",
ShippingPreference = "SET_PROVIDED_ADDRESS",
CancelUrl = "http://localhost:4627/Checkout/CheckoutCancel.aspx",
ReturnUrl = "http://localhost:4627/Payment/Success"
},
PurchaseUnits = new List<PurchaseUnitRequest>
{
new PurchaseUnitRequest{
ReferenceId = "PUHF", InvoiceId = "10",
Description = "Sporting Goods",
CustomId = "CUST-HighFashions",
SoftDescriptor = "HighFashions",
AmountWithBreakdown = new AmountWithBreakdown
{
CurrencyCode = "USD",
Value = "230.00",
AmountBreakdown = new AmountBreakdown
{
ItemTotal = new Money
{
CurrencyCode = "USD",
Value = "180.00"
},
Shipping = new Money
{
CurrencyCode = "USD",
Value = "30.00"
},
Handling = new Money
{
CurrencyCode = "USD",
Value = "10.00"
},
TaxTotal = new Money
{
CurrencyCode = "USD",
Value = "20.00"
},
ShippingDiscount = new Money
{
CurrencyCode = "USD",
Value = "10.00"
}
}
},
Items = new List<Item>
{
new Item
{
Name = "T-shirt",
Description = "Green XL",
Sku = "sku01",
UnitAmount = new Money
{
CurrencyCode = "USD",
Value = "90.00"
},
Tax = new Money
{
CurrencyCode = "USD",
Value = "10.00"
},
Quantity = "1",
Category = "PHYSICAL_GOODS"
},
new Item
{
Name = "Shoes",
Description = "Running, Size 10.5",
Sku = "sku02",
UnitAmount = new Money
{
CurrencyCode = "USD",
Value = "45.00"
},
Tax = new Money
{
CurrencyCode = "USD",
Value = "5.00"
},
Quantity = "2",
Category = "PHYSICAL_GOODS"
}
},
ShippingDetail = new ShippingDetail
{
Name = new Name
{
FullName = "John Doe"
},
AddressPortable = new AddressPortable
{
AddressLine1 = "123 Townsend St",
AddressLine2 = "Floor 6",
AdminArea2 = "San Francisco",
AdminArea1 = "CA",
PostalCode = "94107",
CountryCode = "US"
}
}
}
}
};
return orderRequest;
}
} |
Partager