[英]PayPal Express Checkout Integration with JS: Production ID
我刚刚在CMS中实现了PayPal Express Checkout集成。 使用我们的Sandbox-ID可以正常工作,但是对于我们的生活环境,我们的客户必须将其Paypal API凭据放入我们的系统中。 在paypal开发人员文档中,我找不到任何解决方案,如何将这些客户凭证集成到Express Checkout集成代码中。
有人能帮帮我吗?
<div id='paypal-button'></div>
<script>
paypal.Button.render({
env: 'production', // Specify 'sandbox' for the test environment, 'production'
style: {
size: 'medium',
color: 'silver',
shape: 'rect'
},
client: {
sandbox: 'ASoNxxxxxxxxxxxxxxxxxxx',
production: '$customer_api'
},
payment: function(resolve, reject) {
// Set up the payment here, when the buyer clicks on the button
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client, {
'intent': 'sale',
'payer':{
'payer_info': {
'email': '$email',
'first_name': '$vorname',
'last_name': '$nachname',
'shipping_address': {
'line1': '$strasse',
'city': '$ort',
'postal_code': '$plz',
'country_code': '$land',
'recipient_name': '$firma'
}
}
},
transactions: [
{
amount: {
'total': '$total',
'currency': '$currency',
'details':{
'subtotal':'$total_netto',
'tax':'$tax',
'shipping':'$shipping',
}
},
},
],
});
},
commit: true,
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function() {
location.href = '/shop/checkout/mode/4'
});
},
onCancel: function(data, actions) {
return actions.redirect();
},
onError: function(err) {
location.href = '/shop/checkout/mode/4'
}
}, '#paypal-button');
</script>
我找到了一个解决方案,并编写了一个php脚本来实现快速的paypal-express-checkout-button :(通过使用此sdk: https : //github.com/paypal/PayPal-PHP-SDK/releases )
<?php
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payee;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
class Application_Model_Paypal{
public function checkout(
$total = 0, //Cost incl. tax
$subtotal = 0, //Cost without tax
$shipping = 0, //Cost for Shipping inkl. tax
$tax = 0, //Tax
$currency = 'EUR', //EUR, USD, ...
$address = array(), //Array() of addressdata
$items = array(), //Array() of Items, [name, amount, tax, price(without. tax), number, description]
$clientid = '', //REST-API-ClientID
$clientsecret = '', //REST-API-Secret
$payee_email = '', //Emailadresse des PayPal-Accounts
$url_success = '', //URL in case of payment success
$url_error = '' //URL in case of payment error
){
/****
*
* ATTENTION:
* total =!= subtotal + shipping + tax
*
* ITEMS:
* subtotal = SUM_OF_ITEMS( price * amount )
*
* ***/
//Clientaddress data
$email = $address['email'];
$firma = $address['firma'];
$vorname = $address['vorname'];
$nachname = $address['nachname'];
$strasse = $address['strasse'];
$plz = $address['plz'];
$ort = $address['ort'];
$land = $address['land'];
//PayPalData
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$_itemlist = array();
foreach($items as $it){
$i = new Item();
$i->setName($it['name'])
->setCurrency($currency)
->setDescription($it['description'])
->setQuantity($it['amount'])
->setTax($it['tax'])
->setSku($it['number']) // Similar to "item_number"
->setPrice($it['price']);
//Item back array
array_push($_itemlist, $i);
}
//Paypal itemlist
$itemList = new ItemList();
$itemList->setItems($_itemlist);
$details = new Details();
$details->setShipping($shipping)
->setTax($tax)
->setSubtotal($subtotal);
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($total)
->setDetails($details);
$payee = new Payee();
$payee->setEmail($payee_email);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setPayee($payee)
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$url_success")
->setCancelUrl("$url_error");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
try {
$clientid = $clientid;
$clientsecret = $clientsecret;
$apiContext = new ApiContext(new OAuthTokenCredential($clientid, $clientsecret));
$apiContext->setConfig(
array(
'mode' => 'live',
));
$payment->create($apiContext);
} catch (Exception $e) {
/*
* print_r($_itemlist);
echo "<div class='alert alert-danger'>".
$e->getMEssage()."<br>".
"<pre>".$e->getData()."</pre><br>".
"Total: $total <br> Subtotal: $subtotal <br> Shipping: $shipping <br> Tax: $tax <br>".
"<pre>$payment</pre>".
"</div>";
*/
}
$res = "
<script src='https://www.paypalobjects.com/api/checkout.js'></script>
<div id='checkout_button'></div>
<script>
// Render the PayPal button
paypal.Button.render({
// Set your environment
env: 'production', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
style: {
size: 'medium',
color: 'silver',
shape: 'rect'
},
client: {
production: '$clientid'
},
// Wait for the PayPal button to be clicked
payment: function() {
// Make a client-side call to the REST api to create the payment
return paypal.rest.payment.create(this.props.env, this.props.client,
".$payment."
);
},
// Wait for the payment to be authorized by the customer
onAuthorize: function(data, actions) {
// Execute the payment
return actions.payment.execute().then(function() {
location.href='$url_success';
});
},
// Wait for the payment to be authorized by the customer
onError: function(err) {
// Show an error page here, when an error occurs
location.href = '$url_error';
},
onCancel: function(data, actions) {
return actions.redirect();
},
}, '#checkout_button');
</script>
";
return $res;
}
}
每个商人都必须在开发人员REST-API-Tool中创建一个新应用,并且必须使用paypal-business-account。 创建这些应用程序凭证后,您必须切换到实时环境。 登录到您的企业帐户,并通过此链接访问“ REST-API应用程序”: https : //developer.paypal.com/developer/applications/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.