PayPalExpressCheckout开发 最近在对接PayPal,告⼀段落,总结⼀下。
说⼀下PayPal Express Checkout的⽀付流程。
⼀切准备就绪,现在来⼀步⼀步的完成这个流程。
先看SetExpressCheckout,这是创建⽀付的⽅法。
public static SetExpressCheckoutResponType SetExpressCheckout()
{
//配置服务信息
//API凭证
string APIAccountName = "*********_";
string APIAccountPassword = "**********";
string Signature = "**********************************************";
/
/API凭证
CustomSecurityHeaderType header = new CustomSecurityHeaderType
{
Credentials = new UrIdPasswordType
{
Urname = APIAccountName,
Password = APIAccountPassword,
Signature = Signature
}
};
PayPalAPIAASoapBinding rvice = new PayPalAPIAASoapBinding();
rvice.Url = "api-3t./nvp"; //这⾥是测试地址
rvice.RequesterCredentials = header;
//配置服务信息
//配置请求信息
SetExpressCheckoutReq req = new SetExpressCheckoutReq();
//商品详情
var cartItems = new List<PaymentDetailsItemType>();
cartItems.Add(new PaymentDetailsItemType
{
Name ="ProductName",
Quantity = "3",
Amount = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value ="2"
},
Description = "ProductName"
});
cartItems.Add(new PaymentDetailsItemType
{
Name = "ProductName2",
Quantity = "4",
Amount = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value = "4"
},
Description = "ProductName2"
});
cartItems.Add(new PaymentDetailsItemType
{
Name = "Discount",
Quantity = "1",
Amount = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value = "-12"
},
Description = "Discount"
});
//商品详情
req.SetExpressCheckoutRequest = new SetExpressCheckoutRequestType
{
Version = "98.0", //版本
SetExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType
{
PaymentAction = PaymentActionCodeType.Sale,
ReturnURL = "/", //返回url
CancelURL = "/", //取消url
//付款详情
PaymentDetails=new PaymentDetailsType[]{
new PaymentDetailsType{
ItemTotal = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value ="10"
},
ShippingTotal = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value = "5"
},
TaxTotal = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value = "2"
},
OrderTotal = new BasicAmountType
{
currencyID = CurrencyCodeType.USD,
Value = "17"
和泉式部},
PaymentDetailsItem = cartItems.ToArray()
}
}
}
};
/
/配置请求信息
SetExpressCheckoutResponType respon = rvice.SetExpressCheckout(req);
return respon;
}
这⾥有⼏个地⽅要说⼀下:
第⼀:折扣信息是和商品写在⼀起的。
第⼆:价格这⾥,ItemTotal=商品价格+折扣价格,OrderTotal=ItemTotal+ShippingTotal+TaxTotal。这两个价格不能出错。 第三:这⾥还可以把⾃⼰的地址加进去,在detail下将AddressOverride设置成1,然后就可以在Address中添加⾃⼰想要传给PayPal的信息了。这⾥要注意的是,地址信息不能乱写,PayPal会验证下。这⾥还有个问题就是我的Name和Phone怎么也传不回
来,希望可以有⼈帮我解决⼀下,谢谢。
第四:这⾥的ReturnUrl和CancelUrl,只⽤Url.Action()不⾏,会报错,要全地址,带http的,这⾥提供两中⽅法。
1. string baUrl = Request.Url.Scheme + "://" + Request.Url.Authority;
string ReturnUrl=baUrl+Url.Action("ReturnUrl");党员远教
2. Url.Action("ReturnUrl", "Test", null, "http")
AddressOverride = "1",
Address = new AddressType { }
获取到respon后,就可以取其中的Token来跳转到PayPal去了,找不到Token的看问题⼀。这⾥还是测试地
址:"www./cgi-bin/webscr?cmd=_express-checkout&token=" + respon.Token。
输⼊账号信息,⽀付,调回⽹站,调⽤GetExpressCheckout⽅法。
public static GetExpressCheckoutDetailsResponType GetExpressCheckout(string token)
{
GetExpressCheckoutDetailsReq req = new GetExpressCheckoutDetailsReq();
req.GetExpressCheckoutDetailsRequest = new GetExpressCheckoutDetailsRequestType
{
Token = token,
Version = APIVersion
};
GetExpressCheckoutDetailsResponType respon = rvice.GetExpressCheckoutDetails(req);杂乱无章的意思
return respon;
}
这⾥将Service和APIVersion移出去,⽅便每⼀个⽅法调⽤。
public static PayPalAPIAASoapBinding rvice
{
get
{
return new PayPalAPIAASoapBinding
{
Url = "api-3t./nvp",
RequesterCredentials = new CustomSecurityHeaderType
{
Credentials = new UrIdPasswordType
{
Urname = "**********_",
Password = "*********",
Signature = "************************"
}
}
};
}
}
public static string APIVersion
{
get
{
return"98.0";
}
}
最后将GetExpressCheckout的返回值作为DoExpressCheckout的参数,⽤来获取付款。 public static DoExpressCheckoutPaymentResponType DoExpressCheckout(GetExpressCheckoutDetailsResponType responGet) {
PayerInfoType paymentInfo = responGet.GetExpressCheckoutDetailsResponDetails.PayerInfo;
DoExpressCheckoutPaymentReq req = new DoExpressCheckoutPaymentReq();
req.DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType
{
Version = APIVersion,
DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType
{
主体工程
PaymentAction = PaymentActionCodeType.Sale,
PaymentActionSpecified = true,
Token = responGet.GetExpressCheckoutDetailsResponDetails.Token,
PayerID = paymentInfo.PayerID,
PaymentDetails = new PaymentDetailsType[] {
new PaymentDetailsType{
OrderTotal = new BasicAmountType{
Value = responGet.GetExpressCheckoutDetailsResponDetails.PaymentDetails[0].OrderTotal.Value,
currencyID = CurrencyCodeType.USD
},
Custom = "123",
ButtonSource = "nopCommerceCart"
}
}
}
};
DoExpressCheckoutPaymentResponType respon = rvice.DoExpressCheckoutPayment(req);
return respon;
}
这样,付款就完成了,这是⼀个⼤概的流程,中间还有⼀些判断错误,返回错误到页⾯的代码,没有写,⾃⼰加。
退款
这⾥需要新添加⼀个服务对象,参数社么的都与rvice⼀样,类型不同:PayPalAPISoapBinding rvice2
六年级下册数学练习题 public static RefundTransactionResponType RefundOrder(string transactionID)
{
RefundTransactionReq req = new RefundTransactionReq();
req.RefundTransactionRequest = new RefundTransactionRequestType {
RefundType = RefundType.Partial, //退款类型,是部分退款还是全额退款
//退款⾦额,全额退款的话不需要这个参数
Amount = new BasicAmountType{
currencyID = CurrencyCodeType.USD,
Value = "100"
},
RefundTypeSpecified = true,
Version = APIVersion,
//付款单的 transactionID
TransactionID = transactionID
};
RefundTransactionResponType respon = rvice2.RefundTransaction(req);
return respon;
超市水果}
信⽤卡付款
这⾥不是很清楚,⼤概说⼀下,有待改进。信⽤卡付款这⾥,不需要跳转到PayPal⽀付页⾯去登录,在本站就可以进⾏付款。这⾥⽤的是PayPal Direct Payment API。
public static DoDirectPaymentResponType DirectPayment()
{
DoDirectPaymentRequestDetailsType details = new DoDirectPaymentRequestDetailsType
{
//IPAddress 这⾥填什么??
IPAddress = "127.0.0.1",
//信⽤卡信息
CreditCard = new CreditCardDetailsType
{
CreditCardNumber = "****96548042****",
CreditCardType = CreditCardTypeType.Visa,
ExpMonthSpecified = true,
ExpMonth = 2,
ExpYearSpecified = true,
ExpYear = 2018,
CVV2 = "234",
CreditCardTypeSpecified = true,
CardOwner = new PayerInfoType
{
PayerCountry = CountryCodeType.US,
Address = new AddressType
{
CountrySpecified = true,
Street1 = "Street1",
Street2 = "Street2",
CityName = "City",
StateOrProvince = "CA",
Country = CountryCodeType.US,
PostalCode = "Zip"
},
Payer = "",
PayerName = new PersonNameType
{
FirstName = "FirstName",
LastName = "LastName"
}
}
},
//⽀付详情
PaymentDetails = new PaymentDetailsType
{
OrderTotal = new BasicAmountType
{
Value = "100",
currencyID = CurrencyCodeType.USD
},
Custom = "123",
ButtonSource = "nopCommerceCart",
ShipToAddress = new AddressType {
Name = "Name",
Street1 = "Street1",
CityName = "City",
StateOrProvince = "State",
PostalCode = "Zip",
Country = CountryCodeType.US,
CountrySpecified = true
}防灾减灾日是几月几日
},
PaymentAction=PaymentActionCodeType.Sale
};
DoDirectPaymentReq req = new DoDirectPaymentReq();
req.DoDirectPaymentRequest = new DoDirectPaymentRequestType
{
为什么柯基要断尾
Version = APIVersion,
DoDirectPaymentRequestDetails = details
};
DoDirectPaymentResponType respon = rvice.DoDirectPayment(req);
return respon;
}
问题⼀:
SetExpressCheckout下执⾏后的SetExpressCheckoutResponType respon,respon.Token没有值。
Answer:找到wcf引⽤中的下⾯代码:
[System.Xml.Serialization.XmlElementAttribute(Order=6)]
public System.Xml.XmlElement Any {
get {
return this.anyField;
}
t {
this.anyField = value;
this.RaiPropertyChanged("Any");
}
}
将[System.Xml.Serialization.XmlElementAttribute(Order=6)]替换成[System.Xml.Serialization.XmlIgnoreAttribute()]